Java
  Home arrow Java arrow Page 3 - The Basics of Multiple Threads in Java
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 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? 
Google.com  
JAVA

The Basics of Multiple Threads in Java
By: Barzan "Tony" Antal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 12
    2009-02-25


    Table of Contents:
  • The Basics of Multiple Threads in Java
  • The Basics and the Thread class
  • The Runnable Interface
  • Taking a Break!

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

     
     
    ADVERTISEMENT


    The Basics of Multiple Threads in Java - The Runnable Interface
    ( Page 3 of 4 )

     

    On the previous page we learned the first solution to multi-threading; it covered extending the Thread class. The Java programming language does not allow multiple inheritances. This is the major pitfall of this solution. With HTML apps, it is mandatory to extend the Applet class, thus the one and only allowed inheritance is already consumed. So this would mean no chance whatsoever for multi-threading. 

    This is why the other solution, which implements the Runnable interface, was designed. The Runnable interface defines only one method, and that's the run() method. Once again we need to code this method when implementing the Runnable interface. Right after, in order to create multiple threads, we first need to create Runnable object instances, and then we're going to pass-by-value the objects to the Thread constructor.

    Check out the example below. 

    class MyRunnable implements Runnable{

    private int a;

     

    public MyRunnable(int a){

    this.a = a;

    }

     

    public void run(){

    for (int i = 1; i <= a; ++i){

    System.out.println(Thread.currentThread().getName() + " is " + i);

    try{

    Thread.sleep(1000);

    }

    catch (InterruptedException e){}

    }

    }

    }

     

    class MainMyThread{

    public static void main(String args[]){

    MyRunnable thr1, thr2;

    thr1 = new MyRunnable(5);

    thr2 = new MyRunnable(10);

    Thread t1 = new Thread(thr1);

    Thread t2 = new Thread(thr2);

    t1.start();

    t2.start();

    }

    }

    The output will be the same as earlier. 

    Thread-0 is 1

    Thread-1 is 1

    Thread-0 is 2

    Thread-1 is 2

    Thread-0 is 3

    Thread-1 is 3

    Thread-0 is 4

    Thread-1 is 4

    Thread-0 is 5

    Thread-1 is 5

    Thread-1 is 6

    Thread-1 is 7

    Thread-1 is 8

    Thread-1 is 9

    Thread-1 is 10

    All right, so this second solution to creating new threads by implementing the Runnable interface is the optimal way to do this when multiple inheritances are required. The earlier solution is a bit easier to use since it doesn't require that much work. With the Runnable interface implementation we need to declare Thread objects, initialize the Runnable object instances, and call the Thread's constructor on them. 

    Until now we've learned that both solutions execute a thread using the start() method. This is critical! Lots of beginners fall into the trap of explicitly calling the run() method of a thread object. This is wrong-it won't execute the thread as a new one; instead, everything will be processed via the conventional solo-thread approach. 

    Therefore, the proper way is to always start a thread using the start() method. At its end, this creates the new thread and right after calls the run() method, just like any other method, but in that newly- created thread. This is the key. Always be careful. 



     
     
    >>> More Java Articles          >>> More By Barzan "Tony" Antal
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek