import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class ConcurrentHashMapTest {
private static final ConcurrentMap<String, String> factory = new ConcurrentHashMap<String, String>();
public static void main(String[] args) {
for(int i=0;i<10;i++){
new Thread(i+""){
public void run(){
String tf = factory.get("wang");
if (tf == null) {
try {
Thread.sleep((int)(Math.random()*1000));
factory.put("wang", this.getName());
Thread.sleep((int)(Math.random()*1000));
} catch (Throwable e) {
e.printStackTrace();
}
}
System.out.println(this.getName()+"_"+factory.get("wang"));
}
}.start();
}
}
}
上面这段代码中concurrentHashMap好像没起到作用,有高手帮忙解惑下吗