lock--unlock的方式在实际中使用较少,一般使用synchronized获取对象的内部锁替代,但是lock--unlock对了解synchronized有很大的帮助。

创建一个bank对象用于模拟动作

package reentrant_lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; /**
* Created by luozhitao on 2017/8/18.
*/
public class Bank { private final double[] accounts;
private Lock bankLock;
private Condition newCondition; public Bank(int n,double intialBalance){ accounts=new double[n]; for(int i =;i<n;i++){
accounts[i]=intialBalance;
} bankLock=new ReentrantLock();
newCondition=bankLock.newCondition(); } //
public void tansfer(int from,int to,double account_m) throws InterruptedException{ bankLock.lock();
int aa=; try{ while (accounts[from]<account_m) {
System.out.println(Thread.currentThread()+"被阻塞");
aa++;
newCondition.await();
} if(aa>) {
System.out.println(Thread.currentThread()+"从阻塞状态被唤醒");
aa=; } accounts[from]-=account_m; System.out.printf("%10.2f from %d to %d",account_m,from,to); accounts[to]+=account_m;
System.out.println("-----");
System.out.printf("the total banlance %10.2f", getTotal()); System.out.println("释放transfer锁之前");
//newCondition.notifyAll();
newCondition.signalAll();
System.out.println("释放transfer锁之后"); } finally { bankLock.unlock(); } } //
public double getTotal(){ bankLock.lock();
try{
double sum=;
for(double a:accounts){ sum+=a;
} return sum; }finally {
bankLock.unlock(); } } // public int size(){ return accounts.length;
}
}

创建线程

package reentrant_lock;

/**
* Created by luozhitao on 2017/8/18.
*/
public class transferRunnable implements Runnable { private Bank bank;
private int fromAccount;
private double maxAccount;
private int DELAY=;
int flag=; public transferRunnable(Bank b,int from,double max){
this.bank=b;
this.fromAccount=from;
this.maxAccount=max; } public void run() { try{
while (true){
int toAccount=(int)((bank.size()-)*Math.random()); System.out.println("toAccount ="+toAccount);
double account_m=maxAccount*Math.random();
System.out.println("account_m is "+account_m);
bank.tansfer(fromAccount,toAccount,account_m); Thread.sleep((int) (DELAY * Math.random()));
flag++; }
}catch (InterruptedException e){e.printStackTrace();} }
}

main

package reentrant_lock;

/**
* Created by luozhitao on 2017/8/18.
*/
public class Banktest { private static final int Naccount=;
private static final double inital_balance=; public static void main(String [] args){ Bank b=new Bank(Naccount,inital_balance); for(int i=;i<;i++){ transferRunnable t=new transferRunnable(b,i,inital_balance); Thread thread=new Thread(t);
thread.start(); } } }

java 并发时使用条件变量--Condition的更多相关文章

  1. java并发多线程显式锁Condition条件简介分析与监视器 多线程下篇(四)

    Lock接口提供了方法Condition newCondition();用于获取对应锁的条件,可以在这个条件对象上调用监视器方法 可以理解为,原本借助于synchronized关键字以及锁对象,配备了 ...

  2. python线程条件变量Condition(31)

    对于线程与线程之间的交互我们在前面的文章已经介绍了 python 互斥锁Lock / python事件Event , 今天继续介绍一种线程交互方式 – 线程条件变量Condition. 一.线程条件变 ...

  3. Java并发(十一):Condition条件

    先做总结: 1.为什么使用Condition条件? synchronized配合Object的wait().notify()系列方法可以实现等待/通知模式. Lock提供了条件Condition,对线 ...

  4. c++并发编程之条件变量(Condition Variable)

    条件变量(Condition Variable)的一般用法是:线程 A 等待某个条件并挂起,直到线程 B 设置了这个条件,并通知条件变量,然后线程 A 被唤醒.经典的「生产者-消费者」问题就可以用条件 ...

  5. [development][C] 条件变量(condition variables)的应用场景是什么

    产生这个问题的起因是这样的: ‎[:] ‎<‎tong‎>‎ lilydjwg: 主线程要启动N个子线程, 一个局部变量作为把同样的参数传入每一个子线程. 子线程在开始的十行会处理完参数. ...

  6. java多线程技术之条件变量

    上一篇讲述了并发包下的Lock,Lock可以更好的解决线程同步问题,使之更面向对象,并且ReadWriteLock在处理同步时更强大,那么同样,线程间仅仅互斥是不够的,还需要通信,本篇的内容是基于上篇 ...

  7. 深入解析条件变量(condition variables)

    深入解析条件变量 什么是条件变量(condition variables) 引用APUE中的一句话: Condition variables are another synchronization m ...

  8. Linux组件封装(二)中条件变量Condition的封装

    条件变量主要用于实现线程之间的协作关系. pthread_cond_t常用的操作有: int pthread_cond_init(pthread_cond_t *cond, pthread_conda ...

  9. python线程的条件变量Condition的用法实例

      Condition 对象就是条件变量,它总是与某种锁相关联,可以是外部传入的锁或是系统默认创建的锁.当几个条件变量共享一个锁时,你就应该自己传入一个锁.这个锁不需要你操心,Condition 类会 ...

随机推荐

  1. Centos服务器被挂马的一次抓马经历

    转载:http://blog.csdn.net/qq_21439971/article/details/54631440 今天早上五点,收到监控宝的警告短信,说是网站M无法访问了.睡的正香,再说网站所 ...

  2. mysql外键理解

    一个班级的学生个人信息表: 什么是外键 在设计的时候,就给表1加入一个外键,这个外键就是表2中的学号字段,那么这样表1就是主表,表2就是子表. 外键用来干什么 为了一张表记录的数据不要太过冗余. 这和 ...

  3. MVVM中轻松实现Command绑定任意事件的Command

    下面是“银光中国”(不错的WPF或SL网站)WPF学习教程中的一个连接, http://www.silverlightchina.net/html/study/WPF/2011/0715/9034.h ...

  4. vim 的寄存器

    If you've been following my series on Vim, it should be clear now that Vim has a pretty clear philos ...

  5. spoj-ASSIGN-bitDP

    ASSIGN - Assignments #dynamic-programming Problem Your task will be to calculate number of different ...

  6. C#/JAVA 程序员转GO/GOLANG程序员笔记大全(DAY 04)

    -------------------- interface 接口 // 定义: type IHumaner interface { SayHi() // 接口中只能是方法声明,没有实现,没有数据字段 ...

  7. MySQL相关错误汇总

    Eroor 1 描述: 在启动mysql的时候出现如下问题:"ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' ...

  8. 【Raspberry Pi】 小问题汇总

    注: 此系列为自己之前所搭建网站内容. 目前入手树莓派2,将遇到的一些琐碎的问题记录在此. 1. 更改时区 查看日期命令:date 输入sudo dpkg-reconfigure tzdata后按提示 ...

  9. NPOI:初次操作(新建Excel)

    1. 由于在某些电脑上没有安装office,或者有权限限制,使用COM组件进行读写Excel的话会出现问题, 为此,NPOI是一个很好的选择,NPOI可以在上述环境中满足Office的操作需求,并且功 ...

  10. 同一主机,不同域名绑定不同网站(IIS主机头实现方法)

    说明: 网站A域名:www.weba.com 网站A路径:D:\website\weba 网站B域名:www.webb.com 网站B路径:D:\website\webb 公用主机IP:111.222 ...