JAVA 多线程环境下那些神坑-静态私有变量

二话不说,直接来代码:



public class testStatic {
private static Map map =new HashMap();

public static Map getMap1(){
map =new HashMap();
map.put("test", "map1");
try {
Thread.sleep(1000);//模拟cpu 线程切换
} catch (InterruptedException e) {
e.printStackTrace();
}
return map;
}

public static Map getMap2(){
map =new HashMap();
map.put("test", "map2");
try {
Thread.sleep(1000);//模拟cpu 线程切换
} catch (InterruptedException e) {
e.printStackTrace();
}
return map;
}

public static void main(String[] args){
new Thread(){
public void run(){
System.out.println("map1:"+testStatic.getMap1());
}
}.start();
System.out.println("---------");
new Thread(){
public void run(){
System.out.println("map2:"+testStatic.getMap2());
}
}.start();
}

}

结果:

---------
map1:{test=map2}
map2:{test=map2}



你可能感兴趣的:(java)