gpt4 book ai didi

java - 什么是 java 的 ManualResetEvent 等价物?

转载 作者:IT老高 更新时间:2023-10-28 21:05:26 25 4
gpt4 key购买 nike

java 中的 ManualResetEvent 是什么? ?

最佳答案

class ManualResetEvent {

private final Object monitor = new Object();
private volatile boolean open = false;

public ManualResetEvent(boolean open) {
this.open = open;
}

public void waitOne() throws InterruptedException {
synchronized (monitor) {
while (open==false) {
monitor.wait();
}
}
}

public boolean waitOne(long milliseconds) throws InterruptedException {
synchronized (monitor) {
if (open)
return true;
monitor.wait(milliseconds);
return open;
}
}

public void set() {//open start
synchronized (monitor) {
open = true;
monitor.notifyAll();
}
}

public void reset() {//close stop
open = false;
}
}

关于java - 什么是 java 的 ManualResetEvent 等价物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1064596/

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