如何阻止和等待使用AtomicBoolean [英] How to Block and wait using AtomicBoolean

查看:531
本文介绍了如何阻止和等待使用AtomicBoolean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种暂停一个线程的方法。



我开始使用布尔标志(称为'paused'), while循环(暂停)。



在while循环中有一个 Thread.wait()来阻止执行。



我一直在查看 AtomicBoolean ,它似乎做到了,它不会阻止。



有一个替代或扩展版本的AtomicBoolean,它有一个块方法?



ie例如 AtomoicBoolean.get(false) AtomicBoolean.getFalse()



他们有一个阻止队列,因此是阻止值。



当前设置为:

  while(paused.get()){
synchronized(paused){
try {

paused.wait();
} catch(Exception e){
}

paused.notify();
}
}

  public void pause(){
if(paused.compareAndSet(false,true)){
synchronized(paused){
paused 。通知();
}
}

}


public void resume(){
if(paused.compareAndSet )){
synchronized(paused){
paused.notify();
}
}
}


解决方案>

  AtomicBoolean lock = new AtomicBoolean(false); 
if(lock.compareAndSet(false,true)){
try {
// do something
} catch(Exception e){
//错误处理
} finally {
lock.set(false);
}
}

首先,除非使用原子操作测试和设置), AtomicBoolean 与普通布尔(如果他们是可变的)无关。这里我使用 compareAndSet ,所以如果标志关闭,它只进入临界区。



要暂停线程使用标志,不要去主动等待(线程主体中有一些循环要求我暂停了吗? ),因为它不是一个有效的做法。我将使用等待通知方案。当线程没有更多的工作要做,它调用 wait 某些对象。然后,要重新启动,一些其他线程调用通知

$



如果您想立即暂停在设置标志时跳过执行),您可以尽可能多的步骤划分代码,并用测试包装每一个,最后等待,如果暂停:

  public void run(){
while(true){
if(!paused){
// do something
}

if(!paused){
// do something
}

if(!paused){
// do something
}

if(!paused){
// do something
}

if(paused){
//等待某个对象
}
}
}

您的代码,步骤可能甚至嵌套,或包含不可分割的执行单元,涉及几个步骤。


I am looking for a way of pausing a Thread.

I started with affectively using a boolean flag (called 'paused'), and wrapping a check with a while loop (pause).

Within the while loop there’s a Thread.wait() to block the execution.

I’ve been looking at the AtomicBoolean, which seems to do the trick apart from it doesn’t block.

Is there a alternative or extended version of AtomicBoolean that has a block method ?

i.e. something like AtomicBoolean.getFalse() of AtomoicBoolean.get(false)?

They have a Blocking Queue, so a Blocking value.

Current setup is :

while (paused.get()) {
        synchronized (paused) {
            try {

                paused.wait();
            } catch (Exception e) {
            }

            paused.notify();
        }
    }

with

public void pause() {
    if (paused.compareAndSet(false, true)) {
        synchronized (paused) {
            paused.notify();
        }
    }

}


public void resume() {
    if (paused.compareAndSet(true, false)) {
        synchronized (paused) {
            paused.notify();
        }
    } 
}

解决方案

    AtomicBoolean lock = new AtomicBoolean(false);
    if(lock.compareAndSet(false, true)){
        try {
            //do something
        } catch(Exception e){
            //error handling
        } finally {
            lock.set(false);
        }
    }

First, unless you use an atomic operation (something like test-and-set), AtomicBoolean is as useless as a regular Boolean (If they were mutable). Here I'm using compareAndSet, so that it only enters the critical section if the flag was down. Remember to always unlock in finally.

To pause a thread using a flag, don't go for active wait (some loop in thread body asking "Am I paused?"), as it is not an efficient practice. I'd use a wait-notify scheme. When the thread has no more work to do, it calls wait on some object. Then, to restart, some other thread calls notify on that same object.

If you want to immediately pause (in terms of skip execution when the flag is set), you could divide the code in as much steps as possible, and wrap each one with a test, to finally wait if paused:

public void run(){
    while(true){
        if(!paused){
            //do something
        }

        if(!paused){
            //do something
        }

        if(!paused){
            //do something
        }

        if(!paused){
            //do something
        }

        if(paused){
            //wait on some object
        }
    }       
}   

Depending of your code, the steps may be even nested, or include undivisible units of execution involving several steps.

这篇关于如何阻止和等待使用AtomicBoolean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆