Javatpoint Multithreading
Javatpoint Multithreading
Javatpoint Multithreading
Multithreading in Java
1. Multithreading
2. Multitasking
3. Process-based multitasking
4. Thread-based multitasking
5. What is Thread
Multitasking
Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to
utilize the CPU. Multitasking can be achieved in two ways:
Threads are independent. If there occurs exception in one thread, it doesn't affect other
threads. It uses a shared memory area.
As shown in the above figure, a thread is executed inside the process. There is context-
switching between the threads. There can be multiple processes inside the OS, and one
process can have multiple threads.
Do You Know
o How to perform two tasks by two threads?
o How to perform multithreading by anonymous class?
o What is the Thread Scheduler and what is the difference between preemptive
scheduling and time slicing?
o What happens if we start a thread twice?
o What happens if we call the run() method instead of start() method?
o What is the purpose of join method?
o Why JVM terminates the daemon thread if no user threads are remaining?
o What is the shutdown hook?
o What is garbage collection?
o What is the purpose of finalize() method?
o What does the gc() method?
o What is synchronization and why use synchronization?
o What is the difference between synchronized method and synchronized block?
o What are the two ways to perform static synchronization?
o What is deadlock and when it can occur?
o What is interthread-communication or cooperation?
A thread can be in one of the five states. According to sun, there is only 4 states in thread life cycle in
non-runnable and terminated. There is no running state.
But for better understanding the threads, we are explaining it in the 5 states.
The life cycle of the thread in java is controlled by JVM. The java thread states are as follows:
1. New
2. Runnable
3. Running
4. Non-Runnable (Blocked)
5. Terminated
1) New
The thread is in new state if you create an instance of Thread class but before the invocation of start() m
2) Runnable
The thread is in runnable state after invocation of start() method, but the thread scheduler
has not selected it to be the running thread.
3) Running
The thread is in running state if the thread scheduler has selected it.
4) Non-Runnable (Blocked)
This is the state when the thread is still alive, but is currently not eligible to run.
5) Terminated
A thread is in terminated or dead state when its run() method exits.
Thread class:
Thread class provide constructors and methods to create and perform operations on a thread.Thread cla
class and implements Runnable interface.
o Thread()
o Thread(String name)
o Thread(Runnable r)
o Thread(Runnable r,String name)
Runnable interface:
The Runnable interface should be implemented by any class whose instances are intended to be execute
Runnable interface have only one method named run().
Starting a thread:
start() method of Thread class is used to start a newly created thread. It performs following tasks:
o A new thread starts(with new callstack).
o The thread moves from New state to the Runnable state.
o When the thread gets a chance to execute, its target run() method will run.
There is no guarantee that which runnable thread will be chosen to run by the thread
scheduler.
The thread scheduler mainly uses preemptive or time slicing scheduling to schedule the
threads.
Output:
1
1
2
2
3
3
4
4
As you know well that at a time only one thread is executed. If you sleep a thread for the
specified time,the thread shedular picks up another thread and so on.
1. public class TestThreadTwice1 extends Thread{
2. public void run(){
3. System.out.println("running...");
4. }
5. public static void main(String args[]){
6. TestThreadTwice1 t1=new TestThreadTwice1();
7. t1.start();
8. t1.start();
9. }
10. }
Test it Now
running
Exception in thread "main" java.lang.IllegalThreadStateException
1. class TestCallRun1 extends Thread{
2. public void run(){
3. System.out.println("running...");
4. }
5. public static void main(String args[]){
6. TestCallRun1 t1=new TestCallRun1();
7. t1.run();//fine, but does not start a separate call stack
8. }
9. }
Test it Now
Output:running...
As you can see in the above program that there is no context-switching because here t1 and t2 will be t
object not thread object.