Coordination Between Threads

   

Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Chapter 18.  Programming with Threads


The use of the interrupt() method and the synchronize modifier are crude forms of coordination between threads. There are others that are more sophisticated. The first one that we will cover is the join() method. The join() method comes in two flavors: one that accepts a timeout parameter and one that will wait forever. Listing 18.9 shows us an example of one thread waiting for the other to finish by the use of the join() method.

Listing 18.9 The TestJoin1.java File
 /*   * TestJoin1.java   *   * Created on September 25, 2002, 12:08 PM   */  package ch18;  /**   *   * @author  Stephen Potts   */  public class TestJoin1 extends Thread  {      /** Creates a new instance of TestJoin1 */      public TestJoin1()      {      }      public void run()      {          try          {               for ( int i=0; i<5; i++)              {                  System.out.println("running the first loop " + i);              }              Thread.sleep(1000);              for ( int i=6; i<10; i++)              {                  System.out.println("running the second loop" + i);              }          }catch (InterruptedException ie)          {              System.out.println("Sleep interrupted in run()");          }      }      public static void main(String[] args)       {          try          {              TestJoin1 ti = new TestJoin1();              Thread t = new Thread(ti);              t.start();              t.join();              for ( int i=11; i<15; i++)              {                  System.out.println("running the third loop" + i);              }          }catch (InterruptedException ie)          {              System.out.println("Join interrupted in run()");          }          System.out.println("Exiting from Main");      }  } 

This example is like some of the earlier examples in this chapter in that it loops and prints using two different threads. This version is different in that it waits until the first thread is complete before it allows the main() thread to complete.

 TestJoin1 ti = new TestJoin1();  Thread t = new Thread(ti);  t.start();  t.join(); 

The join() method call is executed against the thread that you want to keep running. You are telling the program to pause here pending the completion of the thread called t. Running this example produces the following output:

 running the first loop 0  running the first loop 1  running the first loop 2  running the first loop 3  running the first loop 4  running the second loop6  running the second loop7  running the second loop8  running the second loop9  running the third loop11  running the third loop12  running the third loop13  running the third loop14  Exiting from Main 

Notice how the loops completed in order. If we comment out the join() method call, the threads each proceed without regard to the other, giving a result that looks similar to this.

 running the third loop11  running the third loop12  running the third loop13  running the third loop14  Exiting from Main  running the first loop 0  running the first loop 1  running the first loop 2  running the first loop 3  running the first loop 4  running the second loop6  running the second loop7  running the second loop8  running the second loop9 

Notice that the first loop and indeed the whole main thread terminates before the new thread completes. This introduces an interesting point. Notice that the program stopped when all threads completed, not when the main thread did.

A variation on this example is to add a long numeric value to the join(), indicating how many milliseconds the thread should wait for the other thread to complete. Listing 18.10 shows a modification of the preceding example that "times out" after three seconds.

Example 18.10 LISITING 18.10 The TextJoin2.java File
 /*   * TestJoin2.java   *   * Created on September 25, 2002, 12:08 PM   */  package ch18;  /**    *   * @author  Stephen Potts   */  public class TestJoin2 extends Thread  {      /** Creates a new instance of TestJoin2 */      public TestJoin2()      {      }      public void run()      {          try          {              for ( int i=0; i<5; i++)              {                  System.out.println("running the first loop " + i);              }              Thread.sleep(5000);              for ( int i=6; i<10; i++)              {                  System.out.println("running the second loop" + i);              }          }catch (InterruptedException ie)          {              System.out.println("Sleep interrupted in run()");          }      }      public static void main(String[] args)       {          try          {              TestJoin2 t2 = new TestJoin2();              Thread t = new Thread(t2);              t.start();              t.join(3000);              for ( int i=11; i<15; i++)              {                  System.out.println("running the third loop" + i);              }          }catch (InterruptedException ie)          {              System.out.println("Join interrupted in run()");          }           System.out.println("Exiting from Main");      }  } 

In this example, the first loop executes while the join() is in effect.

 t.join(3000); 

The join() times out while the new thread is in a sleep state and prints loop three out. After the new thread wakes up, the second loop is printed. The result is shown here:

 running the first loop 0  running the first loop 1  running the first loop 2  running the first loop 3  running the first loop 4  running the third loop11  running the third loop12  running the third loop13  running the third loop14  Exiting from Main  running the second loop6  running the second loop7  running the second loop8  running the second loop9 

Notice how the third loop is executed first.


       
    Top
     



    Java 2 Primer Plus
    Java 2 Primer Plus
    ISBN: 0672324156
    EAN: 2147483647
    Year: 2001
    Pages: 332

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net