gpt4 book ai didi

java - wait() 问题;

转载 作者:行者123 更新时间:2023-11-29 05:48:10 25 4
gpt4 key购买 nike

我目前有这个运行方法,我希望 3 个单独的线程访问并等待另一个线程通知。

public void run(){
try {
System.out.println(this.name + " waits on the table...");
/// waits for agent to signal that new ingredients have been passed out
wait();
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println(this.name + " stops waiting and checks the table...");
checkTable();
}

目前这会抛出一个 java.lang.IllegalMonitorStateException 可以通过在方法上使用 synchroize 来解决。问题是,当我使用 synchroize 时,第一个线程将等待,而其他两个线程无法执行任何操作,因为第一个线程锁定了该方法。所以我的问题是我怎样才能让三个独立的线程可以访问这个方法并同时等待?

最佳答案

使用wait()你必须 own the monitor . You acquire the ownership by using synchronize .在你的情况下 synchronize(this)

但是要阻塞 3 个线程并从第 4 个线程释放它们,您可以使用 java.util.concurrent.CyclicBarrierjava.util.concurrent.CountDownLatchPerception所述

这是一个带有 CyclicBarrier 的版本:

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class RendezVous extends Thread {

private final CyclicBarrier _barrier;

RendezVous( String name, CyclicBarrier barrier ) {
super( name );

_barrier = barrier;
setDaemon( true );
start();
}

@Override public void run() {
System.out.println( getName() + " waits for counterparts..." );
try { _barrier.await(); }
catch( InterruptedException | BrokenBarrierException x ) {
x.printStackTrace(); }
System.out.println( getName() + " has reached its rendez-vous!");
}

public static void main( String[] args ) throws InterruptedException {
CyclicBarrier barrier = new CyclicBarrier( 4 );
new RendezVous( "Rdvz 1", barrier ); Thread.sleep( 1000L );
new RendezVous( "Rdvz 2", barrier ); Thread.sleep( 1000L );
new RendezVous( "Rdvz 3", barrier ); Thread.sleep( 1000L );
new RendezVous( "Rdvz 4", barrier ); Thread.sleep( 10000L );
}
}

输出:

Rdvz 1 waits for counterparts...
Rdvz 2 waits for counterparts...
Rdvz 3 waits for counterparts...
Rdvz 4 waits for counterparts...
Rdvz 4 has reached its rendez-vous!
Rdvz 1 has reached its rendez-vous!
Rdvz 2 has reached its rendez-vous!
Rdvz 3 has reached its rendez-vous!

还有一个带有 CountDownLatch 的版本:

import java.util.concurrent.CountDownLatch;

public class CountDownLatchDemo extends Thread {

private final CountDownLatch _cdl;

CountDownLatchDemo( String name, CountDownLatch cdl ) {
super( name );

_cdl = cdl;
setDaemon( true );
start();
}

@Override public void run() {
System.out.println( getName() + " waits for counterparts..." );
try {
_cdl.countDown();
_cdl.await();
}
catch( InterruptedException x ) {
x.printStackTrace(); }
System.out.println( getName() + " has reached its rendez-vous!");
}

public static void main( String[] args ) throws InterruptedException {
CountDownLatch cdl = new CountDownLatch( 4 );
new CountDownLatchDemo( "Rdvz 1", cdl ); Thread.sleep( 1000L );
new CountDownLatchDemo( "Rdvz 2", cdl ); Thread.sleep( 1000L );
new CountDownLatchDemo( "Rdvz 3", cdl ); Thread.sleep( 1000L );
new CountDownLatchDemo( "Rdvz 4", cdl ); Thread.sleep( 10000L );
}
}

输出:

Rdvz 1 waits for counterparts...
Rdvz 2 waits for counterparts...
Rdvz 3 waits for counterparts...
Rdvz 4 waits for counterparts...
Rdvz 4 has reached its rendez-vous!
Rdvz 1 has reached its rendez-vous!
Rdvz 2 has reached its rendez-vous!
Rdvz 3 has reached its rendez-vous!

关于java - wait() 问题;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15038444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com