concurrent包中condition的使用

本文通过一个具体的示例展示了如何利用Condition来改善多线程环境下的数据处理效率与准确性。通过引入Condition,使得线程能够在特定条件下等待或唤醒,从而实现更精细的线程间同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先回忆上一篇关于读写锁的知识。

首先先看一个例子:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by lizhiqiang on 2016/12/22.
 */
public class testCondition {
    public static void main(String[] args){
        final BoundedBuffer buffer = new BoundedBuffer();
        ExecutorService service = Executors.newFixedThreadPool(10);
        service.execute(new Runnable() {
            @Override
            public void run() {
                buffer.put(1);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("1"+buffer.take());
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                buffer.put(2);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("2"+buffer.take());
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                buffer.put(3);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("3"+buffer.take());
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                buffer.put(4);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("4"+buffer.take());
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                buffer.put(5);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("5"+buffer.take());
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                buffer.put(6);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("6"+buffer.take());
            }
        });
        service.shutdown();
    }
    public static class BoundedBuffer{
        final Object[] items = new Object[100];
        int count;
        public BoundedBuffer(){
            count=0;
        }
        public void put(Object x){
            items[++count] = x;
        }
        public Object take(){
            return items[count--];
        }
    }
}

这个例子实际上是从开启6个线程,分别对一个数组进行写读操作,执行结果:
24
16
43
53
65
31
发现写入的数据被其他线程改了,说明每个线程的操作并不是原子操作,需要加锁。
改后的代码为:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created by lizhiqiang on 2016/12/22.
 */
public class testCondition {
    public static void main(String[] args){
        final BoundedBuffer buffer = new BoundedBuffer();
        ExecutorService service = Executors.newFixedThreadPool(10);
        final ReadWriteLock lock = new ReentrantReadWriteLock();
        service.execute(new Runnable() {
            @Override
            public void run() {
                lock.writeLock().lock();
                buffer.put(1);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("1"+buffer.take());
                lock.writeLock().unlock();
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                lock.writeLock().lock();
                buffer.put(2);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("2"+buffer.take());
                lock.writeLock().unlock();
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                lock.writeLock().lock();
                buffer.put(3);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("3"+buffer.take());
                lock.writeLock().unlock();
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                lock.writeLock().lock();
                buffer.put(4);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("4"+buffer.take());
                lock.writeLock().unlock();
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                lock.writeLock().lock();
                buffer.put(5);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("5"+buffer.take());
                lock.writeLock().unlock();
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                lock.writeLock().lock();
                buffer.put(6);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("6"+buffer.take());
                lock.writeLock().unlock();
            }
        });
        service.shutdown();
    }
    public static class BoundedBuffer{
        final Object[] items = new Object[100];
        int count;
        public BoundedBuffer(){
            count=0;
        }
        public void put(Object x){
            items[++count] = x;
        }
        public Object take(){
            return items[count--];
        }
    }
}
执行结果为:
11
22
33
44
55
66
当然它们的输出顺序可能是不一样的,但是写入和读是对应的了。
以上部分是回忆上一篇关于读写锁的知识。
================================================================================================
下面开始Condition的内容
真实应用场景:传入队列任务,监听器从队列中获取任务。
但是如果我们take的时候,发现count为0怎么办?如果我们put的时候,如果count达到上限怎么办?
过去的做法经常过一会再次尝试,这种方式的实时性不是特别高,如何做到精准的实时性呢?
首先是想加一个通知的操作,即:如果take的时候发现count为0,则将线程进入等待。另一个线程put的时候,则唤醒之前的线程,让线程继续执行。
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.*;

/**
 * Created by lizhiqiang on 2016/12/22.
 */
public class testCondition {
    public static void main(String[] args){
        final BoundedBuffer buffer = new BoundedBuffer();
        ExecutorService service = Executors.newFixedThreadPool(2);
        service.execute(new Runnable() {
            @Override
            public void run() {
                while(true){
                    try {
                        Thread.currentThread().sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    buffer.put(1);
                }
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    System.out.println(new Date() + ":" + buffer.take());
                }
            }
        });
        service.shutdown();
    }
    public static class BoundedBuffer{
        final Lock lock = new ReentrantLock();

        Condition notEmpty = lock.newCondition();
        Condition notFull = lock.newCondition();
        final Object[] items = new Object[3];
        int count;
        public BoundedBuffer(){
            count=0;
        }
        public void put(Object x){
            lock.lock();
            if(count==2){
                try {
                    notFull.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            ++count;
            items[count] = x;
            notEmpty.signal();
            lock.unlock();
        }
        public Object take(){
            lock.lock();
            Object result;
            if(count==0) {
                try {
                    notEmpty.await();

                } catch (InterruptedException e) {
                }
            }
            result = items[count--];
            notFull.signal();
            lock.unlock();
            return result;
        }
    }
}

测试结果为:

Thu Dec 22 16:41:10 CST 2016:1
Thu Dec 22 16:41:11 CST 2016:1
Thu Dec 22 16:41:12 CST 2016:1
Thu Dec 22 16:41:13 CST 2016:1
Thu Dec 22 16:41:14 CST 2016:1
Thu Dec 22 16:41:15 CST 2016:1
Thu Dec 22 16:41:16 CST 2016:1
Thu Dec 22 16:41:17 CST 2016:1
Thu Dec 22 16:41:18 CST 2016:1
Thu Dec 22 16:41:19 CST 2016:1
Thu Dec 22 16:41:20 CST 2016:1
Thu Dec 22 16:41:21 CST 2016:1
Thu Dec 22 16:41:22 CST 2016:1
Thu Dec 22 16:41:23 CST 2016:1
Thu Dec 22 16:41:24 CST 2016:1


资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值