6 Replies Latest reply: Aug 21, 2011 1:10 PM by 796440 RSS

    how can we control the executions of 4 threads

    832053
      Hi,

      I tried to create 4 threads and tried to run them in custimized manner but i m facing some problem please help me in thi regard.Below is the code which i wrote to schedule the threads in custimized may using wait and notify...

      public class MyThreadOne implements Runnable {

      String tName;
      Thread t;

      MyThreadOne(String threadName) {
      tName = threadName;
      t = new Thread(this, tName);
      t.start();
      }

      public void run() {
                // wait for me to be notified by main thread before continuing.
                synchronized (t) {
                          try {
                          wait();
                          } catch(InterruptedException e) {
                          throw new RuntimeException(e);
                          }

                }
                // after main thread notifies we run normally.


      System.out.println("Thread: " + tName);

      System.out.println("Terminating thread: " + tName);
      }
      public Thread getThread() {
      return this.t;
      }

      public static void main(String args[]) {

      // Why are the threads start and terminate randomly?
      MyThreadOne t1 = new MyThreadOne("1");
           MyThreadOne t2 = new MyThreadOne("2");
           MyThreadOne t3 = new MyThreadOne("3");
           MyThreadOne t4 = new MyThreadOne("4");
           // issue a notify to each thread to have them start up
           System.out.println(" notify");          //phani

                     synchronized(t1.getThread()) {
                                    System.out.println("synchronized ");          //phani
                     t1.getThread().notify();
                     }
                     // optional: Thread.sleep(50) here if we want to try to give some lead time...

                     synchronized(t2.getThread()) {
                     t2.getThread().notify();
                     }

                     synchronized(t3.getThread()) {
                     t3.getThread().notify();
                     }

                     synchronized(t4.getThread()) {
                     t4.getThread().notify();
                     }


           try {
           //NO -> Thread.sleep(10000);
           // now wait for each thread to exit.
           t1.getThread().join();
           t2.getThread().join();
           t3.getThread().join();
           t4.getThread().join();
           // we could also do t1.isAlive();
           } catch (InterruptedException e) {
           System.out.println(
           "Exception: Thread main interrupted.");
           }
           System.out.println(
           "Terminating thread: main thread.");

      }
      }

      Edited by: user13779505 on Jan 18, 2011 3:39 AM

      Edited by: user13779505 on Jan 18, 2011 9:23 PM

      Edited by: user13779505 on Jan 19, 2011 11:39 PM
        • 1. Re: how can we schedule the thread flow
          jtahlborn
          most likely your run methods are throwing illegalmonitorstate exceptions because you are not waiting on the same object that you are sunchronized on (synchronized on "t" but waiting on "this").
          • 2. Re: how can we schedule the thread flow
            802316
            You have a few race conditions.

            - It is possible for the threads to be notified before they have started. When this happens the notify is lost and the wait() waits forever.
            - When you notify a waiting threads it takes a small random time to wake up. This means that threads notified 1,2,3,4 could start 4,3,2,1 or any other combination.

            Thread work best when they can run independantly, in any order. You are trying to serialize multiple threads when the best way to serialize a set of tasks is to use one thread.
            • 3. Re: how can we schedule the thread flow
              832053
              if there is some correction in my program please correct it
              • 4. Re: how can we schedule the thread flow
                802316
                user13779505 wrote:
                if there is some correction in my program please correct it
                Thats hard to do as I assume you doing this as a learning exercise, meaning you have make the changes.

                I have already said the best way is to use a loop.

                When you get the code working it should do the same thing as the following. Why would you make it more complicated?
                for(int tName=1;tName<=4;tname++) 
                    System.out.println("Thread: " + tName + "\nTerminating thread: " + tName);
                System.out.println("Terminating thread: main thread.");
                • 5. Re: how can we control the executions of 4 threads
                  Johnny_hunter
                  A runnable implementation is a task, and a Thread is like a worker. The best practice and the natural way is to assign a task to a worker:
                  Thread t = new Thread(new Runnable()) 
                  In your code
                  MyThreadOne(String threadName) {
                            tName = threadName;
                            t = new Thread(this, tName);
                            t.start();
                       }
                  it looks to me as if a task "demand" a worker to work on itself, kind of awkward design if not wrong.

                  John
                  • 6. Re: how can we schedule the thread flow
                    796440
                    user13779505 wrote:
                    if there is some correction in my program please correct it
                    That's not the way it works. You've been given suggestions. Now it's your job to implement them, or ask for clarification if you're still confused.