import java.util.Timer;import java.util.TimerTask;import java.util.concurrent.ConcurrentHashMap;public class ConcurrentHashMapTest { private static ConcurrentHashMaptimerMap = new ConcurrentHashMap (); public static void main(String[] args) { for(int i=0;i<100;i++){ new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub addTimerTask("1",new TimerTask() { @Override public void run() { // TODO Auto-generated method stub System.out.println(Thread.currentThread().getName()+"===="+timerMap.size()); } }, 1000, 1000); } }).start();; } } public static void addTimerTask(String ObjectKey,TimerTask timerTask,long delay, long period){ if(!timerMap.containsKey(ObjectKey)){ Timer timer = new Timer(); Timer timerOld = timerMap.putIfAbsent(ObjectKey, timer); if(timerOld==null){ System.out.println("我被初始化了>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); timer.schedule(timerTask, delay,period); } } }}
http://www.myexception.cn/other/1486433.html