两个线程同时售票问题
package com.xiaozhi.threads;
/*
* Runnable避免了单继承的缺陷
* 如果几个线程公用一个Runable对象的话,里面的变量可以共享
*/
public class Test3 {
public static void main(String[] args) {
RunTicket runTicket=new RunTicket();
Thread thread1=new Thread(runTicket);
Thread thread2=new Thread(runTicket);
thread1.start();
thread2.start();
}
}
class RunTicket implements Runnable{
private int num=100;
@Override
public void run() {
while(true)
{
if(num>0)
System.out.println(Thread.currentThread().getName()+"---------"+num--);
}
}
}