一个关于线程的问题
public class ThreadSyncDemo2{
public static void main(String []args){
ThreadTest t=new ThreadTest();
new Thread(t).start();
new Thread(t).start();
}
}
class ThreadTest implements Runnable{
private int tickets=100;
public void run(){
while(true){
synchronized(this){
if(tickets>0){
try{
Thread.sleep(3000);
}
catch(Exception e){
System.out.println(e.getMessage());
}
System.out.println(Thread.currentThread().getName()+"正在买第"+tickets--+"号票");
}
}
}
}
}
问题:执行中,两个线程是不断交替运行的,但是我在代码中怎么没找到使县城交替运行的代码?难道线程的交替是默认完成的?