没出现0的结果
怎么执行都是6,是版本太新了,还是电脑太慢了~唉~
public class SynchronizedDemo {
private boolean ready = false;
private int result=0;
private int number=1;
public void write(){
ready = true;
number =2;
}
public void read(){
if(ready){
result = number*3;
}
System.out.println("result的值是"+result);
}
private class ReadWriteThread extends Thread{
private boolean flag;
public ReadWriteThread(boolean flag){
this.flag = flag;
}
public void run(){
if(flag){
write();
}else{
read();
}
}
}
public static void main(String[] args){
SynchronizedDemo synDemo = new SynchronizedDemo();
synDemo.new ReadWriteThread(true).start();
synDemo.new ReadWriteThread(false).start();
}
}