随笔 - 1162  文章 - 0  评论 - 16  阅读 - 59万 

一、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();

    }
}
复制代码

 

posted on   格物致知_Tony  阅读(58)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
历史上的今天:
2021-01-18 Java 异常处理:总结
2021-01-18 Java 异常处理:自定义异常
2021-01-18 Java 异常处理 之 手动抛出异常
2021-01-18 Java 异常处理 之 异常处理机制二:throws + 异常类型
2021-01-18 Java 异常处理 之 异常处理机制一:try-catch-finally
2021-01-18 Java 异常处理 之 异常概述与异常体系结构
2020-01-18 Java 之 泛型的其他知识
点击右上角即可分享
微信分享提示

目录导航