Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could, as mentioned in other posts, synchronize on the class or on an explicit monitor.</p> <p>There are 2 other ways, if we assume that your are using the sychnronize only for thread-safe getting and setting of the property: <code>volatile</code> and <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/atomic/AtomicReference.html" rel="nofollow noreferrer">AtomicReference</a>.</p> <p><strong>volatile</strong></p> <p>The <code>volatile</code> keyword will make access to the variable atomic, meaning that reading and assigning the variable won't be optimized by the CPUs local registers and are done atomically.</p> <p><strong>AtomicReference</strong></p> <p>The AtomicReference is a special class at the <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/atomic/package-summary.html" rel="nofollow noreferrer">java.util.concurrent.atomic</a> package, which allows atomic access to a variable-like reference. It is very similiar to <code>volatile</code>, but gives you some additional atomic operations, like compareAndSet. </p> <p>Example:</p> <pre><code>public class PayloadService extends Service { private static final AtomicReference&lt;PayloadDTO&gt; payload = new AtomicReference&lt;PayloadDTO&gt;(new PayloadDTO()); public static void setPayload(PayloadDTO payload){ PayloadService.payload.set(payload); } public static PayloadDTO getPayload() { return PayloadService.payload.get ; } </code></pre> <p><strong>Edit:</strong></p> <p>Your Holder seems quite confused, since you are instantiating classes only to call static Methods. A try to get it fixed with AtomicReference:</p> <pre><code>public class PayloadHolder { private static AtomicReference&lt;PayloadHolder&gt; holder = new AtomicReference&lt;PayloadHolder(); //This should be fetched through the holder instance, so no static private AtomicReference&lt;PayloadDTO&gt; payload = new AtomicReference&lt;PayloadDTO&gt;(); private PayloadHolder(){ } public static PayloadHolder getInstance(){ PayloadHolder instance = holder.get(); //Check if there's already an instance if(instance == null){ //Try to set a new PayloadHolder - if no one set it already. holder.compareAndSet(null, new PayloadHolder()); instance = holder.get(); } return instance; } public void initPayload(){ payload.set(new PayloadDTO()); //Alternative to prevent a second init: //payload.compareAndSet(null, new PayloadDTO()); } public PayloadDTO getPayload() { return payload.get; } public void setPayload(PayloadDTO p) { payload.set(p); } } public class PayloadService extends Service { private final PayloadHolder payloadHolder = PayloadHolder.getInstance(); public void initPayload(){ payloadHolder.initPayload(); } public void setPayload(PayloadDTO payload){ payloadHolder.setPayload(payload); } public PayloadDTO getPayload() { return payloadHolder.getPayload(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload