SlideShare a Scribd company logo
Thread Basics
• In computer science, a thread of execution is
the smallest sequence of programmed
instructions that can be managed
independently by a scheduler (typically as part
of an operating system).
• multithreaded programming language which
means we can develop multithreaded
program using Java. A multithreaded program
contains two or more parts that can run
concurrently and each part can handle
different task at the same time making
optimal use of the available resources
specially when your computer has multiple
CPUs.
• Multithreading is the ability of a program or
an operating system process to manage its use
by more than one user at a time and to even
manage multiple requests by the same user
without having to have multiple copies of the
programming running in the computer. 235
Multi-threaded applications
• Responsiveness: Multi-threading has the ability for an
application to remain responsive to input. In a single-
threaded program, if the main execution thread blocks
on a long-running task, the entire application can
appear to freeze. By moving such long-running tasks to
a worker thread that runs concurrently with the main
execution thread, it is possible for the application to
remain responsive to user input while executing tasks
in the background. On the other hand, in most cases
multithreading is not the only way to keep a program
responsive, with non-blocking I/O and/or Unix signals
being available for gaining similar results
• Faster Execution: This advantage of a
multithreaded program allows it to operate
faster on computer systems that have multiple
or multi-core CPUs, or across a cluster of
machines, because the threads of the program
naturally lend themselves to truly concurrent
execution.
• Less Resource Intensive: Using threads, an
application can serve multiple clients
concurrently using less resource than it would
need when using multiple process copies of
itself. For example, the Apache HTTP server,
which uses a pool of listener and server
threads for listening to incoming requests and
processing these
• . Better System Utilization: Multi-threaded
applications can also utilize the system better.
For example, a file-system using multiple
threads can achieve higher throughput and
lower latency since data in faster mediums like
the cache can be delivered earlier while
waiting for a slower medium to retrieve the
data.
• Simplified Sharing and Communication:
Unlike processes, which require message
passing or shared memory to perform inter-
process communication, communication
between threads is very simple. Threads
automatically share the data, code and files
and so, communication is vastly simplified.
• Parallelization: Applications looking to utilize
multi-core and multi-CPU systems can use multi-
threading to split data and tasks into parallel sub-
tasks and let the underlying architecture manage
how the threads run, either concurrently on a
single core or in parallel on multiple cores. GPU
computing environments like CUDA and OpenCL
use the multi-threading model where dozens to
hundreds of threads run in parallel on a large
number of cores.
• Multi-threading has the following drawbacks:
• Synchronization: Since threads share the same address space, the
programmer must be careful to avoid race conditions and other
non-intuitive behaviors. In order for data to be correctly
manipulated, threads will often need to rendezvous in time in order
to process the data in the correct order. Threads may also require
mutually exclusive operations (often implemented using
semaphores) in order to prevent common data from being
simultaneously modified or read while in the process of being
modified. Careless use of such primitives can lead to deadlocks.
• Thread crashes Process: An illegal operation performed by a thread
crashes the entire process and so, one misbehaving thread can
disrupt the processing of all the other threads in the application.
Java Thread & Multithreading
Java library for threading
• import java.util.Random;
Java Thread & Multithreading
Java Thread & Multithreading
Two ways for Thread Creation
Implementing thread using Runnable
interface
• public class thread1 implements Runnable{
• @Override
• public void run()
• {
• System.out.println("hello world");
• }
• public static void main(String args[]) {
• //(new Thread(new hello_thread())).start();
• Thread t=new Thread(new thread1());
• t.start();
•
• }
• }
Extending Thread class
Creating thread using Extending
Thread class
• public class thread2 extends Thread{
• public void run()
• {
• System.out.println("helloworld");
• }
• public static void main(String[] args)
• {
• thread2 t=new thread2();
• t.start();
• }
•
• }
Java Thread & Multithreading
• Can we start a thread twice?
• No. After staring a thread, it can never be
started again. If you does so, an
IllegalThreadStateException is thrown.
Thread Life Cycle
Java Thread & Multithreading
Java Thread & Multithreading
Java Thread & Multithreading
Major thread operations
Multithreaded Program for calculator
• public class calc_add extends Thread
• {
• @Override
• public void run()
• {
•
• try
• {
• int sum;
• int a=2;
• int b=4;
• sum=a+b;
• System.out.println(sum);
• Thread.sleep(8000);
• }
• catch(Exception e)
• {
• System.out.println(e);
• }
• }
• }
• public class calc_mul extends Thread {
• public void run()
• {
•
• try
• {
• int mul;
• int a=2;
• int b=4;
• mul=a*b;
• System.out.print(mul);
• Thread.sleep(2000);
• }
• catch(Exception e)
• {
• System.out.println(e);
• }
•
• }}
• public class calc_min extends Thread{
• public void run()
• {
•
• try
• {
• int minus;
• int a=2;
• int b=4;
• minus=a-b;
• System.out.print(minus);
• Thread.sleep(2000);
• }
• catch(Exception e)
• {
• System.out.println(e);
• }
•
• }}
• public class calc_main {
•
• public static void main(String[] args)
• {
• calc_add t1=new calc_add();
• calc_mul t2=new calc_mul();
• calc_min t3=new calc_min();
• t1.start();
• t2.start();
• t3.start();
• }
• }

More Related Content

PPTX
Multithread Programing in Java
PPTX
Multithreading in java
PPSX
Multithreading in-java
PDF
Java Thread Synchronization
PPTX
Thread model of java
PPTX
Multi-threaded Programming in JAVA
PPT
Java And Multithreading
PPTX
Multithread Programing in Java
Multithreading in java
Multithreading in-java
Java Thread Synchronization
Thread model of java
Multi-threaded Programming in JAVA
Java And Multithreading

What's hot (20)

PPT
Chap2 2 1
ODP
Multithreading Concepts
PPT
Java Multithreading
ODP
Multithreading In Java
PDF
Threads concept in java
PPT
Learning Java 3 – Threads and Synchronization
PDF
javathreads
PPT
Thread model in java
PPTX
Java Multi Thead Programming
PPT
12 multi-threading
 
PPT
Threads in Java
PPT
Java Threads and Concurrency
PPTX
Multithreading in java
PDF
Programming with Threads in Java
PDF
Java threads
PPT
Basic of Multithreading in JAva
PPT
Java thread
PPTX
PPTX
Multithreading in java
PPTX
MULTI THREADING IN JAVA
Chap2 2 1
Multithreading Concepts
Java Multithreading
Multithreading In Java
Threads concept in java
Learning Java 3 – Threads and Synchronization
javathreads
Thread model in java
Java Multi Thead Programming
12 multi-threading
 
Threads in Java
Java Threads and Concurrency
Multithreading in java
Programming with Threads in Java
Java threads
Basic of Multithreading in JAva
Java thread
Multithreading in java
MULTI THREADING IN JAVA
Ad

Viewers also liked (19)

PPTX
Multithreading in java
DOCX
Java Code For Sample Projects I/O
PPTX
Thread lifecycle
PDF
Correct and efficient synchronization of java thread
PDF
My History
PPTX
Thread presentation
PDF
Threading Programming Guide
PPT
Operating System-Threads-Galvin
PDF
Economic Peace Paper - IKV and YPRI
PPTX
How Cloud Computing is changing the Automotive Industry - KNOWARTH
PPTX
Power point sumi
PDF
Deutsche GRI Wohnen 2015 Brochure
PPTX
SWISS BULLION
PPTX
SWISS BULLION TABLES
PDF
Carousel30: Optimizing for the mobile user experience whitepaper
PDF
Seller Specific Strategy Report
PDF
Achieve Summer 2015 WEB small
PPTX
Migrating Magento 1.x to Magento 2.0
DOCX
Itani.docx cv
Multithreading in java
Java Code For Sample Projects I/O
Thread lifecycle
Correct and efficient synchronization of java thread
My History
Thread presentation
Threading Programming Guide
Operating System-Threads-Galvin
Economic Peace Paper - IKV and YPRI
How Cloud Computing is changing the Automotive Industry - KNOWARTH
Power point sumi
Deutsche GRI Wohnen 2015 Brochure
SWISS BULLION
SWISS BULLION TABLES
Carousel30: Optimizing for the mobile user experience whitepaper
Seller Specific Strategy Report
Achieve Summer 2015 WEB small
Migrating Magento 1.x to Magento 2.0
Itani.docx cv
Ad

Similar to Java Thread & Multithreading (20)

PPTX
Object-Oriented-Prog_MultiThreading.pptx
PDF
threads (1).pdfmjlkjfwjgliwiufuaiusyroayr
PDF
Threads lecture slides for operating systems
PDF
Pthread
PDF
Threads operating system slides easy understand
PPTX
OS Module-2.pptx
PPTX
W-9.pptx
PPTX
Operating system 20 threads
PPTX
Threading.pptx
PPT
Multithreading
 
PPT
PPT
PPT
multithreading
DOC
PPT
Java multithreading
PDF
Parallel and Distributed Computing chapter 3
DOC
Wiki 2
PPTX
Chapter 3 chapter reading task
PPT
4.Process.ppt
PPTX
Lecture 3 threads
Object-Oriented-Prog_MultiThreading.pptx
threads (1).pdfmjlkjfwjgliwiufuaiusyroayr
Threads lecture slides for operating systems
Pthread
Threads operating system slides easy understand
OS Module-2.pptx
W-9.pptx
Operating system 20 threads
Threading.pptx
Multithreading
 
multithreading
Java multithreading
Parallel and Distributed Computing chapter 3
Wiki 2
Chapter 3 chapter reading task
4.Process.ppt
Lecture 3 threads

More from jehan1987 (7)

PPTX
Algorithm analysis (All in one)
PPTX
Artifitial intelligence (ai) all in one
PPTX
Java interfaces
PPTX
Complete java swing
PPTX
Object oriented programming in C++
PPTX
Data structure and algorithm All in One
PPTX
Assessment of project management practices in pakistani software industry
Algorithm analysis (All in one)
Artifitial intelligence (ai) all in one
Java interfaces
Complete java swing
Object oriented programming in C++
Data structure and algorithm All in One
Assessment of project management practices in pakistani software industry

Recently uploaded (20)

PDF
Anesthesia in Laparoscopic Surgery in India
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Cell Types and Its function , kingdom of life
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Complications of Minimal Access Surgery at WLH
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Business Ethics Teaching Materials for college
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Institutional Correction lecture only . . .
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Anesthesia in Laparoscopic Surgery in India
VCE English Exam - Section C Student Revision Booklet
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Cell Types and Its function , kingdom of life
human mycosis Human fungal infections are called human mycosis..pptx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Complications of Minimal Access Surgery at WLH
TR - Agricultural Crops Production NC III.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Classroom Observation Tools for Teachers
PPH.pptx obstetrics and gynecology in nursing
Business Ethics Teaching Materials for college
Module 4: Burden of Disease Tutorial Slides S2 2025
Supply Chain Operations Speaking Notes -ICLT Program
Institutional Correction lecture only . . .
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...

Java Thread & Multithreading

  • 2. • In computer science, a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler (typically as part of an operating system).
  • 3. • multithreaded programming language which means we can develop multithreaded program using Java. A multithreaded program contains two or more parts that can run concurrently and each part can handle different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs.
  • 4. • Multithreading is the ability of a program or an operating system process to manage its use by more than one user at a time and to even manage multiple requests by the same user without having to have multiple copies of the programming running in the computer. 235
  • 5. Multi-threaded applications • Responsiveness: Multi-threading has the ability for an application to remain responsive to input. In a single- threaded program, if the main execution thread blocks on a long-running task, the entire application can appear to freeze. By moving such long-running tasks to a worker thread that runs concurrently with the main execution thread, it is possible for the application to remain responsive to user input while executing tasks in the background. On the other hand, in most cases multithreading is not the only way to keep a program responsive, with non-blocking I/O and/or Unix signals being available for gaining similar results
  • 6. • Faster Execution: This advantage of a multithreaded program allows it to operate faster on computer systems that have multiple or multi-core CPUs, or across a cluster of machines, because the threads of the program naturally lend themselves to truly concurrent execution.
  • 7. • Less Resource Intensive: Using threads, an application can serve multiple clients concurrently using less resource than it would need when using multiple process copies of itself. For example, the Apache HTTP server, which uses a pool of listener and server threads for listening to incoming requests and processing these
  • 8. • . Better System Utilization: Multi-threaded applications can also utilize the system better. For example, a file-system using multiple threads can achieve higher throughput and lower latency since data in faster mediums like the cache can be delivered earlier while waiting for a slower medium to retrieve the data.
  • 9. • Simplified Sharing and Communication: Unlike processes, which require message passing or shared memory to perform inter- process communication, communication between threads is very simple. Threads automatically share the data, code and files and so, communication is vastly simplified.
  • 10. • Parallelization: Applications looking to utilize multi-core and multi-CPU systems can use multi- threading to split data and tasks into parallel sub- tasks and let the underlying architecture manage how the threads run, either concurrently on a single core or in parallel on multiple cores. GPU computing environments like CUDA and OpenCL use the multi-threading model where dozens to hundreds of threads run in parallel on a large number of cores.
  • 11. • Multi-threading has the following drawbacks: • Synchronization: Since threads share the same address space, the programmer must be careful to avoid race conditions and other non-intuitive behaviors. In order for data to be correctly manipulated, threads will often need to rendezvous in time in order to process the data in the correct order. Threads may also require mutually exclusive operations (often implemented using semaphores) in order to prevent common data from being simultaneously modified or read while in the process of being modified. Careless use of such primitives can lead to deadlocks. • Thread crashes Process: An illegal operation performed by a thread crashes the entire process and so, one misbehaving thread can disrupt the processing of all the other threads in the application.
  • 13. Java library for threading • import java.util.Random;
  • 16. Two ways for Thread Creation
  • 17. Implementing thread using Runnable interface • public class thread1 implements Runnable{ • @Override • public void run() • { • System.out.println("hello world"); • } • public static void main(String args[]) { • //(new Thread(new hello_thread())).start(); • Thread t=new Thread(new thread1()); • t.start(); • • } • }
  • 19. Creating thread using Extending Thread class • public class thread2 extends Thread{ • public void run() • { • System.out.println("helloworld"); • } • public static void main(String[] args) • { • thread2 t=new thread2(); • t.start(); • } • • }
  • 21. • Can we start a thread twice? • No. After staring a thread, it can never be started again. If you does so, an IllegalThreadStateException is thrown.
  • 27. Multithreaded Program for calculator • public class calc_add extends Thread • { • @Override • public void run() • { • • try • { • int sum; • int a=2; • int b=4; • sum=a+b; • System.out.println(sum); • Thread.sleep(8000); • } • catch(Exception e) • { • System.out.println(e); • } • } • }
  • 28. • public class calc_mul extends Thread { • public void run() • { • • try • { • int mul; • int a=2; • int b=4; • mul=a*b; • System.out.print(mul); • Thread.sleep(2000); • } • catch(Exception e) • { • System.out.println(e); • } • • }}
  • 29. • public class calc_min extends Thread{ • public void run() • { • • try • { • int minus; • int a=2; • int b=4; • minus=a-b; • System.out.print(minus); • Thread.sleep(2000); • } • catch(Exception e) • { • System.out.println(e); • } • • }}
  • 30. • public class calc_main { • • public static void main(String[] args) • { • calc_add t1=new calc_add(); • calc_mul t2=new calc_mul(); • calc_min t3=new calc_min(); • t1.start(); • t2.start(); • t3.start(); • } • }