Multithreading allows a program to split into multiple subprograms called threads that can run concurrently. Threads go through various states like new, runnable, running, blocked, and dead. There are two main ways to create threads: by extending the Thread class or implementing the Runnable interface. Threads can have different priorities that influence scheduling order. Multithreading allows performing multiple operations simultaneously to save time without blocking the user, and exceptions in one thread do not affect others.
2. What is Multithreading?
Multithreading in java is a process of
executing multiple threads simultaneously.
It is a conceptual programing paradigm
where a program is divided into two or
more subprograms which can be
implemented at the same time.
4. Life Cycle Of Thread(in details)
Newborn : When we create a new Thread
object using new operator , thread state is New
Thread .At this point thread is not alive and it’s
a state internal to java program.
Runnable: When we call start() function on
Thread object ,it’s state is changed to Runnable
. The control is given to Thread scheduler to
finish it’s execution.
5. Running: When thread is executing, it’s state is
changed to Running. A thread can change state to
Runnable, Dead or Blocked from running state
depends on time slicing, thread completion run()
method or waiting for some resources.
6. Blocked /Waiting: The programmer can make a
running thread to become inactive temporarily
for some period. In this period, the thread is
said to be in blocked state.
Dead : When the execution of run() method is
over, as the job it is meant is done, it is brought
to dead state.
7. Some Thread Method
Start(): Creates a new thread and make it runnable.
run(): It is used to perform action for a thread.
yield(): causes the currently executing thread object to
temporarily pause and allow other threads to execute.
suspend(): is used to suspend the thread
resume(): is used to resume the suspended thread
Sleep():A thread to sleep for specified time period using
the method sleep.
stop(): is used to stop the thread.
Wait():it is use a thread inactive foe some time.
notify():it use to resume the waited thread.
8. Example
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
9. How to create thread
There are two ways to create a thread:
1.By extending Thread class
2.By implementing Runnable interface.
10. Thread Class: Thread class provide constructors and
methods to create and perform operations on a thread.
Thread class extends Object class and implements
Runnable interface. Example:
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
} }
11. Runnable Interface: The Runnable interface should be
implemented by any class whose instances are
intended to be executed by a thread. Runnable
interface have only one method named run().
Example:
class Multi3 implements Runnable{
public void run(){
System.out.println("thread is running..."); {
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
t1.start();
} }
12. Priority of a Thread
Each thread have a priority. Priorities are
represented by a number between 1 and 10. In most
cases, thread scheduler schedules the threads
according to their priority (known as preemptive
scheduling).
Type of priority:
1.public static int MIN_PRIORITY(1)
2.public static int NORM_PRIORITY(5)
3.public static int MAX_PRIORITY(10)
13. Priority of a Thread(Example)
class TestMultiPriority1 extends Thread{
public void run(){
System.out.println("running thread name is:"+Thread.currentThread().getName());
System.out.println("running thread priority is:"+Thread.currentThread().getPriority());
}
public static void main(String args[]){
TestMultiPriority1 m1=new TestMultiPriority1();
TestMultiPriority1 m2=new TestMultiPriority1();
m1.setPriority(Thread.MIN_PRIORITY);
m2.setPriority(Thread.MAX_PRIORITY);
m1.start();
m2.start();
} }
14. Advantages of Java Multithreading
1) It doesn't block the user because threads
are independent and you can perform
multiple operations at the same time.
2) You can perform many operations together,
so it saves time.
3) Threads are independent, so it doesn't
affect other threads if an exception occurs in
a single thread.