首页 \ 问答 \ 线程安全否定AtomicBoolean get()作为while循环中的条件(Thread-safe negated AtomicBoolean get() as a condition in a while loop)

线程安全否定AtomicBoolean get()作为while循环中的条件(Thread-safe negated AtomicBoolean get() as a condition in a while loop)

假设我有以下代码:

AtomicBoolean condition;
condition = new AtomicBoolean(false);
(...)
while(!condition.get()){
   // do some stuff
}

我知道condition.get()原子 ,但是,也是!condition.get()原子?

我的意思是,是否可能发生一个Thread以原子方式读取布尔值,然后在应用! 操作,以便另一个Thread进入循环之前? 如果是这样的话,使用如下函数会更好吗?

private synchronized boolean checkNegatedCondition(AtomicBoolean cond){
    return !cond.get();
}

(...)

while(checkNegatedCondition(condition)){
   // do some stuff
}

提前致谢。


Suppose I have the following code:

AtomicBoolean condition;
condition = new AtomicBoolean(false);
(...)
while(!condition.get()){
   // do some stuff
}

I know that condition.get() is atomic, but, is !condition.get() atomic as well?

I mean, could it happen that one Thread read the boolean value atomically and then it were interrupted before applying the ! operation, so that another Thread gets into the loop before? If that's the case, would it be better to use a function such as:

private synchronized boolean checkNegatedCondition(AtomicBoolean cond){
    return !cond.get();
}

(...)

while(checkNegatedCondition(condition)){
   // do some stuff
}

Thanks in advance.


原文:https://stackoverflow.com/questions/45673934
更新时间:2024-04-02 13:04

最满意答案

文章暗示每个函数都应该是异步的。 因此, loadProfile函数也需要callback

function loadProfile(id, callback) { ... }

现在“那个”函数应该异步执行,并在完成时调用回调函数。 通常在AJAX中加载数据,否则。

从前几个段落看来,作者似乎没有做任何实际上异步的事情。 它只是带回调函数的递归函数。 但这一切都是同步发生的,除非你有一个异步(或延期)调用的地方,而不是第一个例子。

另一种在JavaScript中异步编程的方法是通过Promise对象。


The article implies that every function should be asynchronous. Therefore, the loadProfile function takes a callback as well

function loadProfile(id, callback) { ... }

Now "that" function should do it asynchronously and when it is done, then call the callback function. Typically loading data in AJAX, or else.

From the first few paragraphs, it seems that the author is not doing anything actually asynchronous. It is just recursive functions with callbacks. But it all happens synchronously unless you have an asynchronous (or deferred) call somewhere which is not the case of the first examples given.

Another approach to asynch programming in javascript is via a Promise object.

相关问答

更多
  • 文章暗示每个函数都应该是异步的。 因此, loadProfile函数也需要callback function loadProfile(id, callback) { ... } 现在“那个”函数应该异步执行,并在完成时调用回调函数。 通常在AJAX中加载数据,否则。 从前几个段落看来,作者似乎没有做任何实际上异步的事情。 它只是带回调函数的递归函数。 但这一切都是同步发生的,除非你有一个异步(或延期)调用的地方,而不是第一个例子。 另一种在JavaScript中异步编程的方法是通过Promise对象。 T ...
  • 怎么样: function func1(callback, data){ var event = 'something'; callback(event, data); } function func2(data, data){ } var Obj = new Class({ initialize: function(){ this.data = "data"; //there I want to use func1 and use func2 ...
  • 您可以使用bind来传递回调函数。 同时你也可以传递this ,所以你保持对当前Game对象实例的引用: function Game(){ var i = 0; var anim = false; this.turn = 1; this.move = function(){ this.animate(this.advanceTurn.bind(this)); } this.advanceTurn = function(){ this. ...
  • 我打电话给天气模块,我得到一个回调函数(它是天气模块的参数),用于在都灵的天气打印到命令行。 但怎么可能呢? Javascript中的函数是第一类对象意味着您可以将函数存储到变量中并将其传递给另一个函数。 这种模式在Node.js和Javasript中很常见,这称为Continuation传递风格 (CPS) 希望能帮助到你。 i call a weather module and i get a callback function ( it is an argument of weather module ...
  • 您需要定义一个将检索回调值的函数。 function multiply(x,y, callback) { let res = x * y callback(res) } function hardOperation(a,b) { multiply(a, b, function(res){ res = a + b + res console.log("The result is " + res); }) } hardOperat ...
  • preg_replace_callback()中的回调参数允许调用方法,并允许传递数组来告诉方法回调的上下文。 这不仅是$ this,而且还有任何对象变量。 $foo = new Foo(); preg_replace_callback( "#pattern#", array($foo, 'bar'), $subject ); 在上面的例子中,如果Foo :: bar()是私有的,那就行不通了。 然而,在你原来的情况下,私有方法仍然被触发,因为使用$ this,这与private方 ...
  • 在customView类中创建ViewController类型的自定义属性。 在ViewController的viewDidLoad函数中,调用 theCustomView.viewController = self (其中theCustomView是实例 - 你可能也有这个参考,否则创建一个IBOutlet) 然后,在textFieldDidBeginEditing函数中,您可以使用 self.viewController.check() Create a custom property of typ ...
  • 为什么会出现此错误? setTimeout期望将回调函数作为第一个参数 ,但是在第一个位置传递一个数字,为什么是异常。 第二种情况有效,因为你从第一个地方删除了orderNumber ,一个函数得到它。 你需要在第二位之后传递回调函数的参数。 请参阅setTimeout函数签名。 首先是回调函数,第二个 - 时间,至少调用函数,然后传递的所有内容都作为参数传递给回调函数。 这是函数的签名 - []是可选的。 setTimeout(function[, delay, param1, param2, ...]) ...
  • 只要参数用作PaStreamCallback* (它是指向 函数的指针 ),就可以了 PaError Pa_OpenStream ( PaStream ** stream, const PaStreamParameters * inputParameters, const PaStreamParameters * outputParameters, double sampleRate, ...
  • 在你的代码中, callback()在执行setTimeout()后执行,但setTimeout的回调将在5000ms后触发,这是预期的行为。 所以如果你想在hello()执行callback()来执行: function doSomething(callback){ setTimeout(function(){ hello(); callback(); },5000); } In your code callback() was executing af ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。