Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non volatile head and tail reference in Java LinkedBlockingQueue

Why does java.util.concurrent.LinkedBlockingQueue have a head and tail reference as non volatile?

Any change in head or tail in one thread may not be visible to another thread, so will this lead to issues?

like image 408
user1846749 Avatar asked Feb 05 '23 23:02

user1846749


1 Answers

The head and tail are going to be guarded by the putLock or takeLock. As long as your synchronize appropriately, you don't need to declare the fields as volatile.

So, to answer your question. The fields don't need to be volatile if they are correctly synchronized (which they are).

If there is a part of the code you find suspect, let me know, otherwise I cannot find any reason they need to be volatile.

like image 165
John Vint Avatar answered Feb 08 '23 12:02

John Vint