Home arrow Java arrow Page 6 - Multithreading in Java
JAVA

Multithreading in Java


Multithreading allows two parts of the same program to run concurrently. This article discusses how to pull off this performance-improving feat in Java. It is excerpted from chapter 10 of the book Java Demystified, written by Jim Keogh (McGraw-Hill/Osborne, 2004; ISBN: 0072254548).

Author Info:
By: McGraw-Hill/Osborne
Rating: 4 stars4 stars4 stars4 stars4 stars / 535
August 04, 2005
TABLE OF CONTENTS:
  1. · Multithreading in Java
  2. · Overhead
  3. · The Thread Classes and the Runnable Interface
  4. · Creating Your Own Thread
  5. · Creating a Thread by Using extends
  6. · Using isAlive() and join()
  7. · Setting Thread Priorities
  8. · Synchronizing Threads
  9. · Using the Synchronized Statement
  10. · Suspending and Resuming Threads

print this article
SEARCH DEVARTICLES

Multithreading in Java - Using isAlive() and join()
(Page 6 of 10 )

Typically, the main thread is the last thread to finish in a program. However, there isn’t any guarantee that the main thread won’t finish before a child thread finishes. In the previous example, we told the main method to sleep until the child threads terminate. However, we estimated the time it takes for the child threads to complete processing. If our estimate was too short, a child thread could terminate after the main thread terminates. Therefore, the sleep technique isn’t the best one to use to guarantee that the main thread terminates last.

Programmers use two other techniques to ensure that the main thread is the last thread to terminate. These techniques involve calling the isAlive() method and the join() method. Both of these methods are defined in the Thread class.

The isAlive() method determines whether a thread is still running. If it is, the isAlive() method returns a Boolean true value; otherwise, a Boolean false is returned. You can use the isAlive() method to examine whether a child thread continues to run. The join() method works differently than the isAlive() method. The join() method waits until the child thread terminates and “joins” the main thread. In addition, you can use the join() method to specify the amount of time you want to wait for a child thread to terminate.

The following example illustrates how to use the isAlive() method and the join() method in your program. This example is nearly the same as the previous example. The difference lies in the main() method of the Demo class definition.

After the threads are declared using the constructor of the MyThread class, the isAlive() method is called for each thread. The value returned by the isAlive() method is then displayed on the screen. Next, the join() method is called for each thread. The join() method causes the main thread to wait for all child threads to complete processing before the main thread terminates.

Here is what is displayed on the screen when this program runs:

Thread Status: Alive
Thread 1: true
Thread 2: true
Thread 3: true
Thread 4: true
Threads Joining.
Thread: 1
Thread: 2
Thread: 3
Thread: 4
Terminating thread: 1
Terminating thread: 2
Terminating thread: 3
Terminating thread: 4
Thread Status: Alive
Thread 1: false
Thread 2: false
Thread 3: false
Thread 4: false
Terminating thread: main thread.
class MyThread implements Runnable {
  String tName;
  Thread t;
  MyThread (String threadName) {
    
tName = threadName;
     t = new Thread (this, tName);
     t.start();
  }
  public void run() {
    
try {
         System.out.println("Thread: " + tName );
         Thread.sleep(2000);
    
} catch (InterruptedException e ) {
       System.out.println("Exception: Thread "
               
+ tName + " interrupted");
     }
     System.out.println("Terminating thread: " + tName );
 
}
}
class Demo {
   public static void main (String args []) {
      MyThread thread1 = new MyThread ("1");
      MyThread thread2 = new MyThread ("2");
      MyThread thread3 = new MyThread ("3");
      MyThread thread4 = new MyThread ("4");
      System.out.println("Thread Status: Alive");
      System.out.println("Thread 1: "
               
+ thread1.t.isAlive());
      System.out.println("Thread 2: "
               
+ thread2.t.isAlive());
      System.out.println("Thread 3: "
               
+ thread3.t.isAlive());
      System.out.println("Thread 4: "
               
+ thread4.t.isAlive());
     
try {
         System.out.println("Threads Joining.");
         thread1.t.join();
         thread2.t.join();
         thread3.t.join();
         thread4.t.join();
     
} catch (InterruptedException e) {
        System.out.println(
                 
"Exception: Thread main interrupted.");
      }
      System.out.println("Thread Status: Alive");
      System.out.println("Thread 1: "
               
+ thread1.t.isAlive());
      System.out.println("Thread 2: "
               
+ thread2.t.isAlive());
      System.out.println("Thread 3: "
               
+ thread3.t.isAlive());
      System.out.println("Thread 4: "
               
+ thread4.t.isAlive());
      System.out.println(
                "Terminating thread: main thread."); 
 
}
}


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials