这个题比较经典,既有线程又有唤醒
public class ThreadTest {
public static Object obj = new Object();
public static int number = 10;
public static void main(String[] args) {
MyThread my = new MyThread();
MyThread my2 = new MyThread();
Thread t1 = new Thread(my2, "2号窗口");
Thread t2 = new Thread(my, "1号窗口");
t1.start();
t2.start();
}
static class MyThread implements Runnable {
public void run() {
while (true)
synchronized (obj) {
if (number > 0) {
System.out.println(Thread.currentThread().getName()
+ "正在卖票," + "还剩" + number + "张票");
number--;
obj.notifyAll();
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else
break;
}
}
}
}
Comments | NOTHING