Note that there are some explanatory texts on larger screens.

plurals
  1. POJava ConcurrentHashMap not thread safe.. wth?
    primarykey
    data
    text
    <p>I was using HashMap before like </p> <pre><code> public Map&lt;SocketChannel, UserProfile&gt; clients = new HashMap&lt;SocketChannel, UserProfile&gt;(); </code></pre> <p>now I've switched to ConcurrentHashMap to avoid synchronized blocks and now i'm experiencing problems my server is heavily loaded with 200-400 concurrent clients every second which is expected to grow over time.</p> <p>which now looks like this</p> <pre><code>public ConcurrentHashMap&lt;SocketChannel, UserProfile&gt; clients = new ConcurrentHashMap&lt;SocketChannel, UserProfile&gt;(); </code></pre> <p>My server design works like this. I have a worker thread(s) for processing huge amounts of packets. Each packet is checked with a packetHandler sub-routine (not part of thread) pretty much any client can call it at anytime it's almost like static but it isn't.</p> <p>My whole server is mostly single threaded except for the packet processing portion.</p> <p>Anyways so when someone uses a command like count up all clients online and get some information from them.</p> <p>It is also possible that clients can get disconnected and removed from <strong>ConcurrentHashMap</strong> while the counting is in progress (which causes my problems).</p> <p>Also I'd like to add some code here.</p> <pre><code> int txtGirls=0; int vidGirls=0; int txtBoys=0; int vidBoys=0; Iterator i = clients.values().iterator(); while (i.hasNext()) { UserProfile person = (UserProfile)i.next(); if(person != null) { if(person.getChatType()) { if(person.getGender().equals("m")) vidBoys++; else //&lt;-- crash occurs here. vidGirls++; } else if(!person.getChatType()) { if(person.getGender().equals("m")) txtBoys++; else txtGirls++; } } } </code></pre> <p>I mean of course i'm going to fix it by adding a try-catch Exception inside the Iterator to skip these null clients.</p> <p>But what I don't understand if it checks above if(person != null) shouldn't the code nested automatically works..</p> <p>if it doesn't means it got removed while it was iterating which should be impossible since it's thread safe wtf?</p> <p>What should I do? or is try-catch Exception the best way?</p> <p>Here is the exception</p> <pre><code>java.lang.NullPointerException at Server.processPackets(Server.java:398) at PacketWorker.run(PacketWorker.java:43) at java.lang.Thread.run(Thread.java:636) </code></pre> <p>The processPackets contains the code above. and the comment indicates the line count #</p> <p>Thanks for enlightening me.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload