JDK
package com.taobao.mmp.test;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.taobao.mmp.dataobject.ServiceDO;
publicclass TTTT {
privatestatic Map<Long, ServiceDO> widgetCacheMap = new ConcurrentHashMap<Long, ServiceDO>();
/**
* @param args
*/
publicstaticvoid main(String[] args) {
// TODO Auto-generated method stub
for(int i=0;i<10000;i++){
Thread tt = new Thread(new Rund());
tt.start();
}
}
staticclass Rund implements Runnable{
publicvoid run() {
// TODO Auto-generated method stub
test();
}
/**
* 1W次,总有那么几次线程不安全
*/
publicvoid test(){
TTTT tt = new TTTT();
tt.set();
int s1 = widgetCacheMap.get(1L).getStatus();
tt.change();
int s2 = widgetCacheMap.get(1L).getStatus();
if(s1==s2){
System.out.println(s1+":"+s2);
}
}
}
publicvoid set() {
Map mm= new HashMap();
ServiceDO ss = new ServiceDO();
ss.setStatus(1);
mm.put(1L, ss);
widgetCacheMap = mm;
}
publicvoid change(){
Map mm= new HashMap();
ServiceDO ss = new ServiceDO();
ss.setStatus(2);
mm.put(1L, ss);
widgetCacheMap = mm;
}
}
2:2
2:2
2:2
publicvoid set() {
//Map mm= new HashMap();
ServiceDO ss = new ServiceDO();
ss.setStatus(1);
widgetCacheMap.put(1L, ss);
//widgetCacheMap = mm;
}
publicvoid change(){
//Map mm= new HashMap();
ServiceDO ss = new ServiceDO();
ss.setStatus(2);
widgetCacheMap.put(1L, ss);
//widgetCacheMap = mm;
}
1:1
2:2
2:2
2:2
2:2
publicvoid set() {
synchronized (widgetCacheMap) {
Map mm= new HashMap();
ServiceDO ss = new ServiceDO();
ss.setStatus(1);
mm.put(1L, ss);
widgetCacheMap = mm;
}
}
publicvoid change(){
synchronized (widgetCacheMap) {
Map mm= new HashMap();
ServiceDO ss = new ServiceDO();
ss.setStatus(2);
mm.put(1L, ss);
widgetCacheMap = mm;
}
}
publicvoid test(){
synchronized (widgetCacheMap) {
TTTT tt = new TTTT();
tt.set();
int s1 = widgetCacheMap.get(1L).getStatus();
tt.change();
int s2 = widgetCacheMap.get(1L).getStatus();
if(s1==s2){
System.out.println(s1+":"+s2);
}
}
}
/**
* 1W次,总有那么几次线程不安全
*/
publicvoid test(){
TTTT tt = new TTTT();
tt.set();
int s1 = -1;
synchronized (widgetCacheMap) {
s1 = widgetCacheMap.get(1L).getStatus();
}
tt.change();
int s2 = -2;
synchronized (widgetCacheMap) {
s2 = widgetCacheMap.get(1L).getStatus();
}
if(s1==s2){
System.out.println(s1+":"+s2);
}
}
/**
* 1W次,总有那么几次线程不安全
*/
publicvoid test(){
synchronized (widgetCacheMap) {
TTTT tt = new TTTT();
tt.set();
int s1 = -1;
s1 = widgetCacheMap.get(1L).getStatus();
tt.change();
int s2 = -2;
s2 = widgetCacheMap.get(1L).getStatus();
if(s1==s2){
System.out.println(s1+":"+s2);
}
}
}
/**
* 1W次,总有那么几次线程不安全
*/
publicvoid test(){
synchronized ("") {
TTTT tt = new TTTT();
tt.set();
int s1 = -1;
s1 = widgetCacheMap.get(1L).getStatus();
tt.change();
int s2 = -2;
s2 = widgetCacheMap.get(1L).getStatus();
if(s1==s2){
System.out.println(s1+":"+s2);
}
}
}