java - Threads execute not at the same time -


i have 3 threads, each thread have manipulation instance(q) of same class (q), periodically (that's why use thread.sleep() in method somecheck). main task make thread execute not @ same time, @ 1 time can execute 1 thread. tried put content of run method each thread synchronized (q){}, not understand put notify , wait methods.

class q {         boolean somecheck(int threadsleeptime){         //somecheck__section, if want stop thread - return false;              try{             thread.sleep(threadsleeptime);             } catch (interruptedexception e) {                             }             return true;         } }    class threadfirst extends thread  {     private q q;     threadfirst(q q){this.q=q;}      public void run(){         do{             //working object of class q         }         while(q.somecheck(10));     } }  class threadsecond extends thread  {     private q q;     threadsecond(q q){this.q=q;}      public void run(){         do{             //working object of class q         }         while(q.somecheck(15));     } }  class threadthird extends thread  {      private q q;     threadthird(q q){this.q=q;}      public void run(){         do{             //working object of class q         }         while(q.somecheck(20));     } }  class run{     public static void main(string[] args) {         q q = new q();         threadfirst t1 = new threadfirst(q);         threadsecond t2 = new threadsecond(q);         threadthird t3 = new threadthird(q);         t1.start();         t2.start();         t3.start();     } } 

you don't need put notify() , wait() methods if use synchronized blocks inside of methods, example:

class threadfirst extends thread {     ...     public void run() {         synchronized (q) {             //your loop here         }     }     ... } 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -