找到你要的答案

Q:Any issue with compareAndSet method in AtomicBoolean?

Q:在AtomicBoolean compareandset方法的任何问题吗?

I have encountered with one issue with compareAndSet of AtomicBoolean class. I cannot post my code in this forum. But I will explain the scenario.

1) I have Two mutual exclusive features of one functionality.

2) Both feature have their own cap i.e. a user can use the feature for X number of times only. For example sake, assume that user can use first feature 4 times and second feature 2 times

3) First feature is enabled for 50 minutes of the hour i.e. second feature is disabled during that time

4) Second feature is enabled in last 10 minutes of the hour i.e first feature is disabled during that time

5) Both these features are using same AtomicBoolean attribute ("in progress"). This boolean will check user's earlier request is in progress or not. If user's earlier request is in progress, it will show message : "Your request is already in progress" and boolean will be marked as "true". Once his request is processed, the boolean will turn to "false". A user can re-request the feature only when "in progress" is false.

6) During switch over from first feature to second feature, a particular user got issue. User used first feature for max cap. Now he is trying to use second feature but got a message "Your request is already in progress" even though it is first request for accessing second feature.

This happens only when AtomicBoolean is not reset by first feature. The application ran fine for over 3 months but I have received complaint from one user.

On receiving user request, I got a doubt on "AtomicBoolean"'s compareAndSet method usage. Is there any possible bug with this API? Have you ever encountered any issues with this API? I have not encountered any exceptions/errors in log file

I have encountered with one issue with compareAndSet of AtomicBoolean class. I cannot post my code in this forum. But I will explain the scenario.

1)我有两个互斥功能的一个功能。

2)这两个功能都有自己的上限,即用户可以使用功能的x次数只有。例如,假设用户可以使用4次和第二次功能的第一次功能的2倍

3)第一功能是启用了50分钟的时间,即第二个功能是在那段时间禁用

4)第二个功能是启用在过去的10分钟的时间,即第一功能是在那段时间禁用

5)这些特征都使用相同的AtomicBoolean属性(“进步”)。此布尔将检查用户的早期请求是否正在进行中。如果用户的早期请求正在进行中,它将显示消息:“您的请求已在进行中”,布尔将被标记为“true”。一旦他的请求被处理,布尔将变成“false”。用户只能在“正在进行”是错误的情况下重新请求该功能。

6)在切换从第一个功能到第二个功能,一个特定的用户得到的问题。用户使用第一个功能的上限。现在他试图使用第二个功能,但得到一个消息“你的请求已经在进行”,即使它是第一次访问第二个功能的请求。

这种情况只有当AtomicBoolean不是第一功能复位。该应用程序运行罚款超过3个月,但我收到了来自一个用户的投诉。

在接收用户的请求,我在“atomicboolean”的compareandset方法的使用有一个疑问。是否有任何可能的错误与此API?你有没有遇到任何问题,这个API?我没有遇到任何异常/错误日志文件

answer1: 回答1:

I doubt very much the problem is with the code in Java's Atomic Boolean class.

Almost certainly the problem is in the code that calls the AtomicBoolean. The way you describe the checking for whether an earlier request is in progress suggests that the problem may be in improper synchronization of the code that does this checking. A Java AtomicBoolean won't "check user's earlier request" for you, so you must have written the code that does so; you might try asking for a review of that code from someone who is experienced in multithreaded code.

The fact that the problem only showed up after months of proper operation is in fact quite typical of how concurrency bugs show up. They are often very intermittent. This is another indication that is likely a race condition in your code.

You really need to provide excerpts from your code if you want further help.

我很怀疑的问题是在java的Atomic Boolean类的代码。

几乎可以肯定的问题是代码中调用AtomicBoolean。您描述的检查是否较早的请求正在进行的方式表明,问题可能是在不同步的代码,此检查。一个java atomicboolean不会检查用户的申请”你,所以你必须有书面的,所以代码;你可能会问的人是多线程代码审查,代码的经验。

事实上,这个问题只出现了几个月的正确操作后,实际上是典型的并发错误如何出现。他们往往非常间歇性。这是另一个指示,可能是在您的代码的比赛条件。

你真的需要提供摘录从您的代码,如果你想进一步帮助。

answer2: 回答2:

compareAndSet method of AtomicBoolean does not have any issue. I found the issue with application flow.

I am sharing my experience so that you may not land in same type of issue.

I have found a path in application, where the boolean was set as true for the first feature. During an error scenario, the flag was not reset. In normal scenario, the flag was re-set properly. Once the flag is re-set, second feature will work in normal way as per expectations.

Since getting this error is very infrequent, application ran fine for some months. Once I got the error scenario, second feature was not enabled since flag was not re-set in exception scenario.

After re-setting the boolean in all possible cases ( success & failure), this scenario is not repeated. My suspicion about AtomicBoolean has become false.

AtomicBoolean compareandset方法不会有任何问题。我发现了应用流程的问题。

我分享我的经验,使你可以不降落在同一类型的问题。

我在应用程序中找到了一个路径,其中布尔值被设置为第一个。在错误情况下,未重置标志。在正常情况下,标志重新设置正确。一旦标志重新设置,第二个功能将按照预期正常工作。

由于得到这个错误是非常罕见的,应用程序运行好几个月。一旦我得到了错误的情况,第二个功能没有启用,因为标志没有重新设置在异常情况下。

重新设置布尔在所有可能的情况下(成功和失败),这种情况是不重复。我怀疑AtomicBoolean已经成为虚假。

java  concurrency