如何在Java中使用wait()和notify()?
据我所知,当我希望当前线程停止工作,直到另一个线程对同一个互斥对象调用notify()时,我假设对互斥对象调用wait()。这似乎不起作用 我在试着做一个1-10号的线纹。然后等待另一个线程打印11-20。然后第一个线程将再次打印21-30 Main.java如何在Java中使用wait()和notify()?,java,multithreading,mutex,Java,Multithreading,Mutex,据我所知,当我希望当前线程停止工作,直到另一个线程对同一个互斥对象调用notify()时,我假设对互斥对象调用wait()。这似乎不起作用 我在试着做一个1-10号的线纹。然后等待另一个线程打印11-20。然后第一个线程将再次打印21-30 Main.java public class Main { public static void main(String[] args) throws InterruptedException { Object mutex = 1;
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();
}
}
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);
}
}
}
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();
}
}
}
Child1.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();
}
}
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);
}
}
}
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();
}
}
}
我缺少什么?您必须将
互斥体
引用添加到wait()
和notify()
;也就是说,将wait()
更改为mutex.wait()
并将notify()
更改为mutex.notify()
如果没有此选项,您将在this
(method()
相当于this.method()
)
以下是您的代码,并进行了相应的更改:
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);
}
}
}
公共类Child1实现可运行{
对象互斥;
公共子对象1(对象互斥){
this.mutex=mutex;
}
公开募捐{
已同步(互斥){
对于(int c=0;c<10;c++){
系统输出打印项次(c+1);
}
试一试{
mutex.wait();//此处已更改
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
对于(int c=20;c<31;c++){
系统输出打印项次(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
}
}
}
public类Child2实现可运行{
对象互斥;
公共子对象2(对象互斥){
this.mutex=mutex;
}
公开募捐{
已同步(互斥){
对于(int c=11;c<21;c++){
系统输出打印ln(c);
}
mutex.notify();//此处已更改
}
}
}
您的代码以多种方式失败
首先,不能保证第一个线程也会首先运行。(特别是在多核上,两者并行运行的可能性很高)。因此,如果第二个线程首先进入Child2.run()
的synchronized
块,它甚至会在第一个线程处于等待状态之前调用mutex.notify()
。因此,第一个线程将永远留在mutex.wait()
中
其次,wait()
/notify()
不被认为直接用作线程握手机制。只有当您能够保证第一个线程在第二个线程调用notify()之前调用wait(),这才能起作用。通常,你不能
相反,应该使用wait()
来等待某个条件变为真。该条件通常由另一个线程更改,该线程通过调用notifyAll()
通知等待的线程。因此,握手机制是条件,而不是等待/通知
:
// 1st thread:
synchronized (lock) {
while (!condition) {
lock.wait();
}
// continue
}
// 2nd thread:
synchronized {
condition = true;
lock.notifyAll();
}
wait()
/notify()
或notifyAll()
的任何其他使用模式都是错误的!在循环中始终调用wait()
,这一点也非常重要,因为线程可能会偶然醒来,即使没有notify()
或notifyAll()
使用wait()/notifyAll()
因此,在您的情况下,可以将wait()
和notifyAll()
与stage变量结合使用:
public class Mutex {
static final Object lock = new Object();
static int stage = 1;
static void first() throws InterruptedException {
synchronized (lock) {
// we're already in stage 1
for(int i = 0; i < 10; ++i) System.out.println(i);
// enter stage 2
stage = 2;
lock.notifyAll();
// wait for stage 3
while (stage != 3) { lock.wait(); }
// now we're in stage 3
for(int i = 20; i < 30; ++i) System.out.println(i);
}
}
static void second() throws InterruptedException {
synchronized (lock) {
// wait for stage 2
while (stage != 2) { lock.wait(); }
// now we're in stage 2
for(int i = 20; i < 30; ++i) System.out.println(i);
// enter stage 3
stage = 3;
lock.notifyAll();
}
}
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
try {
Mutex.first();
} catch (InterruptedException ex) { }
}
}).start();
new Thread(new Runnable() {
public void run() {
try {
Mutex.second();
} catch (InterruptedException ex) { }
}
}).start();
}
}
公共类互斥{
静态最终对象锁=新对象();
静态int阶段=1;
static void first()抛出InterruptedException{
已同步(锁定){
//我们已经进入了第一阶段
对于(int i=0;i<10;++i)System.out.println(i);
//进入第二阶段
阶段=2;
lock.notifyAll();
//等待第三阶段
while(stage!=3){lock.wait();}
//现在我们进入第三阶段
对于(inti=20;i<30;++i)系统输出println(i);
}
}
static void second()引发InterruptedException{
已同步(锁定){
//等待第二阶段
while(stage!=2){lock.wait();}
//现在我们进入第二阶段
对于(inti=20;i<30;++i)系统输出println(i);
//进入第三阶段
阶段=3;
lock.notifyAll();
}
}
公共静态void main(字符串[]args){
新线程(newrunnable()){
公开募捐{
试一试{
Mutex.first();
}catch(InterruptedException ex){}
}
}).start();
新线程(newrunnable()){
公开募捐{
试一试{
Mutex.second();
}catch(InterruptedException ex){}
}
}).start();
}
}
@HovercraftFullOfEels,已添加输出。您在调用什么“等待并通知”?不是在互斥上!您的代码在调用方法的当前对象上调用这些方法,Child1和Child2的this
,而不是在互斥体上。您是wait()Child1线程和notify()Child2线程。您必须在同一个线程上调用wait()和notify()。@BKBatchelor:您不能在线程上调用wait和notify(当然可以,因为线程是一个对象,但您不想),而是在对象上调用。@hoverCraftFullOfels,谢谢!调用互斥锁完成了任务。使用wait
的唯一合理方法是在“条件循环”中循环,直到某个条件变为true
(或false
)。线程可能会偶然醒来-即使没有notify
。