一个简单的线程问题
问一下各位师兄师姐:为什么我在getCount()前面加上synchronized或者不加,得到的输出结果顺序都是正确的,以我对线程的理解,如果不加synchronized得到的结果顺序应该是有点混乱的?可是并没有出现.这是为什么?高手指点一下!
class AddCount{
int count=0;
public synchronized int getCount(){
return count++;
}
}
public class TestCountThread{
AddCount ac=new AddCount();
public static void main(String[] args){
TestCountThread tct=new TestCountThread();
for(int i=1;i<10;i++){
tct.new CountThread().start();
tct.new CountThread().start();
tct.new CountThread().start();
tct.new CountThread().start();
}
}
class CountThread extends Thread{
public void run(){
System.out.println(ac.getCount());
}
}
}