gpt4 book ai didi

java - Java 中的启动/暂停/恢复方法

转载 作者:行者123 更新时间:2023-12-01 05:13:58 25 4
gpt4 key购买 nike

Possible Duplicate:
Start/Suspend/Resume/Suspend … a method invoked by other class

我想实现一个 Anytime k-NN 分类器,但我找不到在特定时间内调用“classify(...)”方法、暂停它、在该方法暂停之前获取可用结果的方法,恢复该方法特定的时间,暂停它,获取该方法暂停之前的可用结果,依此类推...

提前致谢!

最佳答案

我发布了PauseableThread最近在这里。

您可以使用ReadWriteLock 实现暂停。如果您每次遇到暂停机会时都会立即捕获一个写锁,那么您只需要暂停者捕获一个读锁来暂停您。

  // The lock.
private final ReadWriteLock pause = new ReentrantReadWriteLock();

// Block if pause has been called without a matching resume.
private void blockIfPaused() throws InterruptedException {
try {
// Grab a write lock. Will block if a read lock has been taken.
pause.writeLock().lockInterruptibly();
} finally {
// Release the lock immediately to avoid blocking when pause is called.
pause.writeLock().unlock();
}
}

// Pause the work. NB: MUST be balanced by a resume.
public void pause() {
// We can wait for a lock here.
pause.readLock().lock();
}

// Resume the work. NB: MUST be balanced by a pause.
public void resume() {
// Release the lock.
pause.readLock().unlock();
}

关于java - Java 中的启动/暂停/恢复方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11592345/

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