Multithreading in java with examples
BY DHAN SINGH SAUD| FILED UNDER: MULTITHREADING
Before we talk about multithreading, let’s discuss threads. A thread is a light-
weight smallest part of a process that can run concurrently with the other
parts(other threads) of the same process. Threads are independent because
they all have separate path of execution that’s the reason if an exception occurs
in one thread, it doesn’t affect the execution of other threads. All threads of a
process share the common memory. The process of executing multiple
threads simultaneously is known as multithreading.
Let’s summarize the discussion in points:
1. The main purpose of multithreading is to provide simultaneous execution of
two or more parts of a program to maximum utilize the CPU time. A
multithreaded program contains two or more parts that can run concurrently.
Each such part of a program called thread.
2. Threads are lightweight sub-processes, they share the common memory
space. In Multithreaded environment, programs that are benefited from
multithreading, utilize the maximum CPU time so that the idle time can be kept to
minimum.
3. A thread can be in one of the following states:
NEW – A thread that has not yet started is in this state.
RUNNABLE – A thread executing in the Java virtual machine is in this state.
BLOCKED – A thread that is blocked waiting for a monitor lock is in this state.
WAITING – A thread that is waiting indefinitely for another thread to perform a
particular action is in this state.
TIMED_WAITING – A thread that is waiting for another thread to perform an
action for up to a specified waiting time is in this state.
TERMINATED – A thread that has exited is in this state.
A thread can be in only one state at a given point in time.
Read more about thread states at this link: Life cycle of threads
Multitasking vs Multithreading vs Multiprocessing
vs parallel processing
If you are new to java you may get confused among these terms as they are
used quite frequently when we discuss multithreading. Let’s talk about them in
brief.
Multitasking: Ability to execute more than one task at the same time is known
as multitasking.
Multithreading: We already discussed about it. It is a process of executing
multiple threads simultaneously. Multithreading is also known as Thread-based
Multitasking.
Multiprocessing: It is same as multitasking, however in multiprocessing more
than one CPUs are involved. On the other hand one CPU is involved in
multitasking.
Parallel Processing: It refers to the utilization of multiple CPUs in a single
computer system.
Creating a thread in Java
There are two ways to create a thread in Java:
1) By extending Thread class.
2) By implementing Runnable interface.
Before we begin with the programs(code) of creating threads, let’s have a look at
these methods of Thread class. We have used few of these methods in the
example below.
getName(): It is used for Obtaining a thread’s name
getPriority(): Obtain a thread’s priority
isAlive(): Determine if a thread is still running
join(): Wait for a thread to terminate
run(): Entry point for the thread
sleep(): suspend a thread for a period of time
start(): start a thread by calling its run() method
Method 1: Thread creation by extending Thread class
Example 1:
class MultithreadingDemo extends Thread{
public void run(){
System.out.println("My thread is in running state.");
}
public static void main(String args[]){
MultithreadingDemo obj=new MultithreadingDemo();
obj.start();
}
}
Output:
My thread is in running state.
Method 2: Thread creation by implementing Runnable
Interface
A Simple Example
class MultithreadingDemo implements Runnable{
public void run(){
System.out.println("My thread is in running state.");
}
public static void main(String args[]){
MultithreadingDemo obj=new MultithreadingDemo();
Thread tobj =new Thread(obj);
tobj.start();
}
}
Output:
My thread is in running state.