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.

Ads

Tutorials   
Threading In Java Example Thread Life Cycle Example in java What Is Thread In Java? Creating multiple Threads Daemon Threads Synchronization in java with example Thread priority in java Threads in Java Java Multithreading Example Thread Synchronization in Java Overview of Networking through JAVA,How to retrieve URL information URL in term of Java Network Programming Overview of Networking through JAVA,Convert URI to URL Socket and ports Server Sockets Overview of Networking through JAVA,Find the Host name in reverse of given IP address Print the URL of a URLConnection Networking in Java Computer Networking Overview of Networking through JAVA,Getting the Local port Overview of Networking through JAVA Overview of Networking through JAVA Overview of Networking through JAVA,Getting list of Local Interfaces on a machine Overview of Networking through JAVA Overview of Networking through JAVA,Find Your Host Name Overview of Networking through JAVA,To retrieve the IP address from Host Name, vice-versa Open a URLConnection to specific website address Return the MIME Header Construct a DatagramSocket on an unspecified port Datagram in network environment Construct a DatagramPacket to receive data Client-Server Architecture Thread Priorities Synchronized Threads Multithreading in Java Interthread Communication Creation of MultiThreads Threading in Java Thread Priorities Inter-Thread Communication Synchronized Threads Creation of Multiple Threads Threading in Java Daemon Threads Deadlock Synchronization Life Cycle of Threads Thread Constructors Thread Creation Overview of Thread
Ads

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

Advertisements

Share on Google+Share on Google+

Creation of Multiple Threads

Posted on: December 15, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Creation of Multiple Threads   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
ranja g
April 4, 2011
java,j2ee

this site is very useful for all those who really want to know the basic and advance features of Java. i m recommend everyone for this site who want to learn Java
bhaskar
July 7, 2011
creating multythreads

sir, i have observed here that outputs of botg using Thread or Runnable are not same in line5 of stdout

Ads

 

Ads