StartLetterRankMP implements Cloneable { @MessageHandler public void handleWordCount(LetterCount letterCount) { if(map == null) { synchronized(this) { if(map == null) { map = new ConcurrentHashMap<Letter, LetterCount>(); } } } map.put(letterCount.getLetter(), letterCount); logger.info("received message for " + letterCount); }  @MessageHandler void handleWordCount(LetterCount letterCount); @Override StartLetterRankMP clone(); @Output void output();  }
@Test public void handleWordCountTest() throws CloneNotSupportedException, InterruptedException { final StartLetterRankMP clone1 = mp.clone(); Thread t1 = new Thread(new Runnable() { @Override public void run() { for(int j=0; j<20; j++) { for(int i =65; i<85; i++) clone1.handleWordCount(new LetterCount(new Letter((char)i), j)); } } }); Thread t2 = new Thread(new Runnable() { @Override public void run() { for(int j=0; j<20; j++) { for(int i=90; i>75; i--) clone1.handleWordCount(new LetterCount(new Letter((char)i), j)); } } }); t1.start(); t2.start(); t1.join(); t2.join(); Assert.assertNotNull(clone1.map); Assert.assertEquals(26, clone1.map.size()); }