全屏

java.util.concurrent.locks.Condition接口提供一个线程挂起执行的能力,直到给定的条件为真。 Condition对象必须绑定到Lock,并使用newCondition()方法获取对象。

Condition类的方法

以下是Condition类中可用的重要方法的列表。序号方法名称描述1public void await()使当前线程等待,直到发出信号或中断信号。

2public boolean await(long time, TimeUnit unit)使当前线程等待直到发出信号或中断,或指定的等待时间过去。

3public long awaitNanos(long nanosTimeout)使当前线程等待直到发出信号或中断,或指定的等待时间过去。

4public long awaitUninterruptibly()使当前线程等待直到发出信号。

5public long awaitUntil()使当前线程等待直到发出信号或中断,或者指定的最后期限过去。

6public void signal()唤醒一个等待线程。

7public void signalAll()唤醒所有等待线程。

实例

以下TestThread程序演示了Condition接口的这些方法。这里我们使用signal()通知和await()挂起线程。import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

public class TestThread {

public static void main(String[] args) throws InterruptedException{

ItemQueue itemQueue = new ItemQueue(10);

//Create a producer and a consumer.

Thread producer = new Producer(itemQueue);

Thread consumer = new Consumer(itemQueue);

//Start both threads.

producer.start();

consumer.start();

//Wait for both threads to terminate.

producer.join();

consumer.join();

}

static class ItemQueue {

private Object[] items = null;

private int current = 0;

private int placeIndex = 0;

private int removeIndex = 0;

private final Lock lock;

private final Condition isEmpty;

private final Condition isFull;

public ItemQueue(int capacity) {

this.items = new Object[capacity];

lock = new ReentrantLock();

isEmpty = lock.newCondition();

isFull = lock.newCondition();

}

public void add(Object item) throws InterruptedException {

lock.lock();

while(current >= items.length)

isFull.await();

items[placeIndex] = item;

placeIndex = (placeIndex + 1) % items.length;

++current;

//Notify the consumer that there is data available.

isEmpty.signal();

lock.unlock();

}

public Object remove() throws InterruptedException {

Object item = null;

lock.lock();

while(current <= 0){

isEmpty.await();

}

item = items[removeIndex];

removeIndex = (removeIndex + 1) % items.length;

--current;

//Notify the producer that there is space available.

isFull.signal();

lock.unlock();

return item;

}

public boolean isEmpty(){

return (items.length == 0);

}

}

static class Producer extends Thread {

private final ItemQueue queue;

public Producer(ItemQueue queue) {

this.queue = queue;

}

@Override

public void run() {

String[] numbers = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};

try {

for(String number: numbers){

queue.add(number);

System.out.println("[Producer]: " + number);

}

queue.add(null);

}

catch (InterruptedException ex) {

ex.printStackTrace();

}

}

}

static class Consumer extends Thread {

private final ItemQueue queue;

public Consumer(ItemQueue queue) {

this.queue = queue;

}

@Override

public void run() {

try {

do {

Object number = queue.remove();

System.out.println("[Consumer]: " + number);

if(number == null){

return;

}

} while(!queue.isEmpty());

}

catch (InterruptedException ex) {

ex.printStackTrace();

}

}

}

}

这将产生以下结果。[Producer]: 1

[Consumer]: 1

[Producer]: 2

[Consumer]: 2

[Producer]: 3

[Consumer]: 3

[Producer]: 4

[Consumer]: 4

[Producer]: 5

[Producer]: 6

[Consumer]: 5

[Producer]: 7

[Consumer]: 6

[Consumer]: 7

[Producer]: 8

[Consumer]: 8

[Producer]: 9

[Consumer]: 9

[Producer]: 10

[Consumer]: 10

[Producer]: 11

[Consumer]: 11

[Producer]: 12

[Consumer]: 12

[Consumer]: null

分享到:

0评论

newcondition java_Java并发Condition接口相关推荐

  1. Java并发编程——详解AQS对Condition接口的具体实现

    目录 一.等待/通知机制与Condition接口 1.1 等待/通知机制 1.2 Condition接口 二.AQS的具体实现 2.1 ConditionObject 2.2 等待机制 2.3 通知机 ...

  2. Condition接口详解

    一.condition和对象监视器 Condition是对象监视器的替代品,拓展了监视器的语义. 相同: 都有一组类似的方法: 对象监视器: Object.wait().Object.wait(lon ...

  3. Condition 接口

    2019独角兽企业重金招聘Python工程师标准>>> Condition 接口 任意一个java对象,都拥有一组监视器方法,主要包括 wait(),wait(long timeou ...

  4. java condition原理_java中Condition接口原理及实现

    Condition是在java 1.5中才出现的,它用来替代传统的Object的wait().notify()实现线程间的协作,相比Object的wait().notify(),使用Condition ...

  5. 多线程笔记补充之线程通信wait和notify方法以及Lock和Condition接口的使用

    线程通信-wait和notify方法介绍: java.lang.Object类提供类两类用于操作线程通信的方法. wait():执行该方法的线程对象释放同步锁,JVM把该线程存放到等待池中,等待其他的 ...

  6. 条件队列java_Java并发系列(4)AbstractQueuedSynchronizer源码分析之条件队列

    AbstractQueuedSynchronizer内部维护了一个同步状态和两个排队区,这两个排队区分别是同步队列和条件队列. 我们还是拿公共厕所做比喻,同步队列是主要的排队区,如果公共厕所没开放,所 ...

  7. @ConditionalOnBean、@ConditionalOnProperty、@ConditionalOnClass、@Conditional和Condition接口的使用

    一.@ConditionalOnBean.@ConditionalOnProperty.@ConditionalOnClass @ConditionalOnBean是指当spring容器中有某个bea ...

  8. 拍卖源码java_Java并发的AQS原理详解

    原文:https://juejin.im/post/5c11d6376fb9a049e82b6253 每一个 Java 的高级程序员在体验过多线程程序开发之后,都需要问自己一个问题,Java 内置的锁 ...

  9. 二、多并发实现接口压力测试

    一.flsak接口压力测试 import base64 import logging import os, cv2,time import urllib, glob import numpy as n ...

最新文章

  1. easy-mock写的一个简单的模拟二页的反馈
  2. SMW0 HTML模版的形式上传文件 维护MIME类型
  3. Python基础教程:括号()[]{}详解
  4. linux mysql 密码文件怎么打开文件,Oracle数据库密码文件创建与使用
  5. 积分梯度:一种新颖的神经网络可视化方法
  6. CSS Hack(Safari、Opera、Chrome、IE6、IE7、 IE8和IE9)
  7. 2021年下半年系统集成项目管理工程师案例分析真题及答案解析
  8. resnet,inception,densenet,senet
  9. 十分钟弄懂最快的APP自动化工具uiautomator2
  10. 未来五年最具前景的一门编程语言是什么?
  11. 泰安的雾霾确实有点大
  12. Fiddler 介绍二
  13. win10打开蓝牙_win10动态锁,只要你离电脑远一点,电脑就自动锁定
  14. SQL零基础入门必知必会!
  15. IntelliJ Idea 解决 Could not autowire. No beans of 'xxxx' type found 的错误提示
  16. [线性代数] 1.3 n阶行列式
  17. WPF遍历视觉树与逻辑树
  18. 服务器光盘的正确使用方法,使用光盘刻录服务器,自动备份数据到光盘(成功案例)...
  19. 去香港读研——申请全过程
  20. 这几款真香旗舰机,买到就是赚到,有你入手了的吗?

热门文章

  1. 前端学习(2793):完成联系我们页面和地图
  2. 前端学习(2631):git安装
  3. 前端学习(2528):一个简单的vue app
  4. 前端学习(2026)vue之电商管理系统电商系统之实现分页功能
  5. 前端学习(1951)vue之电商管理系统电商系统之获取父级数据列表
  6. 前端学习(981):jquery入门
  7. spring学习(5):spring简介
  8. 第二十五期:5G预约用户超千万!是“虚火”还是“真旺”?
  9. html:(19):单选框,复选框,下拉列表框
  10. 实例28:python