Skip to content

Tag: concurrency

Can another thread see an effectively immutable object in an inconsistent state if it is published with a volatile reference?

According to Java Concurrency in Action if we have the following class: and we initialise an instance of this class and publish it in a non-safe way (through a simple public field for example), then the assertCorrectness() might indeed throw an AssertionError, if called from another thread. In other words, this means that some another thread might see an up-to-date

How to fix the Issue with replacing AsyncTask to connect socket?

I’m trying to use This approach(Marked Answer) to replace my code with AsyncTask to make connection between user in android and server, And here’s what I’ve done : Note: I’m not using any specific pattern, just trying to achieve this goal on a basic structure. MainActivity : ClientConnection : ConnectionTask : ConnectionRunningTask: So base on the above code I’ll try

What does “Aren’t allocating the things put into queues” mean?

I’m reading this: In which Doug Lea says: Usually, when you are putting something into a queue, you will have just allocated that new something. And similarly, when you take something out you usually use it and then let it become garbage. In which case the extra allocation for a queue node is not going to make much difference

Why does calling CompletableFuture::cancel cause an immediate CancellationException

I am trying to call cancel on CompletableFuture. It seems from the docs: If not already completed, completes this CompletableFuture with a CancellationException. Dependent CompletableFutures that have not already completed will also complete exceptionally, with a CompletionException caused by this CancellationException. That it should complete them exceptionally which is what I was expecting but instead, it throws and immediate CancellationException.

Is this the correct way to extract counts from a Concurrent Hash Map without missing some or double counting?

Working on something where I’m trying to count the number of times something is happening. Instead of spamming the database with millions of calls, I’m trying to sum the updates in-memory and then dumping the results into the database once per second (so like turning 10 +1s into a single +10) I’ve noticed some strange inconsistency with the counts (like