线程中JDK1.5版本后的LOCK用法

来源:互联网 发布:windows dns 缓存 编辑:程序博客网 时间:2024/05/21 10:27

需求:实现生产者和消费者,两个生产者两个消费者

class Resource{private String name;private int count=1;boolean flag=false;private Lock lock=new ReentrantLock() ;private Condition condition_pro =lock.newCondition() ;private Condition  condition_cus=lock.newCondition() ;public  void setName(String name)throws InterruptedException{lock.lock();try{while(flag)condition_pro.await();this.name=name+"---"+count++;System.out.println(Thread.currentThread()+"生产"+this.name);flag=true;condition_cus.signal() ;}catch (InterruptedException e1){System.out.println(e1.toString());}finally{lock.unlock() ;}//this.notify();/*原因:t1先抢到执行权,判断falg为假,执行生产打印出"生产6786",然后将flag置为true,t2进行判断flag为true则停在wait()处放弃资格。t3获取执行权执行一次消费打印出“消费6786”,这是正常模式。然后t3将flag置为false,t4进行判断flag为假则停在wait()处。这个时候t1有可能再次抢到执行权,判断flag为假,直接进行生产,生产后直接唤醒所有线程,因为t2是最先在线程池中等待的线程,所以将会第一个被唤醒,t2被唤醒后将不会去判断flag条件,而是直接进行生产,这就是造成生产两次的原因。*/}public  void out()throws InterruptedException {lock.lock();try{while(!flag)condition_cus.await();System.out.println(Thread.currentThread()+"消费------"+this.name);flag=false;condition_pro.signal();}catch (InterruptedException e2){System.out.println(e2.toString());}finally{lock.unlock();}}}class Produce implements Runnable{private Resource r;Produce(Resource r){this.r=r;}public void run(){while (true){try{r.setName("商品");}catch (InterruptedException e3){System.out.println(e3.toString());}}}}class Custom implements Runnable{private Resource r;Custom(Resource r){this.r=r;}public void run(){while (true){try{r.out();}catch (InterruptedException e4){System.out.println(e4.toString());}}}}class DeadLock{public static void main(String[] args){Resource r=new Resource();Produce p=new Produce(r);Custom c=new Custom(r);Thread t1=new Thread(p);Thread t2=new Thread(p);Thread t3=new Thread(c);Thread t4=new Thread(c);t1.start();t2.start();t3.start();t4.start();}}


0 0
原创粉丝点击