日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java pause_java – 更有效的暂停循环方式

發(fā)布時(shí)間:2023/12/10 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java pause_java – 更有效的暂停循环方式 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

可用的工具是:

等待/通知 – 我們都試圖擺脫這個(gè)古老的系統(tǒng).

信號(hào)量 – 一旦你的線程抓住它,你持有它直到釋放,所以再次抓住它不會(huì)阻止.這意味著您無(wú)法在自己的線程中暫停.

CyclicBarrier – 每次使用時(shí)都必須重新創(chuàng)建.

ReadWriteLock – 我的最愛(ài).您可以讓任意多個(gè)線程暫停,并且只有當(dāng)所有線程都調(diào)用了簡(jiǎn)歷時(shí)才能恢復(fù).如果你愿意,你甚至可以暫停一下.

import java.util.concurrent.locks.ReadWriteLock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**

* PauseableThread is a Thread with pause/resume and cancel methods.

*

* The meat of the process must implement `step`.

*

* You can either extend this and implement `step` or use the factory.

*

* Note that I cannot extend Thread because my resume will clash with Thread's deprecated one.

*

* Usage: Either write a `Stepper` and run it in a `PausableThread` or extend `PausableThread` and call `blockIfPaused()` at appropriate points.

*/

public abstract class PauseableThread implements Runnable {

// The lock.

// We'll hold a read lock on it to pause the thread.

// The thread will momentarily grab a write lock on it to pause.

// This way you can have multiple pausers using normal locks.

private final ReadWriteLock pause = new ReentrantReadWriteLock();

// Flag to cancel the wholeprocess.

private volatile boolean cancelled = false;

// The exception that caused it to finish.

private Exception thrown = null;

@Override

// The core run mechanism.

public void run() {

try {

while (!cancelled) {

// Block here if we're paused.

blockIfPaused();

// Do my work.

step();

}

} catch (Exception ex) {

// Just fall out when exception is thrown.

thrown = ex;

}

}

// 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();

}

// Stop.

public void cancel() {

// Stop everything.

cancelled = true;

}

// start - like a thread.

public void start() {

// Wrap it in a thread.

new Thread(this).start();

}

// Get the exceptuion that was thrown to stop the thread or null if the thread was cancelled.

public Exception getThrown() {

return thrown;

}

// Create this method to do stuff.

// Calls to this method will stop when pause is called.

// Any thrown exception stops the whole process.

public abstract void step() throws Exception;

// Factory to wrap a Stepper in a PauseableThread

public static PauseableThread make(Stepper stepper) {

StepperThread pauseableStepper = new StepperThread(stepper);

// That's the thread they can pause/resume.

return pauseableStepper;

}

// One of these must be used.

public interface Stepper {

// A Stepper has a step method.

// Any exception thrown causes the enclosing thread to stop.

public void step() throws Exception;

}

// Holder for a Stepper.

private static class StepperThread extends PauseableThread {

private final Stepper stepper;

StepperThread(Stepper stepper) {

this.stepper = stepper;

}

@Override

public void step() throws Exception {

stepper.step();

}

}

// My test counter.

static int n = 0;

// Test/demo.

public static void main(String[] args) throws InterruptedException {

try {

// Simple stepper that just increments n.

Stepper s = new Stepper() {

@Override

public void step() throws Exception {

n += 1;

Thread.sleep(10);

}

};

PauseableThread t = PauseableThread.make(s);

// Start it up.

t.start();

Thread.sleep(1000);

t.pause();

System.out.println("Paused: " + n);

Thread.sleep(1000);

System.out.println("Resuminng: " + n);

t.resume();

Thread.sleep(1000);

t.cancel();

} catch (Exception e) {

}

}

}

編輯:修改代碼以更常用.

總結(jié)

以上是生活随笔為你收集整理的java pause_java – 更有效的暂停循环方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。