我改了一下,你看是不是你想要的结果
class Resource2 {
private String name;
private int count = 1;
private boolean flag = false;
public synchronized void set(String name) {
while (flag)
try {
this.wait();
} catch (InterruptedException e) {
}
this.name = name + count;
count++;
System.out.println(Thread.currentThread().getName() + "...生产者"
+ this.name);
flag = true;
// notifyAll();
notify();
}
public synchronized void get() {
while (!flag)
try {
this.wait();
} catch (InterruptedException e) {
}
System.out.println(Thread.currentThread().getName() + "...消费者..."
+ this.name);
flag = false;
// notifyAll();
notify();//唤醒等待的线程
}
}
class Producer implements Runnable {
Resource2 r;
Producer(Resource2 r) {
this.r = r;
}
public void run() {
while (true) {
r.set("烤鸭");
}
}
}
class Consumer implements Runnable {
Resource2 r;
Consumer(Resource2 r) {
this.r = r;
}
public void run() {
while (true) {
r.get();
}
}
}
public class ConsumerDemo2 {
public static void main(String[] args) {
Resource2 r = new Resource2();
Producer pro = new Producer(r);
Consumer con = new Consumer(r);
Thread t0 = new Thread(pro);
Thread t1 = new Thread(pro);
Thread t2 = new Thread(con);
Thread t3 = new Thread(con);
t0.start();
t1.start();
t2.start();
t3.start();
}
}
|
-
1.jpg
(57.99 KB, 下载次数: 7)
|