java - question about singleton classes and threads -


i'm trying learn singleton classes , how can used in application keep thread safe. let's suppose have singleton class called indexupdater reference obtained follows:

 public static synchronized indexupdater getindexupdater() {     if (ref == null)         // it's ok, can call constructor         ref = new indexupdater();     return ref; }  private static indexupdater ref; 

let's suppose there other methods in class actual work (update indicies, etc.). i'm trying understand how accessing , using singleton work 2 threads. let's suppose in time 1, thread 1 gets reference class, through call indexupdater iu = indexupdater.getindexupdater(); then, in time 2, using reference iu, method within class called iu.updateindex thread 1. happen in time 2, second thread tries reference class. , access methods within singleton or prevented long first thread has active reference class. i'm assuming latter (or else how work?) i'd make sure before implement.

thank you,

elliott

since getindexupdater() synchronized method, prevents threads accessing method (or method protected same synchronizer) simultaneously. problem if other threads accessing object's methods @ same time. keep in mind if thread running synchronized method, other threads trying run synchronized methods on same object blocked.

more info on: http://download.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -