SlideShare a Scribd company logo
2
Most read
3
Most read
6
Most read
Multithreading
by
V.Santhi,
Asst. Prof,
Department of Computer Applications
Bon Secours College for Women
Thanjavur.
Types of Multitasking
There are two distinct types of multitasking:
• Process-based
• Thread-based
Process-based
• Multitasking is the feature that allows your
computer to run two or more program
concurrently.
• For example, process-based multitasking
enables you to run the Java compiler at the
same time that you are using a text editor.
Thread based
• In a thread-based multitasking environment,
the thread is the smallest unit of dispatchable
code.
• This means that a single program can perform
two or more tasks simultaneously
5
A single threaded program
class ABC
{
….
public void main(..)
{
…
..
}
}
begin
body
end
6
A Multithreaded Program
Main Thread
Thread A Thread B Thread C
start start
start
Threads may switch or exchange data/results
• A multithreaded program contains two or
more parts that can run concurrently. Each
part of such a program is called a thread.
• Each thread defines a separate path of
execution
Multithreading
Application Thread
• When we execute an application:
1. The JVM creates a Thread object whose task
is defined by the main() method
2. The JVM starts the thread
3. The thread executes the statements of the
program one by one
4. After executing all the statements, the method
returns and the thread dies
Creating a Thread
You create a thread by instantiating an object of type
Thread.
Java defines two ways in which this can be accomplished:
■ Can implement the Runnable interface.
■ Can extend the Thread class, itself.
The following two sections look at each method, in turn.
Implementing Runnable
The easiest way to create a thread is to create a class that
implements the Runnable interface. Runnable
abstracts a unit of executable code. You can construct a
thread on any object that implements Runnable. To
implement Runnable, a class need only implement a
single method called run( ), which is declared like this:
public void run( )
The Thread Class and the Runnable
Interface
Method Meaning
• getName() Obtain 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.
Extending Thread
public class ThreadExample extends Thread
{
public void run ()
{
for (int i = 1; i <= 100; i++) {
System.out.println(“---”);
}
}
}
class NewThread implements Runnable
{
Thread t;
NewThread()
{
// Create a new, second thread
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for the second
thread.
public void run()
{
try {
for(int i = 5; i > 0; i--)
{
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ThreadDemo {
public static void main(String args[]) {
new NewThread(); // create a new thread
try {
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread
interrupted.");
}
System.out.println("Main thread exiting.");
}
}
Scheduling Threads
14
Thread Priority
• Each thread is assigned a default priority of
Thread.NORM_PRIORITY (constant of 5).
• You can reset the priority using setPriority(int priority)
• By default, a thread has the priority level of the thread
that created it.
– Java allows users to change priority:
• ThreadName.setPriority(intNumber)
– MIN_PRIORITY = 1
– NORM_PRIORITY=5
– MAX_PRIORITY=10
15
Thread Priority Example
class A extends Thread
{
public void run()
{
System.out.println("Thread A started");
for(int i=1;i<=4;i++)
{
System.out.println("t From ThreadA: i= "+i);
}
System.out.println("Exit from A");
}
}
class B extends Thread
{
public void run()
{
System.out.println("Thread B started");
for(int j=1;j<=4;j++)
{
System.out.println("t From ThreadB: j= "+j);
}
System.out.println("Exit from B");
}
}
16
class C extends Thread
{
public void run()
{
System.out.println("Thread C started");
for(int k=1;k<=4;k++)
{
System.out.println("t From
ThreadC: k= "+k);
}
System.out.println("Exit from C");
}
}
class ThreadPriority
{
public static void main(String args[])
{
A threadA=new A();
B threadB=new B();
C threadC=new C();
threadC.setPriority(Thread.MAX_PRIORITY);
threadB.setPriority(threadA.getPriority()+1);
threadA.setPriority(Thread.MIN_PRIORITY);
System.out.println("Started Thread
A");
threadA.start();
System.out.println("Started Thread
B");
threadB.start();
System.out.println("Started Thread
C");
threadC.start();
System.out.println("End of main
thread");
}
}

More Related Content

PPTX
MULTI THREADING IN JAVA
PPT
Threads c sharp
PPTX
Threading
PPTX
Java Thread & Multithreading
PPTX
PPT
Threads c sharp
PPTX
Multithread Programing in Java
PPTX
Thread priorities
MULTI THREADING IN JAVA
Threads c sharp
Threading
Java Thread & Multithreading
Threads c sharp
Multithread Programing in Java
Thread priorities

What's hot (20)

PPT
Intro To .Net Threads
PDF
Java threads
PPTX
Thread model of java
ODP
Multithreading Concepts
PPT
Java Multithreading
PPT
12 multi-threading
 
PPTX
PDF
Java Thread Synchronization
PPT
Java thread
PPTX
Multi-threaded Programming in JAVA
PPT
Chap2 2 1
PPTX
Multi threading
PPT
Multithreading
PPTX
Multithreading in java
PPTX
Java Multi Thead Programming
PPT
Learning Java 3 – Threads and Synchronization
PPTX
Multithreading in java
PPT
Developing Multithreaded Applications
PPT
Java Multithreading and Concurrency
Intro To .Net Threads
Java threads
Thread model of java
Multithreading Concepts
Java Multithreading
12 multi-threading
 
Java Thread Synchronization
Java thread
Multi-threaded Programming in JAVA
Chap2 2 1
Multi threading
Multithreading
Multithreading in java
Java Multi Thead Programming
Learning Java 3 – Threads and Synchronization
Multithreading in java
Developing Multithreaded Applications
Java Multithreading and Concurrency
Ad

Similar to Multithreading (20)

PPTX
Object-Oriented-Prog_MultiThreading.pptx
DOCX
Threadnotes
PPTX
Multithreading
PPTX
Multithreading in java
PPT
multithreading, creating a thread and life cycle in java.ppt
PPTX
multithreading to be used in java with good programs.pptx
DOCX
Module - 5 merged.docx notes about engineering subjects java
PPTX
multithreading,thread and processinjava-210302183809.pptx
PPT
Session 7_MULTITHREADING in java example.ppt
PPTX
Multithreading.pptx
PPTX
advanced java programming paradigms presentation
PPTX
OOPS object oriented programming UNIT-4.pptx
PPTX
Multithreading.pptx
PPTX
Multithreading in java
PPTX
Module 4 - Part 4 - Multithreaded Programming.pptx
PPT
PPT
multithreading
PPT
multhi threading concept in oops through java
PDF
Unit 5 - Java Multihhhhhhhhhhhhhhhhhhhhaaaaaaaaaaaaaaaaathreading.pdf
PPT
Java multithreading
Object-Oriented-Prog_MultiThreading.pptx
Threadnotes
Multithreading
Multithreading in java
multithreading, creating a thread and life cycle in java.ppt
multithreading to be used in java with good programs.pptx
Module - 5 merged.docx notes about engineering subjects java
multithreading,thread and processinjava-210302183809.pptx
Session 7_MULTITHREADING in java example.ppt
Multithreading.pptx
advanced java programming paradigms presentation
OOPS object oriented programming UNIT-4.pptx
Multithreading.pptx
Multithreading in java
Module 4 - Part 4 - Multithreaded Programming.pptx
multithreading
multhi threading concept in oops through java
Unit 5 - Java Multihhhhhhhhhhhhhhhhhhhhaaaaaaaaaaaaaaaaathreading.pdf
Java multithreading
Ad

More from SanthiNivas (20)

PDF
Programming in PHP Course Material BCA 6th Semester
PPTX
Implementing AJAX in PHP. Asynchronous JavaScript and XML
PPT
packages.ppt
PPT
exception-handling-in-java.ppt
PPT
Introduction to PHP.ppt
PPTX
static methods.pptx
PPT
Topologies.ppt
PPT
transmission media.ppt
PPTX
Internet Basics Presentation.pptx
PPT
Topologies.ppt
PPTX
Features of Java.pptx
PPTX
Output Devices.pptx
PPTX
Input Devices.pptx
PPTX
Operating System File Management Unit v.pptx
PPTX
Input and Output Devices
PPT
PDF
DDA ALGORITHM.pdf
PDF
Computer Graphics Unit 2
PDF
Computer Graphics
PPTX
Page Layout and Background
Programming in PHP Course Material BCA 6th Semester
Implementing AJAX in PHP. Asynchronous JavaScript and XML
packages.ppt
exception-handling-in-java.ppt
Introduction to PHP.ppt
static methods.pptx
Topologies.ppt
transmission media.ppt
Internet Basics Presentation.pptx
Topologies.ppt
Features of Java.pptx
Output Devices.pptx
Input Devices.pptx
Operating System File Management Unit v.pptx
Input and Output Devices
DDA ALGORITHM.pdf
Computer Graphics Unit 2
Computer Graphics
Page Layout and Background

Recently uploaded (20)

PPTX
GDM (1) (1).pptx small presentation for students
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Cell Types and Its function , kingdom of life
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Institutional Correction lecture only . . .
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Cell Structure & Organelles in detailed.
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Pre independence Education in Inndia.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
GDM (1) (1).pptx small presentation for students
VCE English Exam - Section C Student Revision Booklet
Microbial diseases, their pathogenesis and prophylaxis
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
01-Introduction-to-Information-Management.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Cell Types and Its function , kingdom of life
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Institutional Correction lecture only . . .
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Cell Structure & Organelles in detailed.
O7-L3 Supply Chain Operations - ICLT Program
Pre independence Education in Inndia.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf

Multithreading

  • 1. Multithreading by V.Santhi, Asst. Prof, Department of Computer Applications Bon Secours College for Women Thanjavur.
  • 2. Types of Multitasking There are two distinct types of multitasking: • Process-based • Thread-based
  • 3. Process-based • Multitasking is the feature that allows your computer to run two or more program concurrently. • For example, process-based multitasking enables you to run the Java compiler at the same time that you are using a text editor.
  • 4. Thread based • In a thread-based multitasking environment, the thread is the smallest unit of dispatchable code. • This means that a single program can perform two or more tasks simultaneously
  • 5. 5 A single threaded program class ABC { …. public void main(..) { … .. } } begin body end
  • 6. 6 A Multithreaded Program Main Thread Thread A Thread B Thread C start start start Threads may switch or exchange data/results
  • 7. • A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread. • Each thread defines a separate path of execution Multithreading
  • 8. Application Thread • When we execute an application: 1. The JVM creates a Thread object whose task is defined by the main() method 2. The JVM starts the thread 3. The thread executes the statements of the program one by one 4. After executing all the statements, the method returns and the thread dies
  • 9. Creating a Thread You create a thread by instantiating an object of type Thread. Java defines two ways in which this can be accomplished: ■ Can implement the Runnable interface. ■ Can extend the Thread class, itself. The following two sections look at each method, in turn. Implementing Runnable The easiest way to create a thread is to create a class that implements the Runnable interface. Runnable abstracts a unit of executable code. You can construct a thread on any object that implements Runnable. To implement Runnable, a class need only implement a single method called run( ), which is declared like this: public void run( )
  • 10. The Thread Class and the Runnable Interface Method Meaning • getName() Obtain 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.
  • 11. Extending Thread public class ThreadExample extends Thread { public void run () { for (int i = 1; i <= 100; i++) { System.out.println(“---”); } } }
  • 12. class NewThread implements Runnable { Thread t; NewThread() { // Create a new, second thread t = new Thread(this, "Demo Thread"); System.out.println("Child thread: " + t); t.start(); // Start the thread } // This is the entry point for the second thread. public void run() { try { for(int i = 5; i > 0; i--) { System.out.println("Child Thread: " + i); Thread.sleep(500); } } catch (InterruptedException e) { System.out.println("Child interrupted."); } System.out.println("Exiting child thread."); } } class ThreadDemo { public static void main(String args[]) { new NewThread(); // create a new thread try { for(int i = 5; i > 0; i--) { System.out.println("Main Thread: " + i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println("Main thread interrupted."); } System.out.println("Main thread exiting."); } }
  • 14. 14 Thread Priority • Each thread is assigned a default priority of Thread.NORM_PRIORITY (constant of 5). • You can reset the priority using setPriority(int priority) • By default, a thread has the priority level of the thread that created it. – Java allows users to change priority: • ThreadName.setPriority(intNumber) – MIN_PRIORITY = 1 – NORM_PRIORITY=5 – MAX_PRIORITY=10
  • 15. 15 Thread Priority Example class A extends Thread { public void run() { System.out.println("Thread A started"); for(int i=1;i<=4;i++) { System.out.println("t From ThreadA: i= "+i); } System.out.println("Exit from A"); } } class B extends Thread { public void run() { System.out.println("Thread B started"); for(int j=1;j<=4;j++) { System.out.println("t From ThreadB: j= "+j); } System.out.println("Exit from B"); } }
  • 16. 16 class C extends Thread { public void run() { System.out.println("Thread C started"); for(int k=1;k<=4;k++) { System.out.println("t From ThreadC: k= "+k); } System.out.println("Exit from C"); } } class ThreadPriority { public static void main(String args[]) { A threadA=new A(); B threadB=new B(); C threadC=new C(); threadC.setPriority(Thread.MAX_PRIORITY); threadB.setPriority(threadA.getPriority()+1); threadA.setPriority(Thread.MIN_PRIORITY); System.out.println("Started Thread A"); threadA.start(); System.out.println("Started Thread B"); threadB.start(); System.out.println("Started Thread C"); threadC.start(); System.out.println("End of main thread"); } }