java - i wanted the threads to work in sequence of there creation but am not able to do so using this code suggestions please -


i wanted code run threads in sequence 1 2 three,one 2 3 , on run in random order tried many ways setting priority nothing seems work 3 threads run in random order not desired output

class thrd extends thread { thread t;  thrd(string name)     {     t=new thread(this,name);     system.out.println(name +":created"+t);     }  @override  public void run(){     try     {         for(int i=1;i<=5;i++)         {             system.out.println(t.getname()+": "+i);             thread.sleep(500);         }     }     catch(interruptedexception e)    {         system.out.println("interrupted"+e);     } }  }  public class threadse {  public static void main(string[] args)    {     thrd o1=new thrd("one");     thrd o2=new thrd("two");     thrd o3=new thrd("three");      o1.t.start();     o2.t.start();     o3.t.start();      system.out.println("thread 1 alive: "+o1.t.isalive());     system.out.println("thread 2 alive: "+o2.t.isalive());     system.out.println("thread 3 alive: "+o3.t.isalive());          try         {          system.out.println("waiting");        o1.t.join();        o2.t.join();        o3.t.join();          }     catch(interruptedexception e)         {         system.out.println("interrupted: "+e);         }      system.out.println("thread 1 alive: "+o1.t.isalive());     system.out.println("thread 2 alive: "+o2.t.isalive());     system.out.println("thread 3 alive: "+o3.t.isalive());     } } 

you have maintain sequence threads perform work , make work critical section. critical section few lines of code on 1 thread can operate @ time here want strict sequential mode threads have make sure::

inside loop thread executes turn there using sequencecount variable . refer /* comments */ inside code more clarification.

class thrd extends thread{        /* sequence counter make execution of threads sequentially */     static int sequencecount=0;     thread t;    public thrd(string name){       t=new thread(this,name);       system.out.println(name+" created");   }    @override   public void run(){       try{            for(int i=0;i<5;i++){               /* wait here if not current thread's turn */               while(true){                   /* if 1st thread's turn , current thread first thread break */                 if((sequencecount==0)&&(t.getname().equals("one")))break;                 /* in way other threads wait it not turn */                 else if((sequencecount==1)&&(t.getname().equals("two")))break;                 else if((sequencecount==2)&&(t.getname().equals("three")))break;             }                 system.out.println(t.getname()+" : "+i);             thread.sleep(500);             /* if last thread printed out move 1st thread..   */             if(sequencecount==2)sequencecount=0;             /* otherwise make turn of next thread */             else sequencecount++;         }      }catch(exception er){er.printstacktrace();} }  }  public class threadse {    public static void main(string[] args)   {    thrd o1=new thrd("one");    thrd o2=new thrd("two");    thrd o3=new thrd("three");     o1.t.start();    o2.t.start();    o3.t.start();   } } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -