Java多线程系列--“基础篇”05之 线程等待与唤醒

 

概要

本章,会对线程等待/唤醒方法进行介绍。涉及到的内容包括:
1. wait(), notify(), notifyAll()等方法介绍
2. wait()和notify()
3. wait(long timeout)和notify()
4. wait() 和 notifyAll()
5. 为什么notify(), wait()等函数定义在Object中,而不是Thread中

转载请注明出处:http://www.cnblogs.com/skywang12345/p/3479224.html

 

wait(), notify(), notifyAll()等方法介绍

在Object.java中,定义了wait(), notify()和notifyAll()等接口。wait()的作用是让当前线程进入等待状态,同时,wait()也会让当前线程释放它所持有的锁。而notify()和notifyAll()的作用,则是唤醒当前对象上的等待线程;notify()是唤醒单个线程,而notifyAll()是唤醒所有的线程。

Object类中关于等待/唤醒的API详细信息如下:
notify()        -- 唤醒在此对象监视器上等待的单个线程。
notifyAll()   -- 唤醒在此对象监视器上等待的所有线程。
wait()                                         -- 让当前线程处于“等待(阻塞)状态”,“直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法”,当前线程被唤醒(进入“就绪状态”)。
wait(long timeout)                    -- 让当前线程处于“等待(阻塞)状态”,“直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者超过指定的时间量”,当前线程被唤醒(进入“就绪状态”)。
wait(long timeout, int nanos)  -- 让当前线程处于“等待(阻塞)状态”,“直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者其他某个线程中断当前线程,或者已超过某个实际时间量”,当前线程被唤醒(进入“就绪状态”)。

 

2. wait()和notify()示例

下面通过示例演示"wait()和notify()配合使用的情形"。

// WaitTest.java的源码
class ThreadA extends Thread{

    public ThreadA(String name) {
        super(name);
    }

    public void run() {
        synchronized (this) {
            System.out.println(Thread.currentThread().getName()+" call notify()");
            // 唤醒当前的wait线程
            notify();
        }
    }
}

public class WaitTest {

    public static void main(String[] args) {

        ThreadA t1 = new ThreadA("t1");

        synchronized(t1) {
            try {
                // 启动“线程t1”
                System.out.println(Thread.currentThread().getName()+" start t1");
                t1.start();

                // 主线程等待t1通过notify()唤醒。
                System.out.println(Thread.currentThread().getName()+" wait()");
                t1.wait();

                System.out.println(Thread.currentThread().getName()+" continue");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

运行结果:

main start t1
main wait()
t1 call notify()
main continue

结果说明
如下图,说明了“主线程”和“线程t1”的流程。

(01) 注意,图中"主线程" 代表“主线程main”。"线程t1" 代表WaitTest中启动的“线程t1”。 而“锁” 代表“t1这个对象的同步锁”。
(02) “主线程”通过 new ThreadA("t1") 新建“线程t1”。随后通过synchronized(t1)获取“t1对象的同步锁”。然后调用t1.start()启动“线程t1”。
(03) “主线程”执行t1.wait() 释放“t1对象的锁”并且进入“等待(阻塞)状态”。等待t1对象上的线程通过notify() 或 notifyAll()将其唤醒。
(04) “线程t1”运行之后,通过synchronized(this)获取“当前对象的锁”;接着调用notify()唤醒“当前对象上的等待线程”,也就是唤醒“主线程”。
(05) “线程t1”运行完毕之后,释放“当前对象的锁”。紧接着,“主线程”获取“t1对象的锁”,然后接着运行。

对于上面的代码?曾经有个朋友问到过:t1.wait()应该是让“线程t1”等待;但是,为什么却是让“主线程main”等待了呢?
在解答该问题前,我们先看看jdk文档中关于wait的一段介绍:

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. 
In other words, this method behaves exactly as if it simply performs the call wait(0). The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

中文意思大概是:

引起“当前线程”等待,直到另外一个线程调用notify()或notifyAll()唤醒该线程。换句话说,这个方法和wait(0)的效果一样!(补充,对于wait(long millis)方法,当millis为0时,表示无限等待,直到被notify()或notifyAll()唤醒)。
“当前线程”在调用wait()时,必须拥有该对象的同步锁。该线程调用wait()之后,会释放该锁;然后一直等待直到“其它线程”调用对象的同步锁的notify()或notifyAll()方法。然后,该线程继续等待直到它重新获取“该对象的同步锁”,然后就可以接着运行。

注意:jdk的解释中,说wait()的作用是让“当前线程”等待,而“当前线程”是指正在cpu上运行的线程!
这也意味着,虽然t1.wait()是通过“线程t1”调用的wait()方法,但是调用t1.wait()的地方是在“主线程main”中。而主线程必须是“当前线程”,也就是运行状态,才可以执行t1.wait()。所以,此时的“当前线程”是“主线程main”!因此,t1.wait()是让“主线程”等待,而不是“线程t1”!

 

3. wait(long timeout)和notify()

wait(long timeout)会让当前线程处于“等待(阻塞)状态”,“直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者超过指定的时间量”,当前线程被唤醒(进入“就绪状态”)。
下面的示例就是演示wait(long timeout)在超时情况下,线程被唤醒的情况。

// WaitTimeoutTest.java的源码
class ThreadA extends Thread{

    public ThreadA(String name) {
        super(name);
    }

    public void run() {
        System.out.println(Thread.currentThread().getName() + " run ");
        // 死循环,不断运行。
        while(true)
            ;
    }
}

public class WaitTimeoutTest {

    public static void main(String[] args) {

        ThreadA t1 = new ThreadA("t1");

        synchronized(t1) {
            try {
                // 启动“线程t1”
                System.out.println(Thread.currentThread().getName() + " start t1");
                t1.start();

                // 主线程等待t1通过notify()唤醒 或 notifyAll()唤醒,或超过3000ms延时;然后才被唤醒。
                System.out.println(Thread.currentThread().getName() + " call wait ");
                t1.wait(3000);

                System.out.println(Thread.currentThread().getName() + " continue");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

运行结果

main start t1
main call wait 
t1 run                  // 大约3秒之后...输出“main continue”
main continue

结果说明
如下图,说明了“主线程”和“线程t1”的流程。
(01) 注意,图中"主线程" 代表WaitTimeoutTest主线程(即,线程main)。"线程t1" 代表WaitTest中启动的线程t1。 而“锁” 代表“t1这个对象的同步锁”。
(02) 主线程main执行t1.start()启动“线程t1”。
(03) 主线程main执行t1.wait(3000),此时,主线程进入“阻塞状态”。需要“用于t1对象锁的线程通过notify() 或者 notifyAll()将其唤醒” 或者 “超时3000ms之后”,主线程main才进入到“就绪状态”,然后才可以运行。
(04) “线程t1”运行之后,进入了死循环,一直不断的运行。
(05) 超时3000ms之后,主线程main会进入到“就绪状态”,然后接着进入“运行状态”。

 

4. wait() 和 notifyAll()

通过前面的示例,我们知道 notify() 可以唤醒在此对象监视器上等待的单个线程。
下面,我们通过示例演示notifyAll()的用法;它的作用是唤醒在此对象监视器上等待的所有线程。

 1 public class NotifyAllTest {
 2 
 3     private static Object obj = new Object();
 4     public static void main(String[] args) {
 5 
 6         ThreadA t1 = new ThreadA("t1");
 7         ThreadA t2 = new ThreadA("t2");
 8         ThreadA t3 = new ThreadA("t3");
 9         t1.start();
10         t2.start();
11         t3.start();
12 
13         try {
14             System.out.println(Thread.currentThread().getName()+" sleep(3000)");
15             Thread.sleep(3000);
16         } catch (InterruptedException e) {
17             e.printStackTrace();
18         }
19 
20         synchronized(obj) {
21             // 主线程等待唤醒。
22             System.out.println(Thread.currentThread().getName()+" notifyAll()");
23             obj.notifyAll();
24         }
25     }
26 
27     static class ThreadA extends Thread{
28 
29         public ThreadA(String name){
30             super(name);
31         }
32 
33         public void run() {
34             synchronized (obj) {
35                 try {
36                     // 打印输出结果
37                     System.out.println(Thread.currentThread().getName() + " wait");
38 
39                     // 唤醒当前的wait线程
40                     obj.wait();
41 
42                     // 打印输出结果
43                     System.out.println(Thread.currentThread().getName() + " continue");
44                 } catch (InterruptedException e) {
45                     e.printStackTrace();
46                 }
47             }
48         }
49     }
50 }

运行结果

t1 wait
main sleep(3000)
t3 wait
t2 wait
main notifyAll()
t2 continue
t3 continue
t1 continue

结果说明
参考下面的流程图。 
(01) 主线程中新建并且启动了3个线程"t1", "t2"和"t3"。
(02) 主线程通过sleep(3000)休眠3秒。在主线程休眠3秒的过程中,我们假设"t1", "t2"和"t3"这3个线程都运行了。以"t1"为例,当它运行的时候,它会执行obj.wait()等待其它线程通过notify()或额nofityAll()来唤醒它;相同的道理,"t2"和"t3"也会等待其它线程通过nofity()或nofityAll()来唤醒它们。
(03) 主线程休眠3秒之后,接着运行。执行 obj.notifyAll() 唤醒obj上的等待线程,即唤醒"t1", "t2"和"t3"这3个线程。 紧接着,主线程的synchronized(obj)运行完毕之后,主线程释放“obj锁”。这样,"t1", "t2"和"t3"就可以获取“obj锁”而继续运行了!

 

5. 为什么notify(), wait()等函数定义在Object中,而不是Thread中

Object中的wait(), notify()等函数,和synchronized一样,会对“对象的同步锁”进行操作。

wait()会使“当前线程”等待,因为线程进入等待状态,所以线程应该释放它锁持有的“同步锁”,否则其它线程获取不到该“同步锁”而无法运行!
OK,线程调用wait()之后,会释放它锁持有的“同步锁”;而且,根据前面的介绍,我们知道:等待线程可以被notify()或notifyAll()唤醒。现在,请思考一个问题:notify()是依据什么唤醒等待线程的?或者说,wait()等待线程和notify()之间是通过什么关联起来的?答案是:依据“对象的同步锁”

负责唤醒等待线程的那个线程(我们称为“唤醒线程”),它只有在获取“该对象的同步锁”(这里的同步锁必须和等待线程的同步锁是同一个),并且调用notify()或notifyAll()方法之后,才能唤醒等待线程。虽然,等待线程被唤醒;但是,它不能立刻执行,因为唤醒线程还持有“该对象的同步锁”。必须等到唤醒线程释放了“对象的同步锁”之后,等待线程才能获取到“对象的同步锁”进而继续运行。

总之,notify(), wait()依赖于“同步锁”,而“同步锁”是对象锁持有,并且每个对象有且仅有一个!这就是为什么notify(), wait()等函数定义在Object类,而不是Thread类中的原因。

 


更多内容

1. Java多线程目录(共xx篇)

2. Java多线程系列--“基础篇”01之 基本概念

3. Java多线程系列--“基础篇”02之 常用的实现多线程的两种方式

4. Java多线程系列--“基础篇”03之 Thread中start()和run()的区别

5. Java多线程系列--“基础篇”04之 synchronized关键字

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 多线程的读者-写者问题是一个经典的同步问题,涉及到多个读线程和写线程对共享数据的访问。以下是一个可能的Java多线程读者-写者问题的实现代码: ```java class ReaderWriter { int readers = 0; int writers = 0; int writeRequests = 0; public synchronized void startRead() throws InterruptedException { while (writers > 0 || writeRequests > 0) { wait(); } readers++; } public synchronized void endRead() { readers--; notifyAll(); } public synchronized void startWrite() throws InterruptedException { writeRequests++; while (readers > 0 || writers > 0) { wait(); } writeRequests--; writers++; } public synchronized void endWrite() { writers--; notifyAll(); } } class Reader implements Runnable { ReaderWriter readerWriter; public Reader(ReaderWriter readerWriter) { this.readerWriter = readerWriter; } public void run() { try { readerWriter.startRead(); // 读取共享数据的操作 Thread.sleep((int) (Math.random() * 1000)); readerWriter.endRead(); } catch (InterruptedException e) { e.printStackTrace(); } } } class Writer implements Runnable { ReaderWriter readerWriter; public Writer(ReaderWriter readerWriter) { this.readerWriter = readerWriter; } public void run() { try { readerWriter.startWrite(); // 写入共享数据的操作 Thread.sleep((int) (Math.random() * 1000)); readerWriter.endWrite(); } catch (InterruptedException e) { e.printStackTrace(); } } } public class Main { public static void main(String[] args) { ReaderWriter readerWriter = new ReaderWriter(); for (int i = 0; i < 5; i++) { new Thread(new Reader(readerWriter)).start(); } for (int i = 0; i < 5; i++) { new Thread(new Writer(readerWriter)).start(); } } } ``` 在上面的代码中,`ReaderWriter`类是一个用于管理读者和写者访问共享数据的同步类。`startRead()`和`endRead()`方法用于读者访问共享数据的开始和结束,`startWrite()`和`endWrite()`方法用于写者访问共享数据的开始和结束。在每个方法中使用`synchronized`关键字来保证同一时间只有一个线程可以执行。`notifyAll()`方法用于唤醒其他正在等待线程。 `Reader`类和`Writer`类分别是读者和写者线程的实现。在`run()`方法中,先调用对应的`startRead()`或`startWrite()`方法来获取访问权限,然后执行读取或写入操作,最后调用对应的`endRead()`或`endWrite()`方法来释放访问权限。 在`main()`方法中创建了5个读者线程和5个写者线程,并启动它们。 以上代码是一种可能的多线程读者-写者问题的实现,但并不是唯一的解决方案。在实际应用中,还需要考虑更多的场景和线程同步的细节,确保共享数据的一致性和线程的安全执行。 ### 回答2: Java多线程读者-写者问题可以通过使用synchronized关键字和wait()、notify()方法来实现。下面是一个简单的示例代码: ``` public class ReaderWriterProblem { private static final int MAX_READERS = 5; private static final int MAX_WRITERS = 2; private static int activeReaders = 0; private static boolean writerActive = false; public static void main(String[] args) { for (int i = 1; i <= MAX_READERS; i++) { new Thread(new Reader(i)).start(); } for (int i = 1; i <= MAX_WRITERS; i++) { new Thread(new Writer(i)).start(); } } static class Reader implements Runnable { private final int readerId; public Reader(int readerId) { this.readerId = readerId; } @Override public void run() { while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } read(); } } private void read() { synchronized (ReaderWriterProblem.class) { while (writerActive) { // 如果有写者在执行 try { ReaderWriterProblem.class.wait(); // 等待写者完成 } catch (InterruptedException e) { e.printStackTrace(); } } activeReaders++; // 增加活跃读者数量 } // 执行读操作 System.out.println("Reader " + readerId + "正在执行读操作"); synchronized (ReaderWriterProblem.class) { activeReaders--; // 减少活跃读者数量 if (activeReaders == 0) { // 如果没有其他读者 ReaderWriterProblem.class.notifyAll(); // 唤醒所有等待线程 } } } } static class Writer implements Runnable { private final int writerId; public Writer(int writerId) { this.writerId = writerId; } @Override public void run() { while (true) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } write(); } } private void write() { synchronized (ReaderWriterProblem.class) { while (activeReaders > 0 || writerActive) { // 如果有读者在执行或者有写者在执行 try { ReaderWriterProblem.class.wait(); // 等待其他线程完成操作 } catch (InterruptedException e) { e.printStackTrace(); } } writerActive = true; } // 执行写操作 System.out.println("Writer " + writerId + "正在执行写操作"); synchronized (ReaderWriterProblem.class) { writerActive = false; ReaderWriterProblem.class.notifyAll(); // 唤醒所有等待线程 } } } } ``` 在上述代码中,我们使用了一个整型变量`activeReaders`来记录当前活跃的读者数量,当读者开始读操作时,会先判断是否有写者在执行,如果有,则等待写者完成;然后增加`activeReaders`;接着执行读操作。读操作完成后,减少`activeReaders`,如果没有其他读者,则调用`notifyAll()`方法唤醒其他等待线程。写者也类似,当写者开始写操作时,会先判断是否有其他读者或写者在执行,如果有,则等待其他线程完成;然后执行写操作;最后,设置`writerActive`为false,并调用`notifyAll()`方法唤醒其他等待线程。 这种方式实现的读者-写者问题可以保证同一时间只能有一个写者或多个读者执行操作,从而保证数据的一致性和并发访问的正确性。 ### 回答3: Java多线程读者-写者问题可以通过使用synchronized关键字和wait()、notify()方法实现。 首先,读者-写者问题是指多个读者线程可以同时读取数据,但写者线程只能在没有读者线程时才能写入数据。 下面是使用Java实现的一个基本读者-写者问题的代码示例: ```java public class ReaderWriter { private int readers; private boolean isWriting; public ReaderWriter() { readers = 0; isWriting = false; } public synchronized void startRead() throws InterruptedException { while (isWriting) { wait(); } readers++; } public synchronized void endRead() { readers--; if (readers == 0) { notifyAll(); } } public synchronized void startWrite() throws InterruptedException { while (isWriting || readers > 0) { wait(); } isWriting = true; } public synchronized void endWrite() { isWriting = false; notifyAll(); } } ``` 上面的代码中,ReaderWriter类用于管理读者和写者的访问。其中,startRead()方法用于读者开始读取数据,endRead()方法用于读者结束读取;startWrite()方法用于写者开始写入数据,endWrite()方法用于写者结束写入。 在startRead()和startWrite()方法中,使用synchronized关键字修饰,确保同一时间只能有一个线程访问该方法。在读者读取数据时,如果有写者在写入,则调用wait()方法使读者线程进入等待状态。在写者写入数据时,如果有其他读者在读取,也调用wait()方法使写者线程进入等待状态。 在endRead()和endWrite()方法中,对读者和写者的数量进行更新,并使用notifyAll()方法唤醒等待线程。当所有读者都结束读取时,唤醒可能等待的写者线程;当写者结束写入时,唤醒可能等待的读者线程。 通过以上方法的实现,能够实现读者-写者问题的多线程并发访问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值