首页上一页 1 下一页尾页 3 条记录 1/1页
java 入门经典,第12章12.7 为什么运行的结果不对
发表在Java图书答疑
2014-11-26
是否精华
是
否
版块置顶:
是
否
第12章12.7 为什么运行的结果不对?加synchronized("")结果不对啊
剩余票数9
剩余票数8
剩余票数7
剩余票数6
剩余票数5
剩余票数4
剩余票数3
剩余票数2
剩余票数1
剩余票数0
剩余票数-1
剩余票数-2
剩余票数-3
public class T12_7 implements Runnable{
int num = 10;
public void run(){
while(true){
synchronized(""){
if(num > 0){
try{
Thread.sleep(100);
}catch(Exception e){
e.printStackTrace();
}
System.out.println("剩余票数"+ --num);
}
}
}
}
public static void main(String[] args){
T12_6 test = new T12_6();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
Thread t3 = new Thread(test);
Thread t4 = new Thread(test);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
剩余票数9
剩余票数8
剩余票数7
剩余票数6
剩余票数5
剩余票数4
剩余票数3
剩余票数2
剩余票数1
剩余票数0
剩余票数-1
剩余票数-2
剩余票数-3
public class T12_7 implements Runnable{
int num = 10;
public void run(){
while(true){
synchronized(""){
if(num > 0){
try{
Thread.sleep(100);
}catch(Exception e){
e.printStackTrace();
}
System.out.println("剩余票数"+ --num);
}
}
}
}
public static void main(String[] args){
T12_6 test = new T12_6();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
Thread t3 = new Thread(test);
Thread t4 = new Thread(test);
t1.start();
t2.start();
t3.start();
t4.start();
}
}