package Test1;
public class Ticket implements Runnable {
private int ticket = 100;
Object obj = new Object();
@Override
public void run() {
String name = Thread.currentThread().getName();
System.out.println("线程" + name + "进入方法了...");
while (true) {
m1();
if (ticket <= 0) {
break;
}
}
}
private synchronized void m1() {
String name = Thread.currentThread().getName();
if (ticket > 0) {
System.out.println(name + "卖出了第" + (ticket--) + "张票");
}
}
}
|
|