Skip to content
Advertisement

Tag: concurrency

Java happens-before relationship?

Consider the following code. Thread A calls run(), then another thread B calls test() and there shouldn’t be any happens-before relationship. I know it is not guaranteed that thread B sees the changes which thread A made. But is it possible that the output of this program is: Answer Yes, it’s possible, because it’s not explicitly forbidden. The read of

Is it necessary to make `AtomicBoolean` also `volatile`?

My understanding: Declaring a variable volatile guarantees the visibility for other threads about writes to that variable. Essentially, every write to volatile variable happens-before subsequent reads. I understand the atomicity of AtomicBoolean.compareAndSet() and how it provides the atomicity of read+write operation that volatile doesn’t. But I don’t see any doc providing visibility guarantee by AtomicBoolean like the following: Every successful

How to run async bash command in java?

I’m trying to run an async bash command from a java file and wait for it to finish before I continue the java code execution. At this moment I’ve tried using Callable like so: and I call it like so: Thanks!!! Answer An easier way is to use Java 9+ .onExit(): If you want to block anyway, use .waitFor().

How to take items from queue in chunks?

I have mulitple producer threads that add objects to a shared queue concurrently. I want to create a singlethread consumer that reads from that shared queue for further data processing (database batch insert). Problem: I want to only take the data from the queue in chunks for better performance during batch insert. Thus I somehow have to detect how many

Implementing a resource read/write lock in Java

I’m trying to implement a simple read/write lock for a resource accessed concurrently by multiple threads. The workers randomly try reading or writing to a shared object. When a read lock is set, workers should not be able to write until the lock is released. When a write lock is set, read and write are not permitted. Although my implementation

Advertisement