java - Number of Users Join At Time Case Scenarios? -
i need know same action perform @ time different users case scenarios.
for ex : 1 room there.users continuously joins room , leaves room @ time.and 1 more case no of users try join same room @ time.
how handle above example cases in java synchronization or else.please suggest me best case handle this?
i did 1 sample.please see below sample code once :
public class sample{ private arraylist<string> players = new arraylist<string>(); public synchronized void joinroom(string name) { players.add(name); //do } public synchronized void leaveroom(string name) { for(string uname : players){ if(uname.equals(name)) { players.remove(uname);//here there possibility concurrent modification exception.if gets how solve } } //do } }
but didn't know cases handle using above sample code or not.please suggest me 1 best handle cases?
thanks in advance.
you use concurrentskiplistset this. handle synchronization , more efficiently.the concurrentskiplistset allows concurrent reads , writes.
regarding concurrent modification exception, use iterator:
iterator<string> iter = players.iterator(); while(iter.hasnext()){ if(iter.next().equals(name)){ iter.remove(); break; } }
Comments
Post a Comment