public class ProducterConsumerSample {
public static void main(String[] args) {
Resourse res = new Resourse();
//两个生产者
Producter producter1 = new Producter(res);
Producter producter2 = new Producter(res);
//三个消费者
Consumer consumer1 = new Consumer(res);
Consumer consumer2 = new Consumer(res);
Consumer consumer3 = new Consumer(res); Thread t1 = new Thread(producter1);
Thread t2 = new Thread(producter2);
Thread t3 = new Thread(consumer1);
Thread t4 = new Thread(consumer2);
Thread t5 = new Thread(consumer3);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
/**
* 显示如下
* Thread-1 生产者 ++ 苹果手机---1
* Thread-2 消费者 苹果手机---1
* Thread-0 生产者 ++ 苹果手机---2
* Thread-2 消费者 苹果手机---2
* Thread-0 生产者 ++ 苹果手机---3
* Thread-2 消费者 苹果手机---3
* Thread-0 生产者 ++ 苹果手机---4
* Thread-2 消费者 苹果手机---4
* Thread-0 生产者 ++ 苹果手机---5
* Thread-2 消费者 苹果手机---5
* Thread-0 生产者 ++ 苹果手机---6
* Thread-2 消费者 苹果手机---6
* Thread-0 生产者 ++ 苹果手机---7
* Thread-2 消费者 苹果手机---7
* Thread-0 生产者 ++ 苹果手机---8
* Thread-2 消费者 苹果手机---8
* Thread-0 生产者 ++ 苹果手机---9
* Thread-2 消费者 苹果手机---9
* Thread-0 生产者 ++ 苹果手机---10
* Thread-2 消费者 苹果手机---10
*/
}
} class Resourse {
private int productId = 1;
private String productName;
private boolean flag = false; /**
* 同步函数,解决多线程操作时的安全问题
*/
public synchronized void product(String productName) {
while (flag) { //关键点 此处一定要用while循环
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.productName = productName + "---" + productId++;
System.out.println(Thread.currentThread().getName() + " 生产者 ++ " + this.productName);
flag = true;
this.notifyAll(); //关键点 此处一定要把所有wait线程全部唤醒
} /**
* 同步函数,解决多线程操作时的安全问题
*/
public synchronized void consume() {
while (!flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " 消费者 " + this.productName);
flag = false;
this.notifyAll();
}
} class Producter implements Runnable { private Resourse res; Producter(Resourse res) {
this.res = res;
} @Override
public void run() {
while (true) {
res.product("苹果手机");
}
}
} class Consumer implements Runnable { private Resourse res; Consumer(Resourse res) {
this.res = res;
} @Override
public void run() {
while (true) {
res.consume();
}
}
}
import java.io.PrintStream;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; /**
* 使用lock实现多线程之间的数据通讯和同步
*/
public class ProducterConsumerSample {
public static void main(String[] args) {
Resourse res = new Resourse();
//两个生产者
Producter producter1 = new Producter(res);
Producter producter2 = new Producter(res);
//三个消费者
Consumer consumer1 = new Consumer(res);
Consumer consumer2 = new Consumer(res);
Consumer consumer3 = new Consumer(res); Thread t1 = new Thread(producter1);
Thread t2 = new Thread(producter2);
Thread t3 = new Thread(consumer1);
Thread t4 = new Thread(consumer2);
Thread t5 = new Thread(consumer3);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
/**
* 显示如下
* Thread-1 生产者 ++ 苹果手机---105851
* Thread-4 消费者 苹果手机---105851
* Thread-0 生产者 ++ 苹果手机---105852
* Thread-2 消费者 苹果手机---105852
* Thread-1 生产者 ++ 苹果手机---105853
* Thread-3 消费者 苹果手机---105853
* Thread-0 生产者 ++ 苹果手机---105854
* Thread-4 消费者 苹果手机---105854
* Thread-1 生产者 ++ 苹果手机---105855
* Thread-2 消费者 苹果手机---105855
* Thread-0 生产者 ++ 苹果手机---105856
* Thread-3 消费者 苹果手机---105856
*/
}
} class Resourse {
private int productId = 1;
private String productName;
private boolean flag = false;
private final Lock lock = new ReentrantLock();
private Condition condition_pro = lock.newCondition(); //生产者Condition
private Condition condition_Cons = lock.newCondition(); //消费者Condition public void product(String productName) {
lock.lock(); //加锁
try {
while (flag) { //关键点 此处一定要用while循环
try {
condition_pro.await(); //生产者等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.productName = productName + "---" + productId++;
System.out.println(Thread.currentThread().getName() + " 生产者 ++ " + this.productName);
flag = true;
condition_Cons.signal(); //唤醒消费者
} finally {
lock.unlock(); //解锁
}
} public void consume() {
lock.lock(); //加锁
try {
while (!flag) {
try {
condition_Cons.await(); //消费者等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " 消费者 " + this.productName);
flag = false;
condition_pro.signal(); //唤醒生产者
} finally {
lock.unlock(); //解锁
}
}
} class Producter implements Runnable { private Resourse res; Producter(Resourse res) {
this.res = res;
} @Override
public void run() {
while (true) {
res.product("苹果手机");
}
}
} class Consumer implements Runnable { private Resourse res; Consumer(Resourse res) {
this.res = res;
} @Override
public void run() {
while (true) {
res.consume();
}
}
}