标签:his tac 执行 ble current main als str notify
如果有多个生产者和多个消费者 像之前那样就会产生安全问题,例如 会打印两个生产者一个消费者 或者一个生产者两个消费者 。
为了防止这种情况的发生(线程醒了没有去判断标记),需要将if()改为while 这样当线程有等待状态被唤醒的时候可以进行循环判断,但是又由于这样会使同一类线程全部阻塞进入等待状态,没有唤醒另一类线程
因此需要将this.notify()变为this.notifyAll(); 将唤醒另一类的线程进行执行。
package cn.zz;
class Resource2 {
private String name;
private int count;
private boolean flag = false;
public synchronized void set(String name) {
while (flag) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.name = name + "..." + count++;
System.out.println(Thread.currentThread().getName() + "...生产者"
+ this.name);
flag = true;
this.notifyAll();
}
public synchronized void out() {
while (!flag) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + "...消费者..."
+ this.name);
flag = false;
this.notifyAll();
}
}
class Producer2 implements Runnable {
Resource2 res2;
public Producer2(Resource2 res2) {
this.res2 = res2;
}
@Override
public void run() {
while (true) {
res2.set("商品");
}
}
}
class Consumer2 implements Runnable {
Resource2 res2;
public Consumer2(Resource2 res2) {
this.res2 = res2;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
res2.out();
}
}
}
public class ProducerAndConsumer2 {
public static void main(String[] args) {
Resource2 res2 = new Resource2();
new Thread(new Producer2(res2)).start();
new Thread(new Producer2(res2)).start();
new Thread(new Consumer2(res2)).start();
new Thread(new Consumer2(res2)).start();
}
}
如上图,为改进之后运行的结果,并没有产生多个消费者对应一个生产者 或者多个生产者对应一个消费者。
标签:his tac 执行 ble current main als str notify
原文地址:https://www.cnblogs.com/twqwe/p/9749920.html