多生产多消费
//RoastDuck
public class RoastDuck {
private String name;
private int count = 1;
private boolean flag=false;
public synchronized void produce(String name){
while(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name + count;
count++;
System.out.println("producer..."+Thread.currentThread().getName()+"..."+this.name);
flag=true;
notifyAll();
}
public synchronized void consume(){
while(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("consumer..."+Thread.currentThread().getName()+"..."+this.name);
flag=false;
notifyAll();
}
}
//DuckProductor
public class DuckProductor implements Runnable {
RoastDuck rd;
public DuckProductor(RoastDuck rd) {
this.rd = rd;
}
@Override
public void run() {
while (true){
rd.produce("烤鸭");
}
}
}
//DuckConsumer
public class DuckConsumer implements Runnable {
RoastDuck rd;
public DuckConsumer(RoastDuck rd) {
this.rd = rd;
}
@Override
public void run() {
while (true){
rd.consume();
}
}
}
//main
RoastDuck rd = new RoastDuck();
DuckProductor dp = new DuckProductor(rd);
DuckConsumer dc = new DuckConsumer(rd);
Thread t0 = new Thread(dp);
Thread t1 = new Thread(dp);
Thread t2 = new Thread(dc);
Thread t3 = new Thread(dc);
t0.start();
t1.start();
t2.start();
t3.start();
重点在于 while notifyAll的理解
将同步和锁封装成了对象,并将操作锁的隐式方法显示化
await singal singalAll
//RoastDuck
public class RoastDuck {
private String name;
private int count = 1;
private boolean flag=false;
Lock lock = new ReentrantLock();
Condition pro_con = lock.newCondition();
Condition com_con = lock.newCondition();
public void produce(String name){
lock.lock();
try {
while(flag){
try {
pro_con.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name + count;
count++;
System.out.println("producer5.0..."+Thread.currentThread().getName()+"..."+this.name);
flag=true;
com_con.signal();
}finally {
lock.unlock();
}
}
public void consume(){
lock.lock();
try {
while(!flag){
try {
com_con.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("consumer5.0..."+Thread.currentThread().getName()+"..."+this.name);
flag=false;
pro_con.signal();
}finally {
lock.unlock();
}
}
}
生产者消费者1
/**
* @author yuankun
* @date 2018.12.21
* @Description 锅盔资源类-每次只提供一个锅盔
*/
public class GuoKui {
private int counter = 0;
private String name;
private boolean flag = false;
// 生产者
public synchronized void Product(String name){
// 如果有锅盔则等待消费
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//生产锅盔
counter++;
this.name = name + counter;
flag = true;
System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name+" | 有锅盔了快来买!");
notifyAll();
}
// 消费者
public synchronized void consume(){
// 没有锅盔就等待生产
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
flag = false;
System.out.println(Thread.currentThread().getName()+"...消费者..."+this.name+" | 锅盔被买走!");
notifyAll();
}
}
/**
* @author yuankun
* @date 2018.12.21
* @Description 生产者线程类,调用Guokui资源
*/
public class Product implements Runnable {
private GuoKui gk;
private Thread t;
public Product(GuoKui gk) {
this.gk = gk;
t = new Thread(this);
t.start();
}
public void run() {
for(int i=0;i<10;i++){
gk.Product("袁琨牌大锅盔");
}
}
}
/**
* @author yuankun
* @date 2018.12.21
* @Description 消费者线程类,调用Guokui资源
*/
public class Consumer implements Runnable {
private GuoKui gk;
private Thread t;
public Consumer(GuoKui gk) {
this.gk = gk;
t = new Thread(this);
t.start();
}
public void run() {
for(int i=0;i<10;i++){
gk.consume();
}
}
}
/**
* @author yuankun
* @date 2018.12.21
* @Description 执行类
*/
public class Sell_Guokui {
public static void main(String args[]){
GuoKui gk = new GuoKui();
new Product(gk);
new Consumer(gk);
}
}
生产者消费者2
/**
* 共享对象Goods
* @author Administrator
*
*/
public class Goods {
private String brand;
private String name;
private boolean isFlag;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Goods(String brand, String name) {
super();
this.brand = brand;
this.name = name;
}
public Goods() {
super();
}
public synchronized void set(String brand, String name) {
if(isFlag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.setBrand(brand);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.setName(name);
this.notifyAll();
isFlag=true;
}
public synchronized void get() {
if(!isFlag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("消费者消费了"+this.getBrand()+this.getName());
notifyAll();
isFlag=false;
}
}
/**
* 生产者
* @author Administrator
*
*/
public class Produce implements Runnable {
private Goods goods;
public Produce(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
for(int i=0;i<10;i++) {
if(i%2==0) {
goods.set("北京", "烤鸭");
}else {
goods.set("成都", "兔头");
}
System.out.println("生产者生产了"+goods.getBrand()+goods.getName());
}
}
}
/**
* 消费者
* @author Administrator
*
*/
public class Consume implements Runnable {
private Goods goods;
public Consume(Goods goods) {
super();
this.goods = goods;
}
@Override
public void run() {
for(int i=0;i<10;i++) {
goods.get();
}
}
}
//测试类
public class Test {
public static void main(String[] args) throws InterruptedException {
Goods goods = new Goods();
Produce produce = new Produce(goods);
Consume consume = new Consume(goods);
Thread tpThread = new Thread(produce);
Thread tcThread = new Thread(consume);
tpThread.start();
Thread.sleep(100);
tcThread.start();
}
}