title: Thread can't read instances that are being modified by other threads
body: <p>This scheme shows the functions that are being discussed below...
  <a href="https://i.stack.imgur.com/81bHa.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/81bHa.jpg" alt="Concurrency problem being discussed." /></a></p>
  <p>The <code>OrderBook</code> class looks like this:</p>
  <pre><code>public class OrderBook {
  
      private TreeMap&lt;Double, Double&gt; bids;
      private TreeMap&lt;Double, Double&gt; asks;
      private Entry&lt;Double, Double&gt; bestBid;
      private Entry&lt;Double, Double&gt; bestAsk;
  
      public OrderBook() {
          this.bids = new TreeMap&lt;&gt;();
          this.asks = new TreeMap&lt;&gt;();
      }
      
      // Getters and setters...
      
      // Example function that modifies its variables...
      public void updateBids(double bidPrice, double bidVol) {
          if(this.getBids().containsKey(bidPrice)) {
              if(bidVol == 0.0) {
                  //System.out.println(&quot;Vol. 0: &quot; + bidPrice + &quot; - &quot; + bidVol);
                  this.getBids().remove(bidPrice);
              }
              else if(bidVol &gt; 0.0) {
                  //System.out.println(&quot;Actualizar Vol.: &quot; + bidPrice + &quot; - &quot; + bidVol);
                  this.getBids().replace(bidPrice, bidVol);
              }
              else {
                  //System.out.println(&quot;Error. Unexpected volume:&quot; + 
                  //        bidPrice + &quot; - &quot; + vol);
              }
          }
          else {
              // ...
          }
          this.setBestBid(this.getBids().lastEntry());
      }
  }
  </code></pre>
  <p><code>Client 1</code> class and <code>Client 2</code> class are different from each other (they perform different write operations over the their <code>OrderBook</code> class) and they are launched from different threads. A <code>Client</code> class looks like this:</p>
  <pre><code>public class Client1 extends WebSocketClient {
      
      private OrderBook ob;
      
      public Client1(URI serverURI, OrderBook ob) {
          super(serverURI);
          this.ob = ob;
      }
     
      // Extended class implementations...
  
      @Override
      public void onMessage(String message) {
          parse(message);
      }
      
      private void parse(String msg) {
          JSONObject json = new JSONObject(msg);
          
          if(json.has(&quot;b&quot;)) {
              double b = json.getDouble(&quot;b&quot;);
              double a = json.getDouble(&quot;a&quot;);
              double B = json.getDouble(&quot;B&quot;);
              double A = json.getDouble(&quot;A&quot;);
              
              /**
               * HERE performs the modification of the OrderBook class passed in the
               * constructor. I don't know if this synchronized block is needed...
               */
              synchronized(this.ob) {
                  this.ob.setBestBid(new AbstractMap.SimpleEntry&lt;&gt;(bidPrice, bidVol));
                  this.ob.setBestAsk(new AbstractMap.SimpleEntry&lt;&gt;(askPrice, askVol));
              }
          } 
      }
  }
  </code></pre>
  <p>The problem comes when in the <code>Main</code> class (launched in another thread), I try to read the updated instances of the class <code>OrderBook</code> that are being modified by the <code>Client x</code> classes...</p>
  <p><code>Main</code> class looks like this...</p>
  <pre><code>public class Main implements Runnable {
      
      private OrderBook ob1;
      private OrderBook ob2;
      
      public Oportunity(OrderBook ob1, OrderBook ob2) throws URISyntaxException {
          this.ob1 = ob1;
          this.ob2 = ob2;
      } 
      @Override
      public void run() {
          while(true) {
              // PROBLEM HERE: doesn't show anything...
              System.out.println(this.ob1.getLasValue());
              System.out.println(this.ob2.getLasValue());
          }   
      }
  
      public static void main(String[] args) throws Throwable {
      
          OrderBook ob1 = new OrderBook();
          OrderBook ob2 = new OrderBook();
      
          Thread client1 = new Thread(new Client1(new URI(&quot;...&quot;), ob1));
          Thread client2 = new Thread(new Client2(new URI(&quot;...&quot;), ob2));
          
          Thread m = new Thread(new Main(ob1, ob2));
      
          client1.start();
          client2.start();
      
          m.start();
      
      }
  }
  </code></pre>
  <p><strong>QUESTIONS:</strong></p>
  <ol>
  <li>How could I access consistently the last updated values of both instances of <code>OrderBook</code>?</li>
  <li>Also, is it possible to show priority on write operations over read operations?</li>
  </ol>