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Z7DgECav3IcsO6YeOB5KPld6Yb4oOITJ6wsnf6wjLTi89Hz8LCo4hMlLCI6M/vR5a3gtxLTgfB0c6zl1JgkOYfJswfGR/N5h1IzgcHB1G0wSHMLkxSLy1+HppI2bEpz1RJNPPE4DdeCvBHL93XY/8pWBJ75VWVd+X8EhTB7XEwYWIvN5QUiybOozldNlOQ3UI15H7yvkO35djJgmvJYZnyNoJzOb4LYeJo/K6B885LUsG32mceqxGf2Dekh/KPqRFOQIn+M0SVZwCJPH9eTnePuQEmfqczc2Iw3UhW+W/J2c0tKCG+9znCbJCg5h8rieMLASiew3Ktv7HPp29cTYOQ3Uxa0BwuTZ0N2oe6JE38OC0/prX26JMHluf278ssjpsqz8zGV4YQi3iPiV8qB0AlHrwELq/K3nY+K6XcdQLvY52SaTFRyBMHnz4O+mLP4gdqssOF2KgmMQJm86bqva3k35u4NVBpw2gwWHMHkATGew4AAA04HgAKhIJ7gbN24AABbGCc7KAAAswwuXLl1qAAA1uNT8H+LjmdeWz8fGAAAAAElFTkSuQmCC" alt="" />
在 Condition 对象中,与 wait、notify 和 notifyAll 方法对应的分别是
                                       await、signal 和 signalAll。
Condition 实例实质上被绑定到一个锁上。
要为特定 Lock 实例获得 Condition 实例,请使用其 newCondition() 方法。

package com.wf.zhang.juc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; /*
* 生产者消费者案例:
*/
public class TestProductorAndConsumerForLock { public static void main(String[] args) {
Clerk clerk = new Clerk(); Productor pro = new Productor(clerk);
Consumer con = new Consumer(clerk); new Thread(pro, "生产者 A").start();
new Thread(con, "消费者 B").start(); // new Thread(pro, "生产者 C").start();
// new Thread(con, "消费者 D").start();
} } class Clerk {
private int product = 0; private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition(); // 进货
public void get() {
lock.lock(); try {
if (product >= 1) { // 为了避免虚假唤醒,应该总是使用在循环中。
System.out.println("产品已满!"); try {
condition.await();
} catch (InterruptedException e) {
} }
System.out.println(Thread.currentThread().getName() + " : "
+ ++product); condition.signalAll();
} finally {
lock.unlock();
} } // 卖货
public void sale() {
lock.lock(); try {
if (product <= 0) {
System.out.println("缺货!"); try {
condition.await();
} catch (InterruptedException e) {
}
} System.out.println(Thread.currentThread().getName() + " : "
+ --product); condition.signalAll(); } finally {
lock.unlock();
}
}
} // 生产者
class Productor implements Runnable { private Clerk clerk; public Productor(Clerk clerk) {
this.clerk = clerk;
} @Override
public void run() {
for (int i = 0; i < 20; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
} clerk.get();
}
}
} // 消费者
class Consumer implements Runnable { private Clerk clerk; public Consumer(Clerk clerk) {
this.clerk = clerk;
} @Override
public void run() {
for (int i = 0; i < 20; i++) {
clerk.sale();
}
} }

JUC-8-lock和Condition使用的更多相关文章

  1. 四、线程同步之Lock和Condition

    Lock同步锁 Lock 在jdk1.5  提供了Lock以便执行同步操作,和synchronized不同的是Lock提供了显示的方法获取锁和释放锁.Lock提供了以下几个方法,请求和释放锁: voi ...

  2. 线程高级应用-心得5-java5线程并发库中Lock和Condition实现线程同步通讯

    1.Lock相关知识介绍 好比我同时种了几块地的麦子,然后就等待收割.收割时,则是哪块先熟了,先收割哪块. 下面举一个面试题的例子来引出Lock缓存读写锁的案例,一个load()和get()方法返回值 ...

  3. 【Java线程】Lock、Condition

    http://www.infoq.com/cn/articles/java-memory-model-5  深入理解Java内存模型(五)——锁 http://www.ibm.com/develope ...

  4. 【Java线程】锁机制:synchronized、Lock、Condition

    http://www.infoq.com/cn/articles/java-memory-model-5  深入理解Java内存模型(五)——锁 http://www.ibm.com/develope ...

  5. 并发之lock的condition接口

    13.死磕Java并发-----J.U.C之Condition 12.Condition使用总结 11.Java并发编程系列之十七:Condition接口 === 13.死磕Java并发-----J. ...

  6. Lock和Condition在JDK中LinkedBlockingQueue的应用

    Lock和Condition在JDK中LinkedBlockingQueue的应用,核心源码注释解析如下: import java.util.concurrent.LinkedBlockingQueu ...

  7. java并发编程系列三、Lock和Condition

    有了synchronized为什么还要Lock? 因为Lock和synchronized比较有如下优点 1. 尝试非阻塞地获取锁 2. 获取锁的过程可以被中断 3. 超时获取锁 Lock的标准用法 p ...

  8. 多线程系列三:Lock和Condition

    有了synchronized为什么还要Lock? 因为Lock和synchronized比较有如下优点 1. 尝试非阻塞地获取锁 2. 获取锁的过程可以被中断 3. 超时获取锁 Lock的标准用法 p ...

  9. Lock和Condition

    1 什么是可重入锁 可重入锁是说一个线程在已经获取了该锁的情况下,还可以再次获取该锁. 主要的应用场景: 可重入锁指的是在一个线程中可以多次获取同一把锁,比如:一个线程在执行一个带锁的方法,该方法中又 ...

  10. 【Java线程】锁机制:synchronized、Lock、Condition(转)

    原文地址 1.synchronized 把代码块声明为 synchronized,有两个重要后果,通常是指该代码具有 原子性(atomicity)和 可见性(visibility). 1.1 原子性 ...

随机推荐

  1. bbbbbb

    Blazor 机制初探以及什么是前后端分离,还不赶紧上车? 标签: Blazor .Net 上一篇文章我发了一个 BlazAdmin 的尝鲜版,这一次主要聊聊 Blazor 是如何做到用 C# 来写前 ...

  2. 【UR #6】懒癌

    Problem Description 你绞尽脑汁也没有解开智商锁给的迷题,只见哐地一下门就开了:"您与锁的主人智商一致." 于是你们窃取了大量内部资料,最后端掉了 \(IIIS\ ...

  3. 【AGC028D】Chord

    Problem Description 给定一个圆,圆上均等地放着 \(2n\) 个点,已有 \(k\) 对点之间连好了边,从中选择剩下 \(n-k\) 对点随意连边. 求所有连边方案中,联通块的个数 ...

  4. 每天3分钟操作系统修炼秘籍(6):Idle进程

    点我查看秘籍连载 CPU的归属:Idle进程 操作系统并不总是繁忙.例如个人PC上任务比较轻,多数时候都无法充分利用CPU,导致CPU处于空闲状态.但CPU既然通电了,它就得运行,那么在它没有任务需要 ...

  5. SSH框架之Spring第四篇

    1.1 JdbcTemplate概述 : 它是spring框架中提供的一个对象,是对原始JdbcAPI对象的简单封装.spring框架为我们提供了很多的操作模板类. ORM持久化技术 模板类 JDBC ...

  6. node error SOCKET error:10106

    上周我的node.js command prompt出错了,什么也干不了 SOCKET error:10106 纠结两天,终于搞定了,其实比较简单,就是不会弄起来好麻烦 参考: 作者:忆常  url: ...

  7. HTML 表单模板

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. C lang:Protect array data——Const

    Xx_Introduction Use pointer translate parameter array original data will change data,and use const p ...

  9. 2、nio的例子实践

    下面的例子,说明了,nio中的三大核心类的基本使用.buffer,channel,selector package com.shengsiyuan.nio; import org.junit.Test ...

  10. Oracle - SPM固定执行计划

    1. 通过dbms_xplan.display_cursor查看指定sql都有哪些执行计划 SQL> select * from table(dbms_xplan.display_cursor( ...