Access to strings across multiple threads

I am looking for something here. I have a singleton class that contains a value that is updated every few seconds by a method inside this class. Right now, accessing this value for multiple threads is done through synchronization, which I would like to eliminate. Would that make sense?

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 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;
        }
    }
}

There may be many instances of a DataSegment to read from myValue, but the metrics class is singleton. myValue is updated every 5 seconds or so, and only MetricsUpdater is allowed to write to it. It makes sense?

, ? JUnit, DataSegment , , concurrency.

+4
3

.

1-

synchronized(myValue) {
    myValueSynchronized = someMethodToUpdateMyValue();
    myValue = myValueSynchronized;
    Thread.sleep(100); 
}

, myValue. , Thread.sleep(100) . , myValue , , . . , . . ReentrantLock String.

    public static MetricsUpdater getInstance() {
        if (theInstance == null) {
            theInstance = new MetricsUpdater();
        }
        return theInstance;
    }

Singleton . DCL (Double Checked Locking . ). MetricsUpdater theInstance = new MetricsUpdate();. ,

 sb.append(metrics.myValue); 

.

1 - , someMethodToUpdateMyValue

class MetricsUpdater {

    private static volatile MetricsUpdater theInstance;
    public volatile String myValue;

    /**
     * DCL . Please avoid
     * Better use 
     * private static MetricsUpdater theInstance = new MetricsUpdate();
     */
    public static MetricsUpdater getInstance() {
        if (theInstance == null) {
            synchronized(MetricsUpdate.class) {
                 if(theInstance == null) {
                     theInstance = new MetricsUpdater();
                 }
            }
        }
        return theInstance;
    }

    // this runs on a timer but to keep it simple I'll just define the method...
    // if your someMethodToUpdateMyValue is thread safe
    private void updateMetrics() {
            myValue = someMethodToUpdateMyValue();
    }
}

2: , someMethodToUpdateMyValue

, / myValue

class MetricsUpdater {

 private static volatile MetricsUpdater theInstance;
 public volatile String myValue;

 /**
 ** Use ReentrantLock instead
 */
 private final  Object lock  = new Object();


     /**
     * DCL . Please avoid
     * Better use 
     * private static MetricsUpdater theInstance = new MetricsUpdate();
     */
    public static MetricsUpdater getInstance() {
        if (theInstance == null) {
            synchronized(MetricsUpdate.class) {
                 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(lock) {
        myValue = someMethodToUpdateMyValue();
    }
}

}

+4

, , volatile ( - , java ). java , (-) , . , , ( volatile/locks/etc), .

+3

, myValue , myValue.

, myValue :

synchronized (metrics)
{
     sb.append(metrics.myValue); // this is the value that currently synchronized
}

:

synchronized(this) {
    // also, to keep things simple, I've replaced all of the actual logic with a method called someMethodToUpdateMyValue()
    myValueSynchronized = someMethodToUpdateMyValue();
    myValue = myValueSynchronized;
}

, myValueSynchronized. myValue, myValue .

+3

All Articles