多线程中使用Lock和Condition机制
来源:互联网 发布:instanceof php 编辑:程序博客网 时间:2024/06/07 06:22
1、资源类
package cn.toltech.t_concurrent;import java.util.ArrayList;import java.util.List;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * Created by sz0816 on 15-1-2 */public class CakeBox { private List<Integer> list = new ArrayList<Integer>(); private final int MAX_SIZE = 1; private Lock lock = new ReentrantLock(); private Condition takeCondition = lock.newCondition(); private Condition putCondition = lock.newCondition(); /**** * 使用Condition来进行控制多线程的锁的问题 * @return */ public Integer getCake1(){ Integer result = null; lock.lock(); try{ while(list.isEmpty()){ try { System.out.println("take wait..."); takeCondition.await(); } catch (InterruptedException e) { e.printStackTrace(); } } result = list.remove(0); System.out.println("get..."+result); putCondition.signal(); }finally { lock.unlock(); } return result; } /**** * 使用Condition来进行控制多线程的锁的问题 * @return */ public void putCake1(Integer integer){ lock.lock(); try{ while(list.size()>=MAX_SIZE){ try { System.out.println("put wait..."); putCondition.await(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("add..."+integer); list.add(integer); takeCondition.signal(); }finally { lock.unlock(); } } /***普通方式**/ public synchronized Integer getCake(){ while(list.isEmpty()){ try { System.out.println("take wait..."); wait(); } catch (InterruptedException e) { e.printStackTrace(); } } Integer integer = list.remove(0); System.out.println("get..."+integer); notifyAll(); return integer; } public synchronized void putCake(Integer integer){ while(list.size()>=MAX_SIZE){ try { System.out.println("put wait..."); wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("add..."+integer); list.add(integer); notifyAll(); }}
2、取资源
package cn.toltech.t_concurrent;import java.util.List;/** * Created by sz0816 on 15-1-21. */public class GetCake implements Runnable{ public CakeBox cakeBox; public GetCake(CakeBox cakeBox){ this.cakeBox = cakeBox; } public Integer getCake(){ return cakeBox.getCake1(); } @Override public void run() { while(true){ getCake(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } }}
3、放资源
package cn.toltech.t_concurrent;/** * Created by sz0816 on 15-1-21. */public class PutCake implements Runnable{ private CakeBox cakeBox; private static int cakeNum; public PutCake(CakeBox cakeBox){ this.cakeBox = cakeBox; } public void putCake(){ cakeBox.putCake1(cakeNum++); } @Override public void run() { while(true){ putCake(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }}
4、测试主程序
package cn.toltech.t_concurrent;/** * Created by sz0816 on 15-1-21. */public class TestReentrantLock { public static void main(String []args) throws Exception{ CakeBox cakeBox = new CakeBox(); new Thread(new GetCake(cakeBox)).start(); new Thread(new PutCake(cakeBox)).start(); new Thread(new PutCake(cakeBox)).start(); while(true){ Thread.sleep(1000); } }}
0 0
- 多线程中使用Lock和Condition机制
- 多线程 : 使用Lock 和 Condition 实现线程间互斥与通信
- java多线程-07-Lock和Condition
- java多线程通信之lock和Condition
- 多线程(4)- Lock和Condition
- 多线程(6)等待唤醒机制Lock,condition
- JDK1.5中LOCK,Condition的使用
- Python的Lock和condition使用
- Lock和Condition对象的使用
- JDK1.5之后怎么玩转多线程(Lock和Condition的使用)
- JAVA多线程顺序执行(使用join,lock,condition,信号量)原理和java源代码
- java多线程(3):Lock接口和Condition监视器接口使用详解
- JAVA多线程-Lock的使用(一)-ReentrantLock与Condition
- Java多线程编程4--Lock的使用--重入锁(ReentrantLock)、Condition
- Java多线程 之 lock与condition的使用(十四)
- java多线程-Lock与Condition接口的使用详解
- Lock的Condition使用
- Java5 多线程(三)--Lock和Condition实现线程同步通信
- 责任链模式
- poj3207 2-SAT
- 关于Cocos引擎 V1.0 Preview版本 编译到安卓的几个坑
- 模块管理常规功能自定义系统的设计与实现(56--开源开发测试版发布 )
- 开源 java CMS - FreeCMS2.2 栏目管理
- 多线程中使用Lock和Condition机制
- Ext.DomHelper的用法
- 关于js跨域问题解决
- Jquery ui css framework
- SNMP4J编程最重要的三个类之Target
- 求二进制数中1的个数(编程之美)
- jstl中的<c:forEach
- Android:延迟执行PostDelayed
- Android性能优化典范