Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

MULTITHREADING IN

JAVA
BATCH 5- KADAM RUTUJA
K.Tejaram Goud
Multithreading in Java

• Multithreading in Java is a process of executing multiple threads


simultaneously.
• A thread is a lightweight sub-process, the smallest unit of
processing. Multiprocessing and multithreading, both are used to
achieve multitasking.

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.
What is Thread in java
• A thread is a lightweight subprocess, the smallest unit of processing. It is a
separate path of execution.
• Threads are independent. If there occurs exception in one thread, it doesn't
affect other threads. It uses a shared memory area.
Java Thread class
• Java provides Thread class to achieve thread programming. Thread class
provides constructors and methods to create and perform operations on a
thread.
Sr.no Modifier and Type Method Description
.
1) void start() It is used to start the
execution of the thread.

2) void run() It is used to do an action for a


thread.

3) static void sleep() It sleeps a thread for the


specified amount of time.

4) static Thread currentThread() It returns a reference to the


currently executing thread
object.
Life cycle of a Thread (Thread States)
In Java, a thread always exists in any one of the
following states. These states are:
1.New
2.Active
3.Blocked / Waiting
4.Timed Waiting
5.Terminated
How to create a thread in Java
There are two ways to create a thread:

1.By extending Thread class


2.By implementing Runnable interface.

Java Thread Example by extending Thread class

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();
}
Output:
} thread is running...
Java Thread Example by implementing Runnable interface

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); // Using the constructor T
hread(Runnable r)
t1.start();
}
}

Output:
thread is running...
Thread.sleep()

1)public static void sleep(long mls) throws InterruptedException

2)public static void sleep(long mls, int n) throws InterruptedException

Parameters:
The following are the parameters used in the sleep() method.
• mls: The time in milliseconds is represented by the parameter mls. The duration
for which the thread will sleep is given by the method sleep().

• n: It shows the additional time up to which the programmer or developer wants


the thread to be in the sleeping state. The range of n is from 0 to 999999.

• The method does not return anything.


import java.lang.Thread;
import java.io.*;
public class TestSleepMethod2
{
public static void main(String argvs[])
{
try {
for (int j = 0; j < 5; j++)
{
Thread.sleep(1000);
System.out.println(j);
}
}
catch (Exception expn)
{
Output:
0
System.out.println(expn); 1
}
2
}
} 3
4
Java join() method
• The join() method in Java is provided by the java.lang.
• Thread class that permits one thread to wait until the other thread to finish its execution.

public final void join() throws InterruptedException

Priority of a Thread (Thread Priority)


• Each thread has a priority. Priorities are represented by a number between 1 and 10.
• In most cases, the thread scheduler schedules the threads according to their priority
(known as preemptive scheduling).
Setter & Getter Method of Thread Priority

public final int getPriority():


The java.lang.Thread.getPriority() method returns the priority of the given
thread.

public final void setPriority(int newPriority):


The java.lang.Thread.setPriority() method updates or assign the priority of the
thread to newPriority. The method throws IllegalArgumentException if the
value newPriority goes out of the range, which is 1 (minimum) to 10
(maximum).
constants defined in Thread class:
1.public static int MIN_PRIORITY

2.public static int NORM_PRIORITY

3.public static int MAX_PRIORITY

Default priority of a thread is 5 (NORM_PRIORITY). The value of MIN_PRIORITY is


1 and the value of MAX_PRIORITY is 10.

You might also like