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

参考链接

目录
相关文章
|
4天前
|
Java 测试技术 开发者
Java面试题:解释CountDownLatch, CyclicBarrier和Semaphore在并发编程中的使用
Java面试题:解释CountDownLatch, CyclicBarrier和Semaphore在并发编程中的使用
27 11
|
1天前
|
Java 调度 开发者
Java中的并发编程:从基础到高级
【7月更文挑战第14天】在Java的世界中,并发编程是提升应用性能和响应能力的关键。本文将带领读者从线程的基础概念出发,深入探讨Java内存模型,逐步过渡到高级并发工具类如Executors框架和并发集合,最后通过案例分析展示如何在实际开发中运用这些知识解决并发问题。文章旨在为初学者提供清晰的学习路径,同时为有经验的开发者提供深度参考。
11 4
|
1天前
|
安全 Java 开发者
Java并发编程中的线程安全性与性能优化
在Java编程中,处理并发问题是至关重要的。本文探讨了Java中线程安全性的概念及其在性能优化中的重要性。通过深入分析多线程环境下的共享资源访问问题,结合常见的并发控制手段和性能优化技巧,帮助开发者更好地理解和应对Java程序中的并发挑战。 【7月更文挑战第14天】
|
1天前
|
监控 Java API
Java并发编程之线程池深度解析
【7月更文挑战第14天】在Java并发编程领域,线程池是提升性能、管理资源的关键工具。本文将深入探讨线程池的核心概念、内部工作原理以及如何有效使用线程池来处理并发任务,旨在为读者提供一套完整的线程池使用和优化策略。
|
3天前
|
存储 安全 算法
深入理解Java并发编程:线程安全与性能优化
【5月更文挑战第72天】 在现代软件开发中,尤其是Java应用开发领域,并发编程是一个无法回避的重要话题。随着多核处理器的普及,合理利用并发机制对于提高软件性能、响应速度和资源利用率具有重要意义。本文旨在探讨Java并发编程的核心概念、线程安全的策略以及性能优化技巧,帮助开发者构建高效且可靠的并发应用。通过实例分析和理论阐述,我们将揭示在高并发环境下如何平衡线程安全与系统性能之间的关系,并提出一系列最佳实践方法。
|
1天前
|
Java 开发者
Java并发编程中的锁机制与性能优化
【7月更文挑战第14天】本文深入探讨了Java中锁的概念、种类及其在并发编程中的应用,并分析了不同锁类型对程序性能的影响。通过实例展示了如何合理选择和使用锁来提升应用的性能,同时指出了锁使用过程中可能遇到的问题和调优策略。旨在为Java开发者提供锁机制的深入理解和性能优化的实用建议。
|
6天前
|
传感器 数据采集 监控
Java串口编程入门
Java串口编程入门
|
15天前
|
Java 数据处理 调度
Java多线程编程入门指南
Java多线程编程入门指南
|
15天前
|
传感器 数据采集 监控
Java串口编程入门
Java串口编程入门
|
12天前
|
Oracle 安全 Java
Java编程入门:从基础到高级技巧
Java编程入门:从基础到高级技巧