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();
• }
• }
Ad

More Related Content

What's hot (20)

Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
Hemo Chella
 
Multithreading Concepts
Multithreading ConceptsMultithreading Concepts
Multithreading Concepts
Arvind Krishnaa
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
Rajkattamuri
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
Muthukumaran Subramanian
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronization
caswenson
 
javathreads
javathreadsjavathreads
javathreads
Arjun Shanka
 
Thread model in java
Thread model in javaThread model in java
Thread model in java
AmbigaMurugesan
 
Java Multi Thead Programming
Java Multi Thead ProgrammingJava Multi Thead Programming
Java Multi Thead Programming
Nishant Mevawala
 
12 multi-threading
12 multi-threading12 multi-threading
12 multi-threading
APU
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
Gaurav Aggarwal
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
Sunil OS
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Lovely Professional University
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Java
koji lin
 
Java threads
Java threadsJava threads
Java threads
Prabhakaran V M
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
suraj pandey
 
Java thread
Java threadJava thread
Java thread
Arati Gadgil
 
multi threading
multi threadingmulti threading
multi threading
Yaswanth Babu Gummadivelli
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Arafat Hossan
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
Rajkattamuri
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronization
caswenson
 
Java Multi Thead Programming
Java Multi Thead ProgrammingJava Multi Thead Programming
Java Multi Thead Programming
Nishant Mevawala
 
12 multi-threading
12 multi-threading12 multi-threading
12 multi-threading
APU
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
Sunil OS
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Java
koji lin
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
suraj pandey
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Arafat Hossan
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
 

Viewers also liked (19)

Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath
 
Java Code For Sample Projects I/O
Java Code For Sample Projects I/OJava Code For Sample Projects I/O
Java Code For Sample Projects I/O
jwjablonski
 
Thread lifecycle
Thread lifecycleThread lifecycle
Thread lifecycle
Somay Nakhal
 
Correct and efficient synchronization of java thread
Correct and efficient synchronization of java threadCorrect and efficient synchronization of java thread
Correct and efficient synchronization of java thread
outofmemoryerror
 
My History
My HistoryMy History
My History
santosh mishra
 
Thread presentation
Thread presentationThread presentation
Thread presentation
AAshish Ojha
 
Threading Programming Guide
Threading Programming GuideThreading Programming Guide
Threading Programming Guide
DEVTYPE
 
Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-Galvin
Sonali Chauhan
 
Economic Peace Paper - IKV and YPRI
Economic Peace Paper - IKV and YPRIEconomic Peace Paper - IKV and YPRI
Economic Peace Paper - IKV and YPRI
Maral Khajeh
 
How Cloud Computing is changing the Automotive Industry - KNOWARTH
How Cloud Computing is changing the Automotive Industry - KNOWARTHHow Cloud Computing is changing the Automotive Industry - KNOWARTH
How Cloud Computing is changing the Automotive Industry - KNOWARTH
KNOWARTH - Software Development Company
 
Power point sumi
Power point sumiPower point sumi
Power point sumi
Sumiyatidede
 
Deutsche GRI Wohnen 2015 Brochure
Deutsche GRI Wohnen 2015 BrochureDeutsche GRI Wohnen 2015 Brochure
Deutsche GRI Wohnen 2015 Brochure
Global Real Estate Institute
 
SWISS BULLION
SWISS BULLIONSWISS BULLION
SWISS BULLION
Yesh Lazarte
 
SWISS BULLION TABLES
SWISS BULLION TABLESSWISS BULLION TABLES
SWISS BULLION TABLES
Yesh Lazarte
 
Carousel30: Optimizing for the mobile user experience whitepaper
Carousel30: Optimizing for the mobile user experience whitepaperCarousel30: Optimizing for the mobile user experience whitepaper
Carousel30: Optimizing for the mobile user experience whitepaper
Carousel30
 
Seller Specific Strategy Report
Seller Specific Strategy ReportSeller Specific Strategy Report
Seller Specific Strategy Report
Arron Groomes
 
Migrating Magento 1.x to Magento 2.0
Migrating Magento 1.x to Magento 2.0Migrating Magento 1.x to Magento 2.0
Migrating Magento 1.x to Magento 2.0
KNOWARTH - Software Development Company
 
Itani.docx cv
Itani.docx cvItani.docx cv
Itani.docx cv
Itani Malema
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath
 
Java Code For Sample Projects I/O
Java Code For Sample Projects I/OJava Code For Sample Projects I/O
Java Code For Sample Projects I/O
jwjablonski
 
Correct and efficient synchronization of java thread
Correct and efficient synchronization of java threadCorrect and efficient synchronization of java thread
Correct and efficient synchronization of java thread
outofmemoryerror
 
Thread presentation
Thread presentationThread presentation
Thread presentation
AAshish Ojha
 
Threading Programming Guide
Threading Programming GuideThreading Programming Guide
Threading Programming Guide
DEVTYPE
 
Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-Galvin
Sonali Chauhan
 
Economic Peace Paper - IKV and YPRI
Economic Peace Paper - IKV and YPRIEconomic Peace Paper - IKV and YPRI
Economic Peace Paper - IKV and YPRI
Maral Khajeh
 
SWISS BULLION TABLES
SWISS BULLION TABLESSWISS BULLION TABLES
SWISS BULLION TABLES
Yesh Lazarte
 
Carousel30: Optimizing for the mobile user experience whitepaper
Carousel30: Optimizing for the mobile user experience whitepaperCarousel30: Optimizing for the mobile user experience whitepaper
Carousel30: Optimizing for the mobile user experience whitepaper
Carousel30
 
Seller Specific Strategy Report
Seller Specific Strategy ReportSeller Specific Strategy Report
Seller Specific Strategy Report
Arron Groomes
 
Ad

Similar to Java Thread & Multithreading (20)

Object-Oriented-Prog_MultiThreading.pptx
Object-Oriented-Prog_MultiThreading.pptxObject-Oriented-Prog_MultiThreading.pptx
Object-Oriented-Prog_MultiThreading.pptx
NasreenTaj20
 
Threads lecture slides for operating systems
Threads lecture slides for operating systemsThreads lecture slides for operating systems
Threads lecture slides for operating systems
amirtarek401
 
Pthread
PthreadPthread
Pthread
Gopi Saiteja
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
bleh23
 
W-9.pptx
W-9.pptxW-9.pptx
W-9.pptx
alianwarr
 
Operating system 20 threads
Operating system 20 threadsOperating system 20 threads
Operating system 20 threads
Vaibhav Khanna
 
Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
BalasundaramSr
 
Multithreading
MultithreadingMultithreading
Multithreading
F K
 
Java
JavaJava
Java
Khasim Cise
 
Java
JavaJava
Java
mdfkhan625
 
multithreading
multithreadingmultithreading
multithreading
Rajkattamuri
 
Threads
ThreadsThreads
Threads
chrisjosewanjira
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
Mohammed625
 
Parallel and Distributed Computing chapter 3
Parallel and Distributed Computing chapter 3Parallel and Distributed Computing chapter 3
Parallel and Distributed Computing chapter 3
AbdullahMunir32
 
Wiki 2
Wiki 2Wiki 2
Wiki 2
Sid Hegde
 
Chapter 3 chapter reading task
Chapter 3 chapter reading taskChapter 3 chapter reading task
Chapter 3 chapter reading task
Grievous Humorist-Ilham
 
4.Process.ppt
4.Process.ppt4.Process.ppt
4.Process.ppt
AkfeteAssefa
 
Lecture 3 threads
Lecture 3   threadsLecture 3   threads
Lecture 3 threads
Kumbirai Junior Muzavazi
 
Scheduling Thread
Scheduling  ThreadScheduling  Thread
Scheduling Thread
MuhammadBilal187526
 
introduction to java Multithreading presentation.pptx
introduction to  java Multithreading presentation.pptxintroduction to  java Multithreading presentation.pptx
introduction to java Multithreading presentation.pptx
ArunPatrick2
 
Object-Oriented-Prog_MultiThreading.pptx
Object-Oriented-Prog_MultiThreading.pptxObject-Oriented-Prog_MultiThreading.pptx
Object-Oriented-Prog_MultiThreading.pptx
NasreenTaj20
 
Threads lecture slides for operating systems
Threads lecture slides for operating systemsThreads lecture slides for operating systems
Threads lecture slides for operating systems
amirtarek401
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
bleh23
 
Operating system 20 threads
Operating system 20 threadsOperating system 20 threads
Operating system 20 threads
Vaibhav Khanna
 
Multithreading
MultithreadingMultithreading
Multithreading
F K
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
Mohammed625
 
Parallel and Distributed Computing chapter 3
Parallel and Distributed Computing chapter 3Parallel and Distributed Computing chapter 3
Parallel and Distributed Computing chapter 3
AbdullahMunir32
 
introduction to java Multithreading presentation.pptx
introduction to  java Multithreading presentation.pptxintroduction to  java Multithreading presentation.pptx
introduction to java Multithreading presentation.pptx
ArunPatrick2
 
Ad

More from jehan1987 (7)

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

Recently uploaded (20)

Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
Tax evasion, Tax planning & Tax avoidance.pptx
Tax evasion, Tax  planning &  Tax avoidance.pptxTax evasion, Tax  planning &  Tax avoidance.pptx
Tax evasion, Tax planning & Tax avoidance.pptx
manishbaidya2017
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18
Celine George
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
How to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 SalesHow to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 Sales
Celine George
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
Lecture 4 INSECT CUTICLE and moulting.pptx
Lecture 4 INSECT CUTICLE and moulting.pptxLecture 4 INSECT CUTICLE and moulting.pptx
Lecture 4 INSECT CUTICLE and moulting.pptx
Arshad Shaikh
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
Computer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issuesComputer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issues
Abhijit Bodhe
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE  BY sweety Tamanna Mahapatra MSc PediatricAPGAR SCORE  BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
SweetytamannaMohapat
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
Tax evasion, Tax planning & Tax avoidance.pptx
Tax evasion, Tax  planning &  Tax avoidance.pptxTax evasion, Tax  planning &  Tax avoidance.pptx
Tax evasion, Tax planning & Tax avoidance.pptx
manishbaidya2017
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18
Celine George
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
How to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 SalesHow to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 Sales
Celine George
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
Lecture 4 INSECT CUTICLE and moulting.pptx
Lecture 4 INSECT CUTICLE and moulting.pptxLecture 4 INSECT CUTICLE and moulting.pptx
Lecture 4 INSECT CUTICLE and moulting.pptx
Arshad Shaikh
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
Computer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issuesComputer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issues
Abhijit Bodhe
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE  BY sweety Tamanna Mahapatra MSc PediatricAPGAR SCORE  BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
SweetytamannaMohapat
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 

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(); • } • }
  翻译: