Explicit odd int (s) visible after build

I have a java class with a non-finite int variable that I explicitly initialized in the constructor to 0. All other variable calls are controlled by ReentrantLock. Should I worry that the threads will not see the initial value of 0, because I did not use the lock in the constructor?

+5
source share
2 answers

Yes, you have to worry. To avoid problems in this case, you need to securely publish an object reference.

From Java Concurrency in practice :

, , . :

  • ;
  • volatile AtomicReference;
  • ;
  • , .

() , new (- ).

, , 0 , , , (JLS ยง17.4.4):

  • (, ) - . , ,
+9

Java Concurrency :

, , , .

, . "-". , , Concurrency. . .

, :

, . :

;

volatile field AtomicReference;

;

. , " ".

, , , (, , LinkedBlockingQueue ..).

, int ( 64- , long) , , "" , - - . , ( , , , , , ).

, , 0, .

+2

All Articles