Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Creation of Multiple Threads 
 

Like creation of a single thread, You can also create more than one thread (multithreads) in a program using class Thread or implementing interface Runnable.

 

Creation of Multiple Threads

                         

Like creation of a single thread, You can also create more than one thread (multithreads) in a program using class Thread or implementing interface Runnable.

Lets see an example having the implementation of the multithreads by extending Thread Class:

 class MyThread extends Thread{
  MyThread(String s){
    super(s);
    start();
  }
  public void run(){
    for(int i=0;i<5;i++){
      System.out.println("Thread Name  :"
             
+Thread.currentThread().getName());
      try{
        Thread.sleep(1000);
      }catch(Exception e){}
    }
  }
}
  public class MultiThread1{
  public static void main(String args[]){
    System.out.println("Thread Name :"
           +Thread.currentThread().getName());   

    MyThread m1=new MyThread("My Thread 1");
    MyThread m2=new MyThread("My Thread 2");
  }
}


Output of the Program

C:\nisha>javac MultiThread1.java

C:\nisha>java MultiThread1
Thread Name :main
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 1
Thread Name :My Thread 2

In this program, two threads are created along with the "main" thread. The currentThread() method of the Thread class returns a reference to the  currently executing thread and the getName( ) method returns the name of the thread. The sleep( ) method pauses execution of the current thread for 1000 milliseconds(1 second) and switches to the another threads to execute it. At the time of execution of the program, both threads are registered with the thread scheduler and the CPU scheduler executes them one by one.

Download this Program

Now, lets create the same program implenting the Runnable interface:

 

class MyThread1 implements Runnable{
  Thread t;
  MyThread1(String s)  {
    t=new Thread(this,s);
    t.start();
  }
  
  public void run()  {
    for(int i=0;i<5;i++) {
      System.out.println("Thread Name  :"+Thread.currentThread().getName());
      try {
      Thread.sleep(1000);
      }catch(Exception e){}
    }
  }
}

public class RunnableThread1{
  public static void main(String args[])  {
    System.out.println("Thread Name :"+Thread.currentThread().getName());   
    MyThread1 m1=new MyThread1("My Thread 1");
    MyThread1 m2=new MyThread1("My Thread 2");
  }
}

Output of the program:

C:\nisha>javac RunnableThread1.java

C:\nisha>java RunnableThread1
Thread Name :main
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 2
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 1

Note that, this program gives the same output as the output of the previous example. It means, you can use either class Thread or interface Runnable to implement thread in your program.

Download this Program

                         

» View all related tutorials
Related Tags: java c class interface object io read thread int this creation extend run lan ext e il section can im

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

these r very good examples.we r getting too much experience by go though thise exssssssssssssss

Posted by pradeep on Tuesday, 03.11.08 @ 16:47pm | #52298

Hello,
I am vikas, i am developing a Tetrix game , but i dont know what i use(swing or awt using multithrading). Please guide me.

Posted by Vikas Gholap on Monday, 10.29.07 @ 16:53pm | #35066

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.