如何在设置AtomicBoolean之前检查另一个条件?(How to check another condition before set AtomicBoolean?)
我有一个AtomicBoolean并想要设置它,如果另一个条件也是如此:
例如,我们有一个带有Active和Saldo字段的bankaccount。 在setInctive方法中,我必须检查Saldo字段是否正好为零,然后将Active字段设置为false。
这是我的解决方案,但我不确定这是否真的有效!
private final AtomicBoolean active = new AtomicBoolean(true); private final AtomicReference<Double> saldo = new AtomicReference<>(0.0); @Override public boolean setInctive() { //First: check if saldo is 0.0 //Second: set active to false saldo.getAndUpdate(aDouble -> { if(aDouble == 0) active.set(false); return aDouble; }); //return negated value of the active field return !active.get(); }
什么是原子的正确解决方案,这甚至可以不使用锁?
I have an AtomicBoolean and want to set it, if another condition is also true:
For example we have a bankaccount with an Active and Saldo field. In the method setInctive, I have to check if the Saldo field is exactly zero and then set the Active field to false.
This is my solution, but I'm not sure if this really works!
private final AtomicBoolean active = new AtomicBoolean(true); private final AtomicReference<Double> saldo = new AtomicReference<>(0.0); @Override public boolean setInctive() { //First: check if saldo is 0.0 //Second: set active to false saldo.getAndUpdate(aDouble -> { if(aDouble == 0) active.set(false); return aDouble; }); //return negated value of the active field return !active.get(); }
What would be a correct solution with atomics and is this even possible without using locks?
原文:https://stackoverflow.com/questions/29803510
最满意答案
从代码本身获取选项,你不会有太多的运气。 Tkinter是tcl / tk解释器的包装器,很多选项只是通过tkinter传递给底层的tcl解释器。
可以在tcl / tk手册页中找到可用选项的权威文档来源: http : //tcl.tk/man/tcl8.5/TkCmd/contents.htm
You aren't going to have a lot of luck getting the options from the code itself. Tkinter is a wrapper around a tcl/tk interpreter, and a lot of the options are simply passed through tkinter to the underlying tcl interpreter.
The definitive source of documentation for the available options can be found in the tcl/tk man pages: http://tcl.tk/man/tcl8.5/TkCmd/contents.htm
相关问答
更多-
LINUX 如何查看JPG文件[2022-06-13]
find -
从代码本身获取选项,你不会有太多的运气。 Tkinter是tcl / tk解释器的包装器,很多选项只是通过tkinter传递给底层的tcl解释器。 可以在tcl / tk手册页中找到可用选项的权威文档来源: http : //tcl.tk/man/tcl8.5/TkCmd/contents.htm You aren't going to have a lot of luck getting the options from the code itself. Tkinter is a wrapper arou ...
-
4.1.5(1)的bash(1)手册页说: -- If no arguments follow this option, then the positional parameters are unset. Otherwise, the positional parame‐ ters are set to the args, even if some of them begin with a -. - Signal the ...
-
不,您误解了参数默认和可用选项。 查看KNeighborsClassifier的文档 ,参数algorithm是一个可选参数(即,您可以在KneighborsClassifier的构造函数中指定它)。 但是,如果您决定指定它,那么它有可用的选项: {'auto', 'ball_tree', 'kd_tree', 'brute'} 。 这意味着您只能从这些给定的algorithm选项中给出值,并且不能使用任何其他字符串来指定algorithm 。 默认选项是'auto' ,意味着如果您不提供任何值,那么它将在 ...
-
我会给你一些选择(双关语)。 不要使用可选参数,而是使用Option.apply 。 当参数不一定是可选的,但是您想要处理可能传递的null值时,这很有用。 def foo(bar: String): ?? = Option(bar)... // operate on the Option[String] 这样做的好处是Option.apply自动将null转换为None ,因此绝对不需要使用if / else。 对非可选参数使用重载。 当参数是真正可选的时,情况更是如此,但是为您提供了传递Option ...
-
使用getopt选项和参数$argv数组 - http://php.net/manual/en/reserved.variables.argv.php 也可以参考http://docs.php.net/manual/en/features.commandline.php查阅来自表单$argv的参数--name=VALUE Use getopt for options and the $argv array for parameters - http://php.net/manual/en/reserved. ...
-
您可以查看dx源以获取更多信息。 You can look at the dx source for more information.
-
要使用SVN 1.7,您需要通过将以下内容添加到更新站点列表来安装Subclipse 1.8: http://subclipse.tigris.org/update_1.8.x : http://subclipse.tigris.org/update_1.8.x To use SVN 1.7 you will need to install Subclipse 1.8 by adding the following to your list of Update Sites: http://subclipse ...
-
这个简单的解决方案呢? while [[ $# -gt 0 ]]; do case $1 in --date|-d ) DATE="$2" ; shift 2 ;; --host|-h ) HOST="$2" ; shift 2 ;; --logs ) LOGS=true ; shift 1 ;; --config ) CONFIG=true ; shift 1 ;; * ) usage; exit 1 ;; esac done 或者你可以使用getopts ...
-
我甚至不知道如何调用这个方法,我只知道我想要实现的行为。 这个东西叫做URI / URL,它的本地部分被传递给webserver。 然后,Web服务器处理该请求。 这甚至可以用Apache和PHP吗? 是。 甚至都没有。 这就是网络服务器的用途。 服务器上发生的事情完全由HTTP协议屏蔽,该协议仅知道URI / URL规范,该规范不管理是否以及如何匹配Web服务器上的具体进程或文件。 例如,对于Apache HTTP Server,有一个名为Mod_Rewrite的着名模块可以进行URL重写。 通常用户浏览 ...