Questions tagged [atomicboolean]
29 questions
301
votes
12 answers
Volatile boolean vs AtomicBoolean
What does AtomicBoolean do that a volatile boolean cannot achieve?

JeffV
- 52,985
- 32
- 103
- 124
22
votes
4 answers
Difference between getAndSet and compareAndSet in AtomicBoolean
The thread title should be self-explnatory... I'm a bit confused between the specification of below methos from AtomicBoolean class:
java.util.concurrent.atomic.AtomicBoolean#compareAndSet
java.util.concurrent.atomic.AtomicBoolean#getAndSet
My…

tmarwen
- 15,750
- 5
- 43
- 62
9
votes
2 answers
AtomicBoolean vs synchronized block
I was trying to cut thread contention in my code by replacing some synchronized blocks with AtomicBoolean.
Here's an example with synchronized:
public void toggleCondition() {
synchronized (this.mutex) {
if (this.toggled) {
…

biasedbit
- 2,860
- 4
- 30
- 47
7
votes
3 answers
Round robin scheduling algorithm in Java using AtomicBoolean
I want to implement a strict round robin scheduling when i send requests to an external system. There are two external system servers. The first request should go to 'System1' and the second request must go to 'System2' and next one to 'System1' and…

Albin
- 371
- 1
- 4
- 18
5
votes
3 answers
Swift 3 - Atomic boolean
Does anybody know how to make an atomic boolean in iOS 10?
Current code:
import UIKit
struct AtomicBoolean {
fileprivate var val: UInt8 = 0
/// Sets the value, and returns the previous value.
/// The test/set is an atomic operation.
…

PAK
- 431
- 4
- 17
4
votes
1 answer
How to compare AtomicBoolean with boolean value in java
I'm trying to deploy TTAS in a multi-threading application in java, using this code:
AtomicBoolean state= new AtomicBoolean(false);
void lock(){
while(true)
{
while(state.get())
{
if(!state.getAndSet(true))
…

Barttttt
- 253
- 1
- 6
- 15
3
votes
2 answers
Synchronizing on AtomicBoolean?
In an application I'm working on I found the following code snippet:
public class MyClass {
private AtomicBoolean atomicBoolean = new AtomicBoolean(false);
public void Execute() {
// Whole lot of business logic
// ....
…

Jdv
- 962
- 10
- 34
2
votes
2 answers
Is there a way to count accesing threads to a primitive variable?
I have a variable in my code, a simple primitive boolean x. Because of code complexity, I am not sure about the number of threads accessing it. Maybe it is never shared, or is used by only one thread, maybe not. If it is shared between threads, I…

Tristate
- 1,498
- 2
- 18
- 38
2
votes
3 answers
Replace AtomicBoolean by primitive type when only the get() and set() methods are used?
My code contains some AtomicBoolean fields. Only the get() and set() methods of these fields are called.
Can the types of these fields safely be replaced by primitive boolean?
I mean, assignment and access operations of primitive booleans are atomic…

eztam
- 3,443
- 7
- 36
- 54
2
votes
3 answers
Using AtomicBoolean instead of synchronized blocks
Say I have a class with 2 instance variables and the following methods (simplified for this question):
private final Object lock = new Object();
private boolean running;
public MyClass() {
synchronized(lock) {
running = false;
…

Evan LaHurd
- 977
- 2
- 14
- 27
2
votes
3 answers
Is this use of AtomicBoolean a valid replacement for synchronized blocks?
Consider two methods a() and b() that cannot be executed at the same time.
The synchronized key word can be used to achieve this as below. Can I achieve the same effect using AtomicBoolean as per the code below this?
final class SynchonizedAB…

newlogic
- 807
- 8
- 25
2
votes
4 answers
AtomicBoolean where is the lock?
AtomicBoolean uses native code for synchronization. How does it translate into java locks?
what's the difference between:
AtomicBoolean a = new AtomicBoolean();
synchronized (a) {
a.set(true);
}
vs:
a.set(true)
I know the synchronized(a) is not…

Lily
- 5,872
- 19
- 56
- 75
1
vote
1 answer
If 2+ front-ends execute the same request, how can I make it so the back-end only allows the first request and ignores the rest?
What am I trying to do?
When 2+ front-ends requests something via socket emit, I only want the back-end to process 1 of those requests and reject the other requests.
What is the code that currently tries to do that?
Backend code.
// Original…

ctrlaltdeleon
- 356
- 3
- 14
1
vote
2 answers
Why did Goetz not use volatile boolean for Listing 7.20 again?
Here is the code from listing 7.20 in Brian Goetz's Java Concurrency in Practice:
public class CheckForMail {
public boolean checkMail(Set hosts, long timeout, TimeUnit unit)
throws InterruptedException {
…

katiex7
- 863
- 12
- 23
1
vote
1 answer
How to make android digital clock flash?
I need to implement Digital clock for my layout. and I want the dots between hours and minutes to disappear and reappear every half of the second or so. I copied android clock code and changed it a bit. The idea was to change time formatting based…

RexSplode
- 1,475
- 1
- 16
- 24