Java并发编程之Wait和Notify

简介: Java并发编程之Wait和Notify

@[toc]

Background

相关概念

什么是多线程

我们把组成程序(Program)各个部分称为线程(Thread)。也可以说,线程就是程序中轻量级的进程(Process)。

多线程(Multithreading)是Java的一个特性,它可以允许一个程序的多个部分(也就是线程)并发地执行,以达到最大程度利用CPU的目的。

Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such program is called a thread. So, threads are light-weight processes within a process.

-- https://www.geeksforgeeks.org/multithreading-in-java/

线程的状态

[外链图片转存中...(img-ZKyokcdB-1676541073994)]

轮询

Samples

我们把循环执行某个逻辑判断,直到判断条件为true才执行判断体中的逻辑,叫做轮询(Polling)。轮询是会浪费一定的CPU资源的。

The process of testing a condition repeatedly till it becomes true is known as polling.Polling is usually implemented with the help of loops to check whether a particular condition is true or not. If it is true, certain action is taken. This waste many CPU cycles and makes the implementation inefficient.

-- https://www.geeksforgeeks.org/inter-thread-communication-java/

下面提供一个轮询的实现示例。

Message:

isAvailable初始值是false,设置为true以后执行轮询体。

注意要使用线程安全的AtomicBoolean,如果使用boolean,在多线程情况下会有意想不到的结果。

import lombok.Getter;
import lombok.Setter;
import java.util.concurrent.atomic.AtomicBoolean;

@Setter
@Getter
public class Message {
    private AtomicBoolean isAvailable = new AtomicBoolean(false);
    private String msg;
    public Message(String str) {
        this.msg = str;
    }
}

PollingWaiter:


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class PollingWaiter implements Runnable {
    private Message msg;
    public PollingWaiter(Message m) {
        this.msg = m;
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        synchronized (msg) {
            int count = 0;
            System.out.println(name + " : waiter starting at time: " + LocalDateTime.now().format(DateTimeFormatter.ISO_TIME));
            while (!msg.getIsAvailable().get()) {
                count++;
            }
            System.out.println(name + " : msg is available at time: " + LocalDateTime.now().format(DateTimeFormatter.ISO_TIME));
            System.out.println(name + " : msg is available after count: " + count);
            System.out.println(name + " : processed: " + msg.getMsg());
        }
    }
}

执行测试:

休眠3秒以后,再执行轮询体内的代码。

import java.util.concurrent.atomic.AtomicBoolean;

public class WaitNotifyTest {

    public static void main(String[] args) {
        testPolling();
    }

    public static void testPolling() {

        Message msg = new Message("process it");

        PollingWaiter waiter = new PollingWaiter(msg);

        new Thread(waiter, "PollingWaiter").start();

        try {
            Thread.sleep(3000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        msg.setIsAvailable(new AtomicBoolean(true));
        System.out.println("over");
    }

}

输出结果:

PollingWaiter : waiter starting at time: 14:26:08.482
over
PollingWaiter : msg is available at time: 14:26:11.402
PollingWaiter : msg is available after count: -69547606
PollingWaiter : processed: process it

wait 和 notify

除了轮询,Java通过wait 和 notify机制实现了线程间的通信。wait就是让执有某个对象的线程处于等待阻塞状态,而notify就是让等待阻塞中的线程重新获得CPU资源,再次进入运行状态。

由于wait 和 notify相关的方法实现在了java.lang.Object类中,因此所有的子类都可以使用这些方法。

wait 和 notify相关的方法需要在synchronized代码块中执行。

wait 和 notify

方法介绍

下面简要介绍一下这些方法:

  • wait()

wait()方法会导致当前线程从执行状态改为待执行状态,一直到另外一个线程为当前对象执行notify()或者notifyAll()方法。

  • wait(long timeout)

与wait()方法的不同点是,如果timeout时间到了以后,还没有前对象执行notify()或者notifyAll(),则线程自动开始执行。

值得注意的是执行wait(0)和wait()的效果是一样的。

  • wait(long timeout, int nanos)

与wait(long timeout)相比,此方法提供了等待超时设置的更高的精度,精确到了纳秒。

1毫秒 = 1,000,000 纳秒。

  • notify()

对于等待此对象的监视器的所有线程,执行notify()会随机唤醒一个线程。

  • notifyAll()

相比与notify(),此方法会唤醒所有等待该对象的监视器的线程。

示例

在上面示例代码的基础上,增加如下代码实现。

Waiter:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Waiter implements Runnable{

    private Message msg;

    public Waiter(Message m){
        this.msg=m;
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        synchronized (msg) {
            try{
                System.out.println(name+" : waiting to get notified at time:"+ LocalDateTime.now().format(DateTimeFormatter.ISO_TIME));
                msg.wait();
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            System.out.println(name+" : waiter thread got notified at time:"+LocalDateTime.now().format(DateTimeFormatter.ISO_TIME));
            //process the message now
            System.out.println(name+" : processed: "+msg.getMsg());
        }
    }

}

Notifier:

public class Notifier implements Runnable {

    private boolean isAll = true;

    private Message msg;

    public Notifier(Message msg, boolean isAll) {
        this.msg = msg;
        this.isAll = isAll;
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        System.out.println(name + " started");
        try {

            Thread.sleep(3000);

            synchronized (msg) {

                System.out.println(name + " : got the msg : "+msg.getMsg());

                msg.setMsg(name + " : Notifier work done");

                if (isAll) {
                    msg.notifyAll();
                } else {
                    msg.notify();
                }

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}

WaitNotifyTest:

import java.util.concurrent.atomic.AtomicBoolean;

public class WaitNotifyTest {

    public static void main(String[] args) {
        //testPolling();
        testNotify();
        //testNotifyAll();
    }

    public static void testPolling() {

        Message msg = new Message("process it");

        PollingWaiter waiter = new PollingWaiter(msg);

        new Thread(waiter, "PollingWaiter").start();

        try {
            Thread.sleep(3000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        msg.setIsAvailable(new AtomicBoolean(true));
        System.out.println("over");
    }

    public static void testNotify() {
        Message msg = new Message("process it");

        Waiter waiter1 = new Waiter(msg);
        new Thread(waiter1, "waiter1").start();

        Waiter waiter2 = new Waiter(msg);
        new Thread(waiter2, "waiter2").start();

        Notifier notifier = new Notifier(msg, false);
        new Thread(notifier, "notifier").start();

        System.out.println("All the threads are started");
    }

    public static void testNotifyAll() {
        Message msg = new Message("process it");

        Waiter waiter1 = new Waiter(msg);
        new Thread(waiter1, "waiter1").start();

        Waiter waiter2 = new Waiter(msg);
        new Thread(waiter2, "waiter2").start();

        Notifier notifier = new Notifier(msg, false);
        new Thread(notifier, "notifier").start();

        System.out.println("All the threads are started");
    }
}

在启动两个线程同时执行wait方法的时候,会发现notify以后只有一个线程被唤醒了,而另一个线程则陷入了无尽地等待之中。

Links

仓库地址

https://github.com/javastudydemo/jsd-concurrent/tree/master/jsd-concurrent/src/main/java/net/ijiangtao/tech/concurrent/jsd/waitnotify/demo1

参考链接

目录
相关文章
|
IDE Java 编译器
java编程最基础学习
Java入门需掌握:环境搭建、基础语法、面向对象、数组集合与异常处理。通过实践编写简单程序,逐步深入学习,打牢编程基础。
593 1
|
Java
如何在Java中进行多线程编程
Java多线程编程常用方式包括:继承Thread类、实现Runnable接口、Callable接口(可返回结果)及使用线程池。推荐线程池以提升性能,避免频繁创建线程。结合同步与通信机制,可有效管理并发任务。
518 6
|
安全 前端开发 Java
从反射到方法句柄:深入探索Java动态编程的终极解决方案
从反射到方法句柄,Java 动态编程不断演进。方法句柄以强类型、低开销、易优化的特性,解决反射性能差、类型弱、安全性低等问题,结合 `invokedynamic` 成为支撑 Lambda 与动态语言的终极方案。
503 0
|
SQL Java 数据库
2025 年 Java 从零基础小白到编程高手的详细学习路线攻略
2025年Java学习路线涵盖基础语法、面向对象、数据库、JavaWeb、Spring全家桶、分布式、云原生与高并发技术,结合实战项目与源码分析,助力零基础学员系统掌握Java开发技能,从入门到精通,全面提升竞争力,顺利进阶编程高手。
1665 2
|
Java 开发者
Java并发编程:CountDownLatch实战解析
Java并发编程:CountDownLatch实战解析
761 100
|
NoSQL Java 关系型数据库
超全 Java 学习路线,帮你系统掌握编程的超详细 Java 学习路线
本文为超全Java学习路线,涵盖基础语法、面向对象编程、数据结构与算法、多线程、JVM原理、主流框架(如Spring Boot)、数据库(MySQL、Redis)及项目实战等内容,助力从零基础到企业级开发高手的进阶之路。
932 1
|
算法 Java
Java多线程编程:实现线程间数据共享机制
以上就是Java中几种主要处理多线程序列化资源以及协调各自独立运行但需相互配合以完成任务threads 的技术手段与策略。正确应用上述技术将大大增强你程序稳定性与效率同时也降低bug出现率因此深刻理解每项技术背后理论至关重要.
821 16
|
安全 Java Shell
Java模块化编程(JPMS)简介与实践
本文全面解析Java 9模块化系统(JPMS),帮助开发者解决JAR地狱、类路径冲突等常见问题,提升代码的封装性、性能与可维护性。内容涵盖模块化核心概念、module-info语法、模块声明、实战迁移、多模块项目构建、高级特性及最佳实践,同时提供常见问题和面试高频题解析,助你掌握Java模块化编程精髓,打造更健壮的应用。
|
安全 算法 Java
Java泛型编程:类型安全与擦除机制
Java泛型详解:从基础语法到类型擦除机制,深入解析通配符与PECS原则,探讨运行时类型获取技巧及最佳实践,助你掌握泛型精髓,写出更安全、灵活的代码。
|
安全 Java 数据库连接
2025 年最新 Java 学习路线图含实操指南助你高效入门 Java 编程掌握核心技能
2025年最新Java学习路线图,涵盖基础环境搭建、核心特性(如密封类、虚拟线程)、模块化开发、响应式编程、主流框架(Spring Boot 3、Spring Security 6)、数据库操作(JPA + Hibernate 6)及微服务实战,助你掌握企业级开发技能。
1485 3