title: accessing a String across multiple threads
body: <p>I'm looking for some input here.  I have a singleton class that contains a value which is updated every few seconds by a method within that class.  Right now, access to this value across multiple threads is done via synchronization, which I would like to eliminate.  Would this make sense?</p>
  
  <pre><code>class DataSegment {
  
      private MetricsUpdater metrics = new MetricsUpdater.getInstance();
  
      public String printValues() {
          StringBuilder sb = new StringBuilder();
          sb.append(value1);
          sb.append(morevalues);
          sb.append(metrics.myValue); // this is the value that's currently synchronized
          return sb.toString();
      }
  }
  
  
  class MetricsUpdater {
  
      private String myValueSynchronized;
      public String myValue;
  
      public static MetricsUpdater getInstance() {
          if (theInstance == null) {
              theInstance = new MetricsUpdater();
          }
          return theInstance;
      }
  
      // this runs on a timer but to keep it simple I'll just define the method...
      private void updateMetrics() {
  
          synchronized(myValue) {
              // also, to keep things simple, I've replaced all of the actual logic with a method called someMethodToUpdateMyValue()
              myValueSynchronized = someMethodToUpdateMyValue();
              myValue = myValueSynchronized;
          }
      }
  }
  </code></pre>
  
  <p>There can be many instances of DataSegment all reading from myValue, but the metrics class is a singleton.  myValue only updates every 5 seconds or so and only MetricsUpdater is allowed to write to it.  Does that make sense?</p>
  
  <p>Does it even need to be synchronized at all if all of the other threads are only allowed to read it?  I've run a boatload of JUnit tests on this, creating many instances of the DataSegment class all printing values like crazy and I have yet to see any concurrency issues.</p>