线程安全否定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 anotherThread
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
最满意答案
文章暗示每个函数都应该是异步的。 因此,
loadProfile
函数也需要callback
function loadProfile(id, callback) { ... }
现在“那个”函数应该异步执行,并在完成时调用回调函数。 通常在AJAX中加载数据,否则。
从前几个段落看来,作者似乎没有做任何实际上异步的事情。 它只是带回调函数的递归函数。 但这一切都是同步发生的,除非你有一个异步(或延期)调用的地方,而不是第一个例子。
另一种在JavaScript中异步编程的方法是通过Promise对象。
The article implies that every function should be asynchronous. Therefore, the
loadProfile
function takes acallback
as wellfunction 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.
相关问答
更多-
基本的回调函数(Basic callback function)[2023-03-30]
文章暗示每个函数都应该是异步的。 因此, loadProfile函数也需要callback function loadProfile(id, callback) { ... } 现在“那个”函数应该异步执行,并在完成时调用回调函数。 通常在AJAX中加载数据,否则。 从前几个段落看来,作者似乎没有做任何实际上异步的事情。 它只是带回调函数的递归函数。 但这一切都是同步发生的,除非你有一个异步(或延期)调用的地方,而不是第一个例子。 另一种在JavaScript中异步编程的方法是通过Promise对象。 T ... -
回调函数javascript(callback function javascript)[2022-09-14]
怎么样: 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. ...
-
回调函数 - NodeJS(callback function - NodeJS)[2021-11-08]
我打电话给天气模块,我得到一个回调函数(它是天气模块的参数),用于在都灵的天气打印到命令行。 但怎么可能呢? Javascript中的函数是第一类对象意味着您可以将函数存储到变量中并将其传递给另一个函数。 这种模式在Node.js和Javasript中很常见,这称为Continuation传递风格 (CPS) 希望能帮助到你。 i call a weather module and i get a callback function ( it is an argument of weather module ... -
回调函数的结果(Callback result to a function)[2022-07-10]
您需要定义一个将检索回调值的函数。 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 ... -
$这在回调函数中($this in callback function)[2023-11-28]
preg_replace_callback()中的回调参数允许调用方法,并允许传递数组来告诉方法回调的上下文。 这不仅是$ this,而且还有任何对象变量。 $foo = new Foo(); preg_replace_callback( "#pattern#", array($foo, 'bar'), $subject ); 在上面的例子中,如果Foo :: bar()是私有的,那就行不通了。 然而,在你原来的情况下,私有方法仍然被触发,因为使用$ this,这与private方 ... -
从其他类回调函数(callback on function from other class)[2022-02-13]
在customView类中创建ViewController类型的自定义属性。 在ViewController的viewDidLoad函数中,调用 theCustomView.viewController = self (其中theCustomView是实例 - 你可能也有这个参考,否则创建一个IBOutlet) 然后,在textFieldDidBeginEditing函数中,您可以使用 self.viewController.check() Create a custom property of typ ... -
了解回调函数(Understanding Callback function)[2023-05-10]
为什么会出现此错误? setTimeout期望将回调函数作为第一个参数 ,但是在第一个位置传递一个数字,为什么是异常。 第二种情况有效,因为你从第一个地方删除了orderNumber ,一个函数得到它。 你需要在第二位之后传递回调函数的参数。 请参阅setTimeout函数签名。 首先是回调函数,第二个 - 时间,至少调用函数,然后传递的所有内容都作为参数传递给回调函数。 这是函数的签名 - []是可选的。 setTimeout(function[, delay, param1, param2, ...]) ... -
没有函数指针的回调(Callback without function pointer)[2022-06-29]
只要参数用作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 ...