Non-volatile reference for head and tail in Java LinkedBlockingQueue

Why java.util.concurrent.LinkedBlockingQueuehas a link headand tailhow not volatile?

Any change in heador tailin one thread may not be visible to another thread, so will this lead to problems?

+4
source share
3 answers

headand tailwill be protected putLockor takeLock. As long as your synchronization is appropriate, you do not need to declare fields as volatile.

So, to answer your question. Fields should not be volatileif they are correctly synchronized (they are).

, , , , .

+8

: , .

: , .. - synchronized {} . synchronized {} .

0

java.util.concurrent.LinkedBlockingQueue

, volatile - , - , volatile , . , , .

-, volatile . , . SO - .

, , volatile - , , type - , . , , .

0

Source: https://habr.com/ru/post/1657282/


All Articles