java - Removing Values from ConcurrentHashMap Properly -
i making simple client server programme allows users connect, alter name, , move rooms chat. server periodically sends heartbeat signals each client if have not been active, , removing client if don't respond.
to farther enhance server cleanup, i'm periodically checking if rooms empty, in case remove room server prevent unnecessary info building up. however, removal creates problem. i'm using concurrenthashmap maps room name concurrenthashmap holds player names , sockets. then, periodically, loop through every room, checking if contains players (has size > 0) or not. if not, remove room.
however, presents problematic situation when user picks exact moment bring together empty room when server decides clean up. since concurrenthashmap handles below-the-hood synchronization, can't synchronize specific situation such removal 100% thread safe. user might bring together room room beingness removed, causes him stuck in state of limbo.
how can prepare problem?
i maintain outer map concurrenthashmap, replace inner map chatroom class. expected activity rate in single room not seem justify such strong concurrent map.
the chatroom class should thread safe, , should have "closed" flag indicates whether room has been closed. close() method should utilize room's lock alter flag, , create subsequent operations illegal. close method should homecoming boolean value indicating whether room has been closed; should closed if , if room empty.
your idle room checker thread should phone call room.close(), , remove outer map.
java client concurrenthashmap
No comments:
Post a Comment