@Test public void testMultiThreaded() throws Exception { final PersistKeyValueImpl impl = injector.getInstance(PersistKeyValueImpl.class); Thread[] threads = new Thread[NUMB_THREADS]; for ( int i = 0; i < NUMB_THREADS; ++i ) { threads[i] = new Thread() { @Override public void run() { for (int i = 0; i < 100; ++i) { impl.put("key1", "value1"); impl.put("key2", "value2"); impl.put("key3", "value3"); impl.put("key4", "value4"); } } }; } for ( int i = 0; i < NUMB_THREADS; ++i ) { threads[i].start(); } for ( int i = 0; i < NUMB_THREADS; ++i ) { threads[i].join(); } }
public synchronized String put(String value) { String key = generateKey(); put(key, value); return key; }
PersistKeyValueImpl { public synchronized String put(String value) { String key = generateKey(); put(key, value); return key; } }
PersistKeyValueImpl { public synchronized String put(String value) { String key = generateKey(); put(key, value); return key; }  }
PersistKeyValueImpl { public synchronized String put(String value) { String key = generateKey(); put(key, value); return key; }  String generateKey(); Collection<String> generateKeys(int number); synchronized String getValue(String key); synchronized String put(String value); synchronized void put(String key, String value); synchronized Map<String, String> getAllKeyValues(); }
PersistKeyValueImpl { public synchronized String put(String value) { String key = generateKey(); put(key, value); return key; }  String generateKey(); Collection<String> generateKeys(int number); synchronized String getValue(String key); synchronized String put(String value); synchronized void put(String key, String value); synchronized Map<String, String> getAllKeyValues();  }