欢迎使用CSDN-markdown编辑器

来源:互联网 发布:阿里云禁止ip访问网站 编辑:程序博客网 时间:2024/05/21 09:54

在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;

}

}
“`