< Prev
Next >

Threads in Java

A thread is a program in execution that is performing some specific task. In Java, a thread is automatically created and started when the Java Virtual Machine(JVM) loads and this thread is called main thread. In order to execute a Java program, the main thread looks for the main() method in it. This thread is called main thread because the main() method of a Java program is the first method that runs in it.

Note : The main thread is provided to us by default, even when we didn't manually create a thread in a program. Besides the default given main thread, we can create our own thread by creating an object of Thread class. A thread(main thread or a user created thread )is an object of java.lang.Thread class.

Multithreaded programming

Java allows us to perform multithreaded programming. A multithreaded program contains multiple threads running concurrently within a program, where each thread is performing some specific task. For example - a thread is performing text editing, a second thread is playing music and a third thread is printing a file in the background.

Hence, a multi-threading in a Java program helps in achieving executing multiple tasks at one time, thereby, multithreaded programs make the most use of CPU efficiency and keeps its idle time to minimum and that's why multithreaded programming with multiple threads running at the same time are used in making interactive applications.

Some useful methods of Thread class.

Methods Description
String getName() Returns the name of thread.
void setName() Setting the name of thread.
int getPriority() Returns the priority of thread.
boolean isAlive() Checks if a thread is alive or dead.
void start() Starts the thread and automatically calls the run() method.
void run() Entry point for the thread.
void sleep() Suspends the execution of a thread for a specific amount of time.
void join() Makes a thread wait for another thread to terminate.
Thread currentThread() Returns the reference to the currently executing thread.

Accessing the default main thread and getting, setting its name -:

public class ThreadExm
public static void main(String... ar)
Thread th = Thread.currentThread();
System.out.println("Thread information : "+th);
System.out.println("Name of main thread - "+ th.getName());

th.setName("Thread2");	//Setting a new name of the main thread.

System.out.println("Thread's new information " +th);
System.out.println("New name of main thread - "+ th.getName());


Output is -:

Thread information : Thread[main,5,main]
Name of main thread - main
Thread's new information : Thread[Thread2,5,main]
New name of main thread - Thread2

Program Analysis

Making the current main thread sleep by using sleep() method

class ThreadExm
public static void main(String... ar)
Thread th= Thread.currentThread();
System.out.println("Thread information:"+ th);
System.out.println("Name of the thread - "+ th.getName());
	System.out.println("main thread going to sleep for 5 seconds");
	th.sleep(5000); //Sleep method may throw InterruptedException, hence a try-catch block
	System.out.println("main thread is awakened and exits");
catch(InterruptedException e)


Output is

Thread information :Thread[main,5,main]
Name of the thread - main
main thread going to sleep for 5 seconds
main thread is awakened and exits

Program Analysis

Note : Call to sleep() method of Thread class must be inside a try-catch block, otherwise it gives us a compile error.

Difference between Process and a Thread

A process is simply a program in execution. For example a WordPad program used being used to edit a document is a process. There could be multiple processes running in a computer at the same time, for example- Media Player playing a movie, A text editor editing a document etc.

Whereas a thread is a part of a program that is running concurrently with other parts of the program. For example, when you are using the WordPad program, you can edit a document and can also print another document at the same time. This is only possible because of multiple threads in execution at the same time in WordPad program, where one thread is looking after editing the document and other thread looking after printing a document.

Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter

< Prev
Next >
< Static in Serialization
Lifecycle of a Thread >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
C++ and Python