@Test public void testGet__multipleThreads() throws InterruptedException { final List<SimpleDateFormat> list = Collections.synchronizedList(new ArrayList<SimpleDateFormat>()); Thread[] threads = new Thread[10]; for (int i = 0; i < 10; i++) { threads[i] = new Thread(new Runnable() { @Override public void run() { list.add(SimpleDateFormatPool.get("dd-MM-yyyy")); } }); threads[i].start(); } for (int i = 0; i < 10; i++) { threads[i].join(); } for (int i = 0; i < 10; i++) { SimpleDateFormat sdf1 = list.get(i); for (int j = 0; j < 10; j++) { if (j != i) { SimpleDateFormat sdf2 = list.get(j); assertNotSame(sdf1, sdf2); } } } }
public static SimpleDateFormat get(String pattern) { Map<String, SimpleDateFormat> map = pool.get(); SimpleDateFormat sdf = map.get(pattern); if (sdf == null) { sdf = new SimpleDateFormat(pattern); sdf.setLenient(false); map.put(pattern, sdf); } return sdf; }
SimpleDateFormatPool { public static SimpleDateFormat get(String pattern) { Map<String, SimpleDateFormat> map = pool.get(); SimpleDateFormat sdf = map.get(pattern); if (sdf == null) { sdf = new SimpleDateFormat(pattern); sdf.setLenient(false); map.put(pattern, sdf); } return sdf; } }
SimpleDateFormatPool { public static SimpleDateFormat get(String pattern) { Map<String, SimpleDateFormat> map = pool.get(); SimpleDateFormat sdf = map.get(pattern); if (sdf == null) { sdf = new SimpleDateFormat(pattern); sdf.setLenient(false); map.put(pattern, sdf); } return sdf; }  }
SimpleDateFormatPool { public static SimpleDateFormat get(String pattern) { Map<String, SimpleDateFormat> map = pool.get(); SimpleDateFormat sdf = map.get(pattern); if (sdf == null) { sdf = new SimpleDateFormat(pattern); sdf.setLenient(false); map.put(pattern, sdf); } return sdf; }  static SimpleDateFormat get(String pattern); }
SimpleDateFormatPool { public static SimpleDateFormat get(String pattern) { Map<String, SimpleDateFormat> map = pool.get(); SimpleDateFormat sdf = map.get(pattern); if (sdf == null) { sdf = new SimpleDateFormat(pattern); sdf.setLenient(false); map.put(pattern, sdf); } return sdf; }  static SimpleDateFormat get(String pattern);  }