fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.concurrent.ConcurrentHashMap;
  4.  
  5. /* Name of the class has to be "Main" only if the class is public. */
  6. class Ideone implements Runnable
  7. {
  8.  
  9. private static ConcurrentHashMap<String, String> chm = null;
  10. private static final Object syncloc = new Object();
  11. private static boolean go = false;
  12.  
  13. public static void setConcurrentHashMap(ConcurrentHashMap<String, String> c) {
  14. System.out.println("Setting CHM " + System.identityHashCode(c));
  15. chm = c;
  16. }
  17.  
  18. public static ConcurrentHashMap<String, String> getConcurrentHashMap() {
  19. return chm;
  20. }
  21.  
  22. public void run() {
  23. try {
  24. synchronized (syncloc) {
  25. while (!go) {
  26. syncloc.wait();
  27. }
  28. }
  29. ConcurrentHashMap<String, String> mymap = getConcurrentHashMap();
  30. if (mymap == null) {
  31. mymap = new ConcurrentHashMap<String, String>();
  32. setConcurrentHashMap(mymap);
  33. }
  34. } catch (InterruptedException ie) {
  35. ie.printStackTrace();
  36. }
  37. }
  38.  
  39.  
  40. public static void main (String[] args) throws java.lang.Exception
  41. {
  42.  
  43. Thread[] threads = new Thread[5];
  44. for (int i = 0; i < threads.length; i++) {
  45. threads[i] = new Thread(new Ideone());
  46. threads[i].start();
  47. }
  48. synchronized (syncloc) {
  49. go = true;
  50. syncloc.notifyAll();
  51. }
  52. for (Thread t : threads) {
  53. t.join();
  54. }
  55. }
  56. }
Success #stdin #stdout 0.06s 381824KB
stdin
Standard input is empty
stdout
Setting CHM 15013136
Setting CHM 30545452
Setting CHM 15416452
Setting CHM 16329928
Setting CHM 30013691