欢迎使用CSDN-markdown编辑器

在jdk1.5之前,只有用synchronized来实现同步,while是为了防止不判断条件而直接执行,实现同步,notifyAll()是为了防止死锁。

package com.cfq.Day827;

public class ThreadP {

    /**
     * 生产者和消费者,在jdk1.5之前的处理方式
     * @param args
     */
    public static void main(String[] args) {
        Resource r=new Resource();
        Product p=new Product(r);
        Consumer c=new Consumer(r);
        Thread t0=new Thread(p);
        Thread t1=new Thread(c);
        Thread t2=new Thread(p);
        Thread t3=new Thread(c);
        t0.start();
        t1.start();
        t2.start();
        t3.start();


    }

}
class Product implements Runnable{
    private Resource r;//有对资源的引用
    public Product(Resource r){
        this.r=r;
    }

    public void run() { 
        while(true){
            r.add("烤鸭");
        }

    }

}
class Consumer implements Runnable{
    private Resource r;//对资源的引用
    public Consumer(Resource r){
        this.r=r;
    }
    public void run() { 
        while(true){
            r.remove();
        }
    }
}
class Resource{
    private String name;
    int count;
    boolean flag=false;
    public synchronized void add(String name){
        while(flag){
            try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}
        }
        this.name=name+count;
        count++;
        System.out.println(Thread.currentThread().getName()+"生产烤鸭-----"+this.name);
        flag=true;
        this.notifyAll();
    }
    public synchronized void remove(){
        while(!flag){
            try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}
        }
        flag=false;
        this.notifyAll();
        System.out.println(Thread.currentThread().getName()+"消费烤鸭。。。。。。。。。。。。。。。。。。。。。。"+this.name);
    }
}

ReentrantLock可以挂多个条件

package com.cfq.Day827;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadP {

    /**
     * 生产者和消费者,在jdk1.5之后的处理方式
     * @param args
     */
    public static void main(String[] args) {
        Resource r=new Resource();
        Product p=new Product(r);
        Consumer c=new Consumer(r);
        Thread t0=new Thread(p);
        Thread t1=new Thread(c);
        Thread t2=new Thread(p);
        Thread t3=new Thread(c);
        t0.start();
        t1.start();
        t2.start();
        t3.start();


    }

}
class Product implements Runnable{
    private Resource r;//有对资源的引用
    public Product(Resource r){
        this.r=r;
    }

    public void run() { 
        while(true){
            r.add("烤鸭");
        }

    }

}
class Consumer implements Runnable{
    private Resource r;//对资源的引用
    public Consumer(Resource r){
        this.r=r;
    }
    public void run() { 
        while(true){
            r.remove();
        }
    }
}
class Resource{
    private String name;
    int count;
    boolean flag=false;
    Lock lock=new ReentrantLock();
      Condition pro=lock.newCondition();
      Condition con=lock.newCondition();
    public  void add(String name){
        lock.lock();
        while(flag){
            try {pro.await();} catch (InterruptedException e) {e.printStackTrace();}
        }
        this.name=name+count;
        count++;
        System.out.println(Thread.currentThread().getName()+"生产烤鸭-----"+this.name);
        flag=true;
        con.signal();
        lock.unlock();
    }
    public void remove(){
        lock.lock();
        while(!flag){
            try {con.await();} catch (InterruptedException e) {e.printStackTrace();}
        }
        flag=false;
        pro.signal();
        System.out.println(Thread.currentThread().getName()+"消费烤鸭。。。。。。。。。。。。。。。。。。。。。。"+this.name);
         lock.unlock();
    }
}
``
阻塞队列的实现

class BoundBuffer{
final Lock lock=new ReentrantLock();
final Condition product=lock.newCondition();
final Condition consumer=lock.newCondition();
Object[] obj=new Object[10];//存放是个对象的地方
int putObj=0;
int takeObj=0;
int count=0;
public void add(Object x){
lock.lock();
while(count==obj.length){
try {
product.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
obj[putObj++]=x;
if(putObj==obj.length){
putObj=0;
}
count++;
consumer.signal();
lock.unlock();
}
public Object take(){
lock.lock();
while(count==0){
try {
consumer.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Object x=obj[takeObj];
if(++takeObj==obj.length){
takeObj=0;
}
–count;
product.signal();
lock.unlock();
return x;

}

}
“`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值