62,614
社区成员
发帖
与我相关
我的任务
分享
class ThreadDemo{
public static void main(String [] args){
ThreadTest tt = new ThreadTest();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
}
}
class ThreadTest implements Runnable{
public void run(){
int tickets = 100;
while(true && tickets>0){
System.out.println(Thread.currentThread().getName()+" saling the ticket of "+tickets--);
}
}
}
class ThreadTest implements Runnable{
int tickets = 100;
public void run(){
while(tickets>0){
try{
Thread.sleep(100);
}catch(Exception e){}
synchronized(this){
if(tickets>0)System.out.println(Thread.currentThread().getName()+" saling the ticket of "+tickets--);
}
}
}
}
class ThreadTest implements Runnable{
int tickets = 100;
public void run(){
synchronized(this){
while(true && tickets>0){
System.out.println(Thread.currentThread().getName()+" saling the ticket of "+tickets--);
}
}
}
}