Threads in Java: How To Create Thread

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

1

Threads in Java
a) Java is a multi-threaded programming language which means we can develop
multi-threaded program using Java.

b) A multi-threaded program contains two or more parts that can run concur-
rently and each part can handle a different task at the same time making
optimal use of the available resources specially when your computer has mul-
tiple CPUs.

c) By definition, multitasking is when multiple processes share common pro-


cessing resources such as a CPU. Multi-threading extends the idea of multi-
tasking into applications where you can subdivide specific operations within
a single application into individual threads. Each of the threads can run in
parallel.

d) The OS divides processing time not only among different applications, but
also among each thread within an application.

e) Multi-threading enables you to write in a way where multiple activities can


proceed concurrently in the same program.

How to Create Thread


There are two ways to create a thread:

1. By extending Thread class

2. By implementing Runnable interface.

Extending a Thread Class


This approach provides more flexibility in handling multiple threads created
using available methods in Thread class.

1. Step 1
You will need to override run( ) method available in Thread class. This
method provides an entry point for the thread and you will put your
complete business logic inside this method. Following is a simple syntax
of run() method
1 public void run ( )

2. Step 2
Once the Thread object is created, you can start it by calling start()
method, which executes a call to run( ) method. Following is a simple
syntax of start() method
1 void start ( );
2

1 class ThreadDemo extends Thread {


private Thread t ;
3 private String threadName ;

5 ThreadDemo ( String name ) {


threadName = name ;
7 System . out . println ( " Creating " + threadName );
}
9

public void run () {


11 System . out . println ( " Running " + threadName );
try {
13 for ( int i = 4; i > 0; i - -) {
System . out . println ( " Thread : " + threadName + " , " + i );
15 // Let the thread sleep for a while .
Thread . sleep (50);
17 }
} catch ( I n t e r r u p t e d E x c e p t i o n e ) {
19 System . out . println ( " Thread " + threadName + " interrupted . " );
}
21 System . out . println ( " Thread " + threadName + " exiting . " );
}
23

public void start () {


25 System . out . println ( " Starting " + threadName );
if ( t == null ) {
27 t = new Thread ( this , threadName );
t . start ();
29 }
}
31 }

33 public class TestThread {

35 public static void main ( String args []) {


ThreadDemo T1 = new ThreadDemo ( " Thread -1 " );
37 T1 . start ();

39 ThreadDemo T2 = new ThreadDemo ( " Thread -2 " );


T2 . start ();
41 }
}

Implementing the Runnable Interface


If your class is intended to be executed as a thread then you can achieve this by
implementing a Runnable interface. You will need to follow three basic steps.
1. Step 1
As a first step, you need to implement a run() method provided by a
3

Runnable interface. This method provides an entry point for the thread
and you will put your complete business logic inside this method. Follow-
ing is a simple syntax of the run() method
public void run ()

2. Step 2
As a second step, you will instantiate a Thread object using the following
constructor
1 Thread ( Runnable threadObj , String threadName );

Where, threadObj is an instance of a class that implements the Runnable


interface and threadName is the name given to the new thread.

3. Step 3
Once a Thread object is created, you can start it by calling start() method,
which executes a call to run( ) method. Following is a simple syntax of
start() method
1 void start ();

1 class RunnableDemo implements Runnable {


private Thread t ;
3 private String threadName ;

5 RunnableDemo ( String name ) {


threadName = name ;
7 System . out . println ( " Creating " + threadName );
}
9

public void run () {


11 System . out . println ( " Running " + threadName );
try {
13 for ( int i = 4; i > 0; i - -) {
System . out . println ( " Thread : " + threadName + " , " + i );
15 // Let the thread sleep for a while .
Thread . sleep (50);
17 }
} catch ( I n t e r r u p t e d E x c e p t i o n e ) {
19 System . out . println ( " Thread " + threadName + " interrupted . " );
}
21 System . out . println ( " Thread " + threadName + " exiting . " );
}
23

public void start () {


25 System . out . println ( " Starting " + threadName );
if ( t == null ) {
27 t = new Thread ( this , threadName );
t . start ();
29 }
4

}
31 }

33 public class TestThread {

35 public static void main ( String args []) {


RunnableDemo R1 = new RunnableDemo ( " Thread -1 " );
37 R1 . start ();

39 RunnableDemo R2 = new RunnableDemo ( " Thread -2 " );


R2 . start ();
41 }
}

You might also like