Thread in Java
Threads allows a program to operate more efficiently by doing multiple things at the same time.
Threads can be used to perform complicated tasks in the background without interrupting the main program.
The Concept Of Multitasking
To help users Operating System accommodates users the privilege of multitasking, where users can perform multiple actions simultaneously on the machine. This Multitasking can be enabled in two ways:
- Process-Based Multitasking
- Thread-Based Multitasking
1. Process-Based Multitasking (Multiprocessing)
In this type of Multitasking, processes are heavyweight and each process was allocated by a separate memory area. And as the process is heavyweight the cost of communication between processes is high and it takes a long time for switching between processes as it involves actions such as loading, saving in registers, updating maps, lists, etc.
2. Thread-Based Multitasking
As we discussed above Threads are provided with lightweight nature and share the same address space, and the cost of communication between threads is also low.
Why is threading used?
Now that we understand why threads are utilized, we can appreciate how they can effectively multitask in a shared memory environment due to their advantages of being lightweight and being able to communicate with other threads for a low cost.
Life Cycle Of Thread
There are different states Thread transfers into during its lifetime, let us know about those states in the following lines: in its lifetime, a thread undergoes the following states, namely:
- New State
- Active State
- Waiting/Blocked State
- Timed Waiting State
- Terminated State
What is the Main Thread?
As is common knowledge, each and every Java program has a Main Method that serves as the starting point for the JVM to execute the code. In the same way, each program in the multithreading concept has a Main Thread that is provided by default by the JVM, so whenever a Java program is created, the JVM provides the Main Thread for its execution.
How to Create Threads using Java Programming Language?
We can create Threads in java using two ways, namely :
- By extending Thread Class
- By Implementing a Runnable interface
1. By Extending Thread Class
Get Kaweesha Nadeethara De Silva’s stories in your inbox
Join Medium for free to get updates from this writer.
We can run Threads in Java by using Thread Class, which provides constructors and methods for creating and performing operations on a Thread, which extends a Thread class that can implement Runnable Interface. We use the following constructors for creating the Thread:
- Thread
- Thread(Runnable r)
- Thread(String name)
- Thread(String name, Runnable r)
after using currentThread().getName()
strat() method
super.strat();
run(int x)
setDaemon()
2. By Implementing a Runnable interface
Java runnable is an interface used to execute code on a concurrent thread. It is an interface which is implemented by any class if we want that the instances of that class should be executed by a thread.
setPriority(1);
setPriority(10);
Removing setPriority(10);
Java Concurrency — yield(), sleep() and join() Methods
We shall discover what the Java yield(), join(), and sleep() methods are and how they differ from one another fundamentally. We will first go through the fundamentals of each of these three approaches before contrasting them.
Using one of the Thread class’s following methods, we can stop a thread from running. The thread will not be allowed to run using any of the three ways.
- yield() Method
Consider a system with three threads: t1, t2, and t3. While threads t2 and t3 are in the Ready/Runnable state, thread t1 receives the processor and begins execution. The times for threads t1 and t2 to finish are respectively 5 hours and 5 minutes. T2 must wait 5 hours to execute a 5-minute task since t1 will conclude its execution in 5 hours. We need a means to stop the execution of a thread in between if something crucial is still pending in such cases where one thread is taking too long to complete its execution. Using yield() makes this possible. The yield() basically means that the thread is not doing anything particularly important and if any other threads or processes need to be run, they should run. Otherwise, the current thread will continue to run. - sleep() Method
The Java Thread class provides the two variant of the sleep() method. First one accepts only an arguments, whereas the other variant accepts two arguments. The method sleep() is being used to halt the working of a thread for a given amount of time. The time up to which the thread remains in the sleeping state is known as the sleeping time of the thread. After the sleeping time is over, the thread starts its execution from where it has left. - join() Method
The join() method of a Thread instance is used to join the start of a thread’s execution to the end of another thread’s execution such that a thread does not start running until another thread ends. If join() is called on a Thread instance, the currently running thread will block until the Thread instance has finished executing. The join() method waits at most this many milliseconds for this thread to die. A timeout of 0 means to wait forever.
References:
So, perhaps this article was able to teach you something new. My mentor Krish (Krishantha Dinesh) encouraged me to write this article. I also used his YouTube video to help me compose this article (Which I would like to recommend to you as well).






