Java
  Home arrow Java arrow Page 3 - Multithreading in Java
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Download TestComplete 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
eWeek
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Multithreading in Java
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 191
    2005-08-04

    Table of Contents:
  • Multithreading in Java
  • Overhead
  • The Thread Classes and the Runnable Interface
  • Creating Your Own Thread
  • Creating a Thread by Using extends
  • Using isAlive() and join()
  • Setting Thread Priorities
  • Synchronizing Threads
  • Using the Synchronized Statement
  • Suspending and Resuming Threads

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Multithreading in Java - The Thread Classes and the Runnable Interface
    (Page 3 of 10 )

    You construct threads by using the Thread class and the Runnable interface. This means that your class must extend the Thread class or implement the Runnable interface. The Thread class defines the methods you use to manage threads. Table 10-1 contains the commonly used methods of the Thread class. You’ll see how these are used throughout the examples in this chapter.

    Method

    Description

    getName()

    Returns the name of the thread.

    getPriority()

    Returns the priority of the thread.

    isAlive()

    Determines whether the thread is running.

    join()

    Pauses until the thread terminates.

    run()

    The entry point into the thread.

    sleep()

    Suspends a thread. This method enables you to specify the period

    the thread is suspended.

    start()

    Starts the thread.

    Table 10-1  Commonly Used Methods Defined in the Thread Class

    The Main Thread

    Every Java program has one thread, even if you don’t create any threads. This thread is called the main thread because it is the thread that executes when you start your program. The main thread spawns threads that you create. These are called child threads. The main thread is always the last thread to finish executing because typically the main thread needs to release a resource used by the program such as network connections.

    Programmers can control the main thread by first creating a Thread object and then using method members of the Thread object to control the main thread. You create a Thread object by calling the currentThread() method. The currentThread() method returns a reference to the thread. You then use this reference to control the main thread just like you control any thread, which you’ll learn how to do in this chapter.

    Let’s create a reference to the main thread and then change the name of the thread from main to Demo Thread. The following program shows how this is done. Here’s what is displayed on the screen when the program runs:

    Current thread: Thread[main, 5,main]
    Renamed Thread: Thread[Demo Thread, 5,main]
    class Demo {
      
    public static void main (String args[] ) {
         
    Thread t = Thread.currentThread();
         
    System.out.println("Current thread: " + t);
         
    t.setName("Demo Thread");
         
    System.out.println("Renamed Thread: " + t);
      
    }
    }

    As you previously learned in this chapter, a thread is automatically created when you execute a program. The objective of this example is to declare a reference to a thread and then assign that reference a reference to the main thread. This is done in the first statement of the main() method.

    We declare the reference by specifying the name of the class and the name for the reference, which is done in the following line of code:

    Thread t

    We acquire a reference to the main thread by calling the currentThread() method member of the Thread class using the following method call:

    Thread.currentThread()

    The reference returned by the currentThread() method is then assigned to the reference previously declared in the opening statement. We then display the thread on the screen:

    Thread[main, 5,main]

    Information within the square brackets tells us something about the thread. The first appearance of the word main is the name of the thread. The number 5 is the thread’s priority, which is normal priority. The priority ranges from 1 to 10, where 1 is the lowest priority and 10 is the highest. The last occurrence of the word main is the name of the group of threads with which the thread belongs. A thread group is a data structure used to control the state of a collection of threads. You don’t need to be concerned about a thread group because the Java run-time environment handles this.

    The setName() method is then called to illustrate how you have control over the main thread of your program. The setName() method is a method member of the Thread class and is used to change the name of a thread. This example uses the setName() method to change the main thread’s name from main to Demo Thread. The thread is once again displayed on the screen to show that the name has been changed. Here’s what is displayed:

    Renamed Thread: Thread[Demo Thread, 5,main]

    More Java Articles
    More By McGraw-Hill/Osborne


       · Keep up the Good work sir.Thanks Kiran
       · This is the best introduction to multithreading i ever had......Thanks a...
       · Hello sir, this is the best online tutorial on multithreading, I have seen many but...
       · Hi,This is really a good tutorial on multithreading concept for begineers. It...
       · It is very easy to understand,with simple language.I.V.N.Venu
       · Example in chapter Five is not so Elucidate.I.V.N.Venu
       · Great job....Thanks alot ...Sanesh
       · sir i have read u r notes it's very easy to understand,very good sir
       · This is trully a very good article as it covers in one piece all the important...
       · Why does all the indian programmers say "sir"? Guys you r not in a school and the...
       · Good Job.It helps me a lot...Keep Going
       · The Hindi Language has an equivalent of "sir" that's used to address any stranger as...
       · This is the best platform from where I can learnmultithreading,thanks..Paresh
       · It is very easy to learn
       · it is very useful n connecting the sources indeed
       · Nice job..Good Article...Very nice..Rakesh..
       · The tutorial is nice, but in chapter 9, when I tried to run the program with wait()...
       · this is very good and easy to read and understand,but the whole data of threads...
       · I found good matter on multithreadig which help me a lot to understand...
       · Please check the sample example.After some analysis, I found the busy flag is...
       · Hi ,It is not much because of the language, its got more to do the with the...
     

    Buy this book now. This article is excerpted from chapter 10 of the book Java Demystified, written by Jim Keogh (McGraw-Hill/Osborne, 2004; ISBN: 0072254548). Check it out at your favorite bookstore today. Buy this book now.

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...

    Iron Speed





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway