user592748
user592748

Reputation: 1234

Is this function atomic?

I have a use case where I need to add values to an underlying datastructure and the order needs to be maintained. I am using ConcurrentLinkedQueue as the underlying datastructure. Following is the function

public void put(V value) {
   concurrentLinkedQueue.add(value); 
}

Is this statement atomic, in the sense if two threads are trying to put values, Thread A first (value V1) and Thread B second (value V2), is there a possibility V2 will be added first and V1 later.

Upvotes: 0

Views: 94

Answers (2)

Sampath
Sampath

Reputation: 1202

Background: ConcurrentLinkedQueue is thread safe in implementation. And it is FIFO. So it will maintain the insertion order.

The answers to your questions:

Is this statement atomic

Yes

Is there a possibility V2 will be added first and V1 later

Yes

If you want the Thread A to insert value before the Thread B, then you need to implement routines to explicitly handle the insertion order. Atomicity of the add operation in ConcurrentLinkedQueue has nothing to do with it. It all depends on which thread calls the add() method first.

If you want to maintain order (i.e. - FIFO) multi threading could not be your answer. May I ask why do you need multi-threaded execution? I can think of a few situations which you will need multi-threading with FIFO order. Simply because you are trying to process ordered data in parallel which cannot be guaranteed to finish processing in the same order.

If by chance it is absolutely necessary to user parallel processing you can use a popular thread configuration like the following:

  1. You can have a Manager-worker system where the manager thread obtains the job, and delegates it to a pool of worker threads which will do the processing and re-sequence the results based on some property in the job's data structure (i.e.- timestamp, sequence number).

enter image description here

  1. You can have a delegator thread which will obtains the job, and delegates it to a pool of worker threads which will do the processing and submit the results to another thread which will perform the re-sequencing based on some property in the job's data structure (i.e.- timestamp, sequence number).

enter image description here

Inside the Manager or Output Sequencer threads you may have to maintain data buffers which will hold the data before sequenced for dispatching.

As you can see, an implementation like this could be quite complex. So you have to ask yourself, 'do you really need to process the data in parallel?'.

Upvotes: 1

aioobe
aioobe

Reputation: 420951

The answer is that your question is kind of meaningless.

If two threads call put in an unsynchronized way, then there's no observable difference between

Thread 1     Thread 2

put          
add
             put
             add

and

Thread 1    Thread 2

put          
             put
             add
add

Even if you make the method synchronized you'll have a data race in your program unless you introduce proper happens-before relations between the two actions.

Ask your self why the order in which the threads call put is important, and make sure that this intention is captured by proper synchronization.

Upvotes: 3

Related Questions