测试类
package thead;
public class testThread {
public static void main(String [] arg){
Tickets ticket = new Tickets();
Thread t1 = new Thread(ticket,"窗口一:");
Thread t2 = new Thread(ticket,"窗口二:");
Thread t3 = new Thread(ticket,"窗口三:");
t1.start();
t2.start();
t3.start();
}
}
实现了runnable接口的类
package thead;
public class Tickets implements Runnable{
private static int tickets = 100;
Object obj = new Object();
@Override
public void run() {
while (true) {
synchronized (Tickets.class) {
try {
if (tickets > 0) {
System.out.println(Thread.currentThread().getName() + "正在售出第" + (tickets--) + "张票");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
参考资料: