level 1
♂温泉♀
楼主
class MyThread implements Runnable{ // 继承Thread类,作为线程的实现类
private int ticket = 5 ; // 表示一共有5张票
public void run(){ // 覆写run()方法,作为线程 的操作主体
for(int i=0;i<100;i++){
if(this.ticket>0){
System.out.println("卖票:ticket = " + ticket--) ;
}
}
}
};
public class Demo02{
public static void main(String args[]){
MyThread mt = new MyThread() ; // 实例化对象
new Thread(mt).start() ; // 调用线程主体
new Thread(mt).start() ; // 调用线程主体
new Thread(mt).start() ; // 调用线程主体
}
};
为什么这样会出现
5
3
4
2
1
的结果
2011年07月06日 09点07分
1
private int ticket = 5 ; // 表示一共有5张票
public void run(){ // 覆写run()方法,作为线程 的操作主体
for(int i=0;i<100;i++){
if(this.ticket>0){
System.out.println("卖票:ticket = " + ticket--) ;
}
}
}
};
public class Demo02{
public static void main(String args[]){
MyThread mt = new MyThread() ; // 实例化对象
new Thread(mt).start() ; // 调用线程主体
new Thread(mt).start() ; // 调用线程主体
new Thread(mt).start() ; // 调用线程主体
}
};
为什么这样会出现
5
3
4
2
1
的结果