//Listing 7-2. Achieving Synchronization in Terms of Locks and Conditions
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; public class A {
public static void main(String[] args) {
Shared s = new Shared();
new Producer(s).start();
new Consumer(s).start();
}
} class Shared {
private char c;
private volatile boolean available;
private final Lock lock;
private final Condition condition; Shared() {
available = false;
lock = new ReentrantLock();
condition = lock.newCondition();
} Lock getLock() {
return lock;
} char getSharedChar() {
lock.lock();
try {
while (!available)
try {
condition.await();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
available = false;
condition.signal();
} finally {
lock.unlock();
return c;
}
} void setSharedChar(char c) {
lock.lock();
try {
while (available)
try {
condition.await();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
this.c = c;
available = true;
condition.signal();
} finally {
lock.unlock();
}
}
} class Producer extends Thread {
private final Lock l;
private final Shared s; Producer(Shared s) {
this.s = s;
l = s.getLock();
} @Override
public void run() {
for (char ch = 'A'; ch <= 'Z'; ch++) {
l.lock();
s.setSharedChar(ch);
System.out.println(ch + " produced by producer.");
l.unlock();
}
}
} class Consumer extends Thread {
private final Lock l;
private final Shared s; Consumer(Shared s) {
this.s = s;
l = s.getLock();
} @Override
public void run() {
char ch;
do {
l.lock();
ch = s.getSharedChar();
System.out.println(ch + " consumed by consumer.");
l.unlock();
} while (ch != 'Z');
}
}

java Conditions的更多相关文章

  1. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  2. Effective Java 57 Use exceptions only for exceptional conditions

    Principle Exceptions are, as their name implies, to be used only for exceptional conditions; they sh ...

  3. Effective Java 58 Use checked exceptions for recoverable conditions and runtime exceptions for programming errors

    Three kinds of throwables Throwables Checked Recoverable checked exceptions Y Y runtime exceptions N ...

  4. 20、Java并发性和多线程-Slipped Conditions

    以下内容转自http://ifeve.com/slipped-conditions/: 所谓Slipped conditions,就是说, 从一个线程检查某一特定条件到该线程操作此条件期间,这个条件已 ...

  5. java 锁4

    关于锁的分类 及 锁的灵活使用: 参见 http://blog.csdn.net/qaz13177_58_/article/details/21543515  有几句话说得相当不错: 锁的分类 : 同 ...

  6. Java多线程系列--“JUC锁”03之 公平锁(一)

    概要 本章对“公平锁”的获取锁机制进行介绍(本文的公平锁指的是互斥锁的公平锁),内容包括:基本概念ReentrantLock数据结构参考代码获取公平锁(基于JDK1.7.0_40)一. tryAcqu ...

  7. Java多线程系列--“JUC锁”04之 公平锁(二)

    概要 前面一章,我们学习了“公平锁”获取锁的详细流程:这里,我们再来看看“公平锁”释放锁的过程.内容包括:参考代码释放公平锁(基于JDK1.7.0_40) “公平锁”的获取过程请参考“Java多线程系 ...

  8. (四)Spark集群搭建-Java&Python版Spark

    Spark集群搭建 视频教程 1.优酷 2.YouTube 安装scala环境 下载地址http://www.scala-lang.org/download/ 上传scala-2.10.5.tgz到m ...

  9. JMM(java内存模型)

    What is a memory model, anyway? In multiprocessorsystems, processors generally have one or more laye ...

随机推荐

  1. View、ViewGroup (转)

    Android原理揭秘系列之View.ViewGroup (转) Android的UI界面都是由View和ViewGroup及其派生类组合而成的.其中,View是所有UI组件的基类,而ViewGrou ...

  2. Windows 7下搭建Jmeter测试环境

    jmeter配置.安装 一. 工具描述 apache jmeter是100%的java桌面应用程序,它被设计用来加载被测试软件功能特性.度量被测试软件的性能.设计jmeter的初衷是测试web应用,后 ...

  3. JDK的命令行工具

    Jcmd:综合工具 jcmd -l  列出当前运行的所有虚拟机 参数-l表示列出所有java虚拟机,针对每一个虚拟机,可以使用help命令列出该虚拟机支持的所有命令 jcmd [pid] help j ...

  4. Bean的前身今世&处理器&Aware

    出生: <bean>......</bean> 幼年:BeanDefinition  bean的接口定义 小学:GenericBeanDefinition  基础的bean映射 ...

  5. VMware Tools安装

    不是每一个程序员都必须玩过linux,只是博主觉得现在的很多服务器都是linux系统的,而自己属于那种前端也搞,后台也搞,对框架搭建也感兴趣,但是很多生产上的框架和工具都是安装在服务器上的,而且有不少 ...

  6. JVM的生命周期

    package test; public class JVMTestLife { public static void main(String[] args) { new Thread(new Run ...

  7. Ajax 学习之获取服务器的值

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  8. js:数据结构笔记4--队列

    队列是一种特殊的列表,数据结构为FIFO: 定义: function Queue() { this.dataStore = []; this.enqueue = enqueue; this.deque ...

  9. 用iftop监控流量

    1.下载iftop http://pan.baidu.com/share/link?shareid=387372457&uk=1829018343 或 wget http://www.ex-p ...

  10. Codeforces Round #190 DIV.2 A. Ciel and Dancing

    #include <cstdio> #include <iostream> #include <vector> using namespace std; int m ...