public class App2 {
public static void main( String[] args ) throws Exception
{
System.out.println("虚拟机内核数:"+Runtime.getRuntime().availableProcessors());
Runnable r = new Runnable() {
Integer count = 100;
@Override
public void run() {
threadRun(count);
}
};
Thread ct1 = new Thread(r,"子线程1号");
Thread ct2 = new Thread(r,"子线程2号");
Thread ct3 = new Thread(r,"子线程3号");
Thread ct4 = new Thread(r,"子线程4号");
Thread ct5 = new Thread(r,"子线程5号");
Thread ct6 = new Thread(r,"子线程6号");
Thread ct7 = new Thread(r,"子线程7号");
Thread ct8 = new Thread(r,"子线程8号");
Thread ct9 = new Thread(r,"子线程9号");
ct1.start();
ct2.start();
ct3.start();
ct4.start();
ct5.start();
ct6.start();
ct7.start();
ct8.start();
ct9.start();
}
/*
* synchronized 无效问题?
* */
synchronized private static void threadRun( Integer count){
try {
count++;
System.out.println(Thread.currentThread().getName() + "--计算count,计算后值为:" + count);
}catch (Exception e){}
}
}
}
Integer
属于不可变对象,你每次修改 count 的值,其实 count 的引用都已经被改变了。你可以自己模拟一个内部值的可变的Integer
对象:然后你把 count 的类型声明为
MutableInt
,count++
替换为count.inc()
—— 就可以看到你想要的结果: