/**
* 生产者与消费者
* 多个生产者,多个消费者
* 思考:为什么会出现生产两次,消费一次,或者生产一次,消费两次
*/
package java160110;
/**
* @author LiZheng
*
*/
public class ProducerConsumerDemo {
/**
* @param args
*/
public static void main(String[] args) {
Resource resource =new Resource();
Producer producer =new Producer(resource);
Consumer consumer =new Consumer(resource);
Thread thread1 =new Thread(producer);
Thread thread2 =new Thread(producer);
Thread thread3 =new Thread(consumer);
Thread thread4 =new Thread(consumer);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
class Resource {
private String name;
private int count = 1;
private boolean flag = false;
// t1 t2
public synchronized void set(String name) {
while (flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//将if 改成while后发生全部等待的情况
this.name = name + "---" + count++;
System.out.println(Thread.currentThread().getName() + "...生产者..." + this.name);
flag = true;
// this.notify();//唤醒应该唤醒对方,notity 有可能唤醒自己本方
this.notifyAll();
}
public synchronized void out() {
while (!flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + "...消费者......." + this.name);
flag = false;
// this.notify();
this.notifyAll();
}
}
class Producer implements Runnable {
private Resource resource;
public Producer(Resource resource) {
this.resource=resource;
}
@Override
public void run() {
while (true) {
resource.set("+++商品++++");
}
}
}
class Consumer implements Runnable {
private Resource resource;
public Consumer(Resource resource) {
this.resource=resource;
}
@Override
public void run() {
while (true) {
resource.out();
}
}
}
* 生产者与消费者
* 多个生产者,多个消费者
* 思考:为什么会出现生产两次,消费一次,或者生产一次,消费两次
*/
package java160110;
/**
* @author LiZheng
*
*/
public class ProducerConsumerDemo {
/**
* @param args
*/
public static void main(String[] args) {
Resource resource =new Resource();
Producer producer =new Producer(resource);
Consumer consumer =new Consumer(resource);
Thread thread1 =new Thread(producer);
Thread thread2 =new Thread(producer);
Thread thread3 =new Thread(consumer);
Thread thread4 =new Thread(consumer);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
class Resource {
private String name;
private int count = 1;
private boolean flag = false;
// t1 t2
public synchronized void set(String name) {
while (flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//将if 改成while后发生全部等待的情况
this.name = name + "---" + count++;
System.out.println(Thread.currentThread().getName() + "...生产者..." + this.name);
flag = true;
// this.notify();//唤醒应该唤醒对方,notity 有可能唤醒自己本方
this.notifyAll();
}
public synchronized void out() {
while (!flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + "...消费者......." + this.name);
flag = false;
// this.notify();
this.notifyAll();
}
}
class Producer implements Runnable {
private Resource resource;
public Producer(Resource resource) {
this.resource=resource;
}
@Override
public void run() {
while (true) {
resource.set("+++商品++++");
}
}
}
class Consumer implements Runnable {
private Resource resource;
public Consumer(Resource resource) {
this.resource=resource;
}
@Override
public void run() {
while (true) {
resource.out();
}
}
}