package byetickets;
public class MyThread implements Runnable{
private int tickets=1;//票数
@Override
public void run() {
while(true){
synchronized (this) {
if (tickets > 100) {//共有100张票
break;
}
System.out.println(Thread.currentThread().getName() + "出售了第" + tickets + "张票!");
tickets++;
}
}
}
}
1.2售票点的模拟
package byetickets;
public class Test01 {
public static void main(String[] args) {
MyThread my=new MyThread();
Thread t1=new Thread(my,"售票点1");
Thread t2=new Thread(my,"售票点2");
Thread t3=new Thread(my,"售票点3");
Thread t4=new Thread(my,"售票点4");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
|
|