Creating multiple Threads

This java tutorial explain how to create multiple thread using Java program. Here you will find step by step process to creating multiple threads.


 

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

In this section you will learn how to create multiple thread in java. Thread is a path of execution within a program, it is a part of process. JVM allow multiple thread to run concurrently. Each and every thread has the priority or you can set or get the priority of thread. Java API provide method like setPriority() and getPriority() method to set or get the priority of thread. Thread having high priority will execute first over thread having low priority. Thread are lightweight process. There are two way to create thread in java they are as follows:

  • By implementing Runnable interface.
  • By extending Thread class.

While implementing Runnable interface or extending Thread class, you have to override run() method.

Example : Code for creating multiple thread.
public class B extends Thread {

public void run() {
System.out.println();
for (int i = 1; i <= 5; i++) {
System.out.println(" Thread name = "+ Thread.currentThread().getName());
}
}
public static void main(String[] args) {
B t1 = new B();
t1.start();

B t2 = new B();
t2.start();

B t3 = new B();
t3.start();
}

Output : When you compile and execute the program the output will be as below

Download SourceCode

Advertisements

Share on Google+Share on Google+

Creating multiple Threads

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

Advertisements

Ads
 

Discuss: Creating multiple Threads  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads