gpt4 book ai didi

java CyclicBarrier 没有被重置破坏

转载 作者:行者123 更新时间:2023-11-30 08:32:44 25 4
gpt4 key购买 nike

我正在尝试通过在启动(等待)几个方(线程)的过程中重置 cyclicbarrier 来测试 BrokenBarrierException,请在下面找到相同的示例代码。

用户类:

public class User implements Runnable {

private final String name;
private final CyclicBarrier cb;

public User(String name, CyclicBarrier cb) {
this.name = name;
this.cb = cb;
}

@Override
public void run() {
System.out.println(name+" user started !!!! ");
try {
cb.await();
} catch (InterruptedException | BrokenBarrierException e) {
System.out.println(e);
e.printStackTrace();
}
}
}

CyclicBarrierTest 类:

public class CyclicBarrierTest {

public static void main(String[] args) {
CyclicBarrier barrier = new CyclicBarrier(5);

User user1 = new User("USER1", barrier);
new Thread(user1).start();

User user2 = new User("USER2", barrier);
new Thread(user2).start();

//Expected users are 5, but only 2 user threads started so far
// and resetting below which should throw barrier broken exception

barrier.reset();

if(barrier.isBroken()) {
System.out.println("Barrier broken ");
}
}
}

所以,在运行上面的 main() 之后,我可以得到任何异常,而且“Barrier broken”也没有打印出来。线程只是在等待。

I have referred the CyclicBarrier API below link:

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html

public void reset():

Resets the barrier to its initial state. If anyparties are currently waiting at the barrier, they will return with aBrokenBarrierException.

但根据 API 描述,我的上述代码似乎无法正常工作,那么我的上述代码有什么问题,为什么它没有抛出 BrokenBarrierException ?

你能帮忙吗?

最佳答案

如果你想抛出异常,你必须确保 barrier.reset();cb.await(); 之后执行,但是这里 System. out.println(name+"user started !!!! "); 是一个非常昂贵的语句,它使得 barrier.reset(); 执行得太早,你可以在之前添加一个 sleep 语句barrier.reset(); ,比如 Thread.sleep(100);

isBroken 的文档:

true if one or more parties broke out of this barrier due to interruption or timeout since construction or the last reset, or a barrier action failed due to an exception; false otherwise.

如果你想把它当成破烂的,你可以为当事人做点什么。您需要删除 reset 以使线程等待。

public class User implements Runnable {

private final String name;
private final CyclicBarrier cb;

public User(String name, CyclicBarrier cb) {
this.name = name;
this.cb = cb;
}

@Override
public void run() {
System.out.println(name+" user started !!!! ");
try {
cb.await(1,TimeUnit.SECONDS);
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(name+" user ended !!!! ");
}
}


public class CyclicBarrierTest {

public static void main(String[] args) throws Exception {
CyclicBarrier barrier = new CyclicBarrier(5);

User user1 = new User("USER1", barrier);
new Thread(user1).start();

User user2 = new User("USER2", barrier);
new Thread(user2).start();


//Expected users are 5, but only 2 user threads started so far
// and resetting below which should throw barrier broken exception
Thread.sleep(100);
// barrier.reset();
Thread.sleep(1100);
if(barrier.isBroken()) {
System.out.println("Barrier broken ");
}
}
}

关于java CyclicBarrier 没有被重置破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39930381/

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