Java Thread : Multithreading - Race condition -


i writing multi-threading program can accessed multiple user simultaneously , program has avoid race conditions.

code / multi-threading :

public class dataprocessor implements serializable, runnable {  private static final long serialversionuid = 1l;  public dataprocessor() {  }  @override public void run() {     process(); }  private void process() {      int isize = 5;      (int icounter = 0; icounter < isize; icounter++) {         datakey objdatakey = new datakey();         arraylist<string> list = //..fetch data method ()         hashmap<string, string> hmpqdata = //..fetch data method ()          sendnforgothelperthread helperthread = new sendnforgothelperthread(objdatakey, list, hmpqdata);         thread t = new thread(helperthread);         t.start();     }  }  class sendnforgothelperthread implements runnable {      private arraylist<string> list;     private hashmap<string, string> hmpqdata;     private datakey objdatakey;      public sendnforgothelperthread(datakey objdatakey, arraylist<string> list, hashmap<string, string> hmpqdata) {         this.list = list;         this.hmpqdata = hmpqdata;         this.objdatakey = objdatakey;     }      @override     public void run() {          try {              // option 1 : synchronized method - sendnforgothelperthread class object locking              datacollector objdatasenderm = new datacollector();             objdatasenderm.synchronizedmethodstore(this.objdatakey, this.list, this.hmpqdata);              // option 2 : synchronized block - sendnforgothelperthread class object locking              synchronized (this) {                 datacollector objdatasender = new datacollector();                 objdatasender.store(this.objdatakey, this.list, this.hmpqdata);             }              // option 3 : class level locking              synchronized (sendnforgothelperthread.class) {                 datacollector objdatasender = new datacollector();                 objdatasender.store(this.objdatakey, this.list, this.hmpqdata);             }          } catch (exception iex) {             system.out.println("exception in thread: " + iex.getmessage());         }     } }  class datacollector {      public void store(datakey objdatakey, arraylist<string> list, hashmap<string, string> hmpqdata) {          hashmap<string, string> retrivedvalue = (hashmap<string, string>) memcacheutil                 .retrievefrommemcache(objdatakey.getkey());          retrivedvalue.putall(hmpqdata);          memcacheutil.addtomemcache(objdatakey.getkey(), retrivedvalue, "exptime value");          // sending data in queue         senddatatoqueue(objdatakey, list, hmpqdata);      }      synchronized void synchronizedmethodstore(datakey objdatakey, arraylist<string> list,             hashmap<string, string> hmpqdata) {         store(objdatakey, list, hmpqdata);      }  }  class datakey {     private string key;      public string getkey() {         return key;     }      public void setkey(string key) {         this.key = key;     } }  public void senddatatoqueue(datakey objdatakey, arraylist<string> list, hashmap<string, string> hmpqdata) {     // sending data queue  } 

}

user 1 :

public class usera {  public static void main(string[] args) {     dataprocessor objdataprocessor = new dataprocessor();     thread thprocessorthread = new thread(objdataprocessor, "processor");     thprocessorthread.start(); } 

}

user 2 :

public class userb {  public static void main(string[] args) {     dataprocessor objdataprocessor = new dataprocessor();     thread thprocessorthread = new thread(objdataprocessor, "processor");     thprocessorthread.start(); } 

}

user & b call dataprocessor thread @ same time. clear option 1 & 2 face race conditions locking object of class / self class object locking , option 3 provide lock on class level - if multiple user access program @ same time option 3 slow application , whole purpose of multi-threading go on toss.

could 1 please advise read-up on how handle scenario.

edit :

could 1 please handle race condition on sendnforgothelperthread thread object - thread being called loop , each loop new thread sendnforgothelperthread being started.

you're passing 2 different instance of dataprocessor threads in class usera , userb, if start these main methods run normally. race-condition won't occur in application.

for race-condition occur have pass shared object, i.e. multiple threads operate on same object , shared objects should have field/attribute shared between multiple threads

    dataprocessor objdataprocessor = new dataprocessor();     thread thprocessorthread1 = new thread(objdataprocessor, "processor-1");     thprocessorthread1.start();     thread thprocessorthread2 = new thread(objdataprocessor, "processor-2");     thprocessorthread2.start(); 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -