zoukankan      html  css  js  c++  java
  • 第三章:(2)线程间通信—Lock 实现线程通信

    一、Lock 接口相关 API

      Java 8 之前使用 synchronized,之后用 lock 锁。
      

     

    二、Lock 接口实现线程通信

      要求:有两个线程,实现对一个初始值是 0 的变量进行操作,一个线程对当前数值加 1,另一个线程对当前数值减 1,这两个线程交替完成效果,要求用线程间通信。

      实现:

    //第一步 创建资源类,定义属性和操作方法
    class Share {
        private int number = 0;
    
        //创建 Lock
        private Lock lock = new ReentrantLock();
    
        private Condition condition = lock.newCondition();
    
        //+1
        public void incr() throws InterruptedException {
            //上锁
            lock.lock();
            try {
                //1. 判断
                while (number != 0) {
                    condition.await();
                }
    
                //2. 干活
                number++;
                System.out.println(Thread.currentThread().getName() + "::" + number);
    
                //3. 通知
                condition.signalAll();
    
            } finally {
                //解锁
                lock.unlock();
            }
        }
    
        //-1
        public void decr() throws InterruptedException {
            //上锁
            lock.lock();
            try {
                //1. 判断
                while (number != 1) {
                    condition.await();
                }
    
                //2. 干活
                number--;
                System.out.println(Thread.currentThread().getName() + "::" + number);
    
                //3. 通知
                condition.signalAll();
    
            } finally {
                //解锁
                lock.unlock();
            }
    
        }
    }
    
    public class ThreadDemo2 {
    
        public static void main(String[] args) {
            //第三步  创建多个线程,调用资源类的操作方法;
            Share share = new Share();
    
            //创建线程
            new Thread(() -> {
                for (int i = 0; i < 10; i++) {
                    try {
                        share.incr();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "AA").start();
    
            new Thread(() -> {
                for (int i = 0; i < 10; i++) {
                    try {
                        share.decr();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "BB").start();
    
            new Thread(() -> {
                for (int i = 0; i < 10; i++) {
                    try {
                        share.incr();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "CC").start();
    
            new Thread(() -> {
                for (int i = 0; i < 10; i++) {
                    try {
                        share.decr();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "DD").start();
    
        }
    }
  • 相关阅读:
    零基础学python-4.5 标准类型分类
    零基础学python-4.4 常用的一些内建函数
    零基础学python-4.3 对象的比较
    零基础学python-4.2 其他内建类型
    零基础学python-4.1 python对象的简介和标准类型
    7、postman中的Newman插件(可生成html测试报告)
    6、postman cookie和token使用
    5、postman认证方式简单了解
    4、postman动态参数传递(含token详细使用)
    3、postman中tests断言使用
  • 原文地址:https://www.cnblogs.com/niujifei/p/15820211.html
Copyright © 2011-2022 走看看