title: Sharing an int or an integer object between multiple threads in Java
body: <p>I have a multithreaded program that includes data structures like <code>ConcurrentHashMap</code> and <code>ConcurrentLinkedList</code>, however I also need every thread to access a shared integer value. The threads themselves are custom thread classes I've made that extend class <code>Thread</code>. My two concerns here are:</p>
  
  <ul>
  <li><p>How can I make each thread see the same integer value? I don't think a primitive int would work as it would not be "passed by reference" (or rather passed by value of the object pointer). The integer needs to be mutable and any changes to one integer by a thread needs to be seen by all the other threads. Would using an Integer Object fix this, what about <code>AtomicInteger</code>?</p></li>
  <li><p>What should I use to preserve thread safety? Each thread will check the integer everytime a loop in them runs but it will change the integer when a thread has finished it's task and is about to return.</p></li>
  </ul>
  
  <p>Thanks in advance! </p>