java notify 用法,如何在Java中使用wait()和notify()?

As I understand, I am suppose to call wait() on the mutex, when I want the current thread to stop working until another thread calls notify() on the same mutex object. That doesn't seem to be working.

I'm trying to make a thread print 1-10. Then wait for another thread to print 11-20. And then the first thread would again print 21-30

Main.java

public class Main {

public static void main(String[] args) throws InterruptedException {

Object mutex = 1;

Thread child1 = new Thread(new Child1(mutex));

Thread child2 = new Thread(new Child2(mutex));

child1.start();

child2.start();

}

}

Child1.java

public class Child1 implements Runnable {

Object mutex;

public Child1(Object mutex){

this.mutex = mutex;

}

public void run() {

synchronized (mutex) {

for(int c = 0; c < 10; c++){

System.out.println(c+1);

}

try {

wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

for(int c = 20; c < 31; c++){

System.out.println(c+1);

}

}

}

Child2.java

public class Child2 implements Runnable {

Object mutex;

public Child2(Object mutex) {

this.mutex = mutex;

}

public void run() {

synchronized (mutex) {

for (int c = 11; c < 21; c++) {

System.out.println(c);

}

notify();

}

}

}

Output

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17Exception in thread "Thread-0"

18

19

20

Exception in thread "Thread-1" java.lang.IllegalMonitorStateException

at java.lang.Object.wait(Native Method)

at java.lang.Object.wait(Object.java:502)

at task42.Child1.run(Child1.java:18)

at java.lang.Thread.run(Thread.java:745)

java.lang.IllegalMonitorStateException

at java.lang.Object.notify(Native Method)

at task42.Child2.run(Child2.java:15)

at java.lang.Thread.run(Thread.java:745)

What am I missing?

解决方案

You must add the mutex reference to wait() and notify(); that is, change wait() to mutex.wait() and notify() to mutex.notify().

Without this, you are calling to wait/notify on this (method() is equivalent to this.method())

Here is your code with the appropriate changes made:

Child1.java

public class Child1 implements Runnable {

Object mutex;

public Child1(Object mutex){

this.mutex = mutex;

}

public void run() {

synchronized (mutex) {

for(int c = 0; c < 10; c++){

System.out.println(c+1);

}

try {

mutex.wait(); // Changed here

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

for(int c = 20; c < 31; c++){

System.out.println(c+1);

}

}

}

Child2.java

public class Child2 implements Runnable {

Object mutex;

public Child2(Object mutex) {

this.mutex = mutex;

}

public void run() {

synchronized (mutex) {

for (int c = 11; c < 21; c++) {

System.out.println(c);

}

mutex.notify(); // Changed here

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值