際際滷

際際滷Share a Scribd company logo
GYAN GANGA COLLAGE OFGYAN GANGA COLLAGE OF
TECHNOLOGYTECHNOLOGY
Bhoomika Thakur
Roll no:0208cs091024
2
Processes & Threads
PROCESS :
 A process consists of an execution
environment together with one or more
threads.
 An execution environment is the unit of
resource management: a collection of local
kernel managed resources to which its
threads have access.
3
Processes and Threads
Threads :
 Threads are schedulable activities
attached to processes.
 Threads can be helpful within servers:
 Concurrent processing of clients
requests can reduce the tendency for
servers to become bottleneck.
 E.g. one thread can process a clients request while a
second thread serving another request waits for a disk
access to complete.
4
Client & Server With Threads
Server
N threads
Input-output
Client
Thread 2 makes
T1
Thread 1
requests to server
generates
results
Requests
Receipt &
queuing
5
Processes and Threads
Processes vs. Threads
1. Processes are heavy
weighted & Threads are light
weighted.
2. Processes are expensive to
create but threads are easier to
create and destroy.
6
 Thread programming is concurrent
programming.
 Java provides methods for creating,
destroying and synchronizing threads.
 The Java thread class includes the
management methods and object
synchronization methods .
Alive
Thread State Diagram
New Thread Dead Thread
Running
Runnable
new CounterThread1(max);
run() method returns
while () {  }
Blocked
Object.wait()
Thread.sleep()
blocking IO call
waiting on a monitor
cntThread.start();
8
Java Thread Methods
1.setPriority(int newPriority) : Set return the threads
priority.
2. getPriority(): Return the threads priority.
3.run() : A thread executes the run() method .
4.start() : It starts the thread by calling run() method.
5.sleep(int millisecs) :Suspend a thread for a period of time.
6.yield() : Enter the READY state and invoke the scheduler.
7.destroy() : Destroy the thread.
9
Java Thread Methods
8.thread.join(int millisecs): Blocks the calling thread for up
to the specified time until thread has terminated.
9.thread.interrupt(): Interrupts thread: causes it to return
from a blocking method call such as sleep().
10.object.wait(long millisecs, int nanosecs):
Blocks the calling thread until a call made to notify() or
notifyAll() on object wakes the thread, or the thread is
interrupted, or the specified time has elapsed.
11.object.notify(), object.notifyAll()
Wakes, respectively, one or all of any threads
that have called wait() on object.
1.By creating thread class:
Class mythread extends Thread
{
}
2.By converting a class to thread:
Class myThread implements runnable
{
}
java.lang.Runnable
 An adapter which enables any given class to
operate as a thread.
 Requires one method be implemented:
 public void run()
 Most common method for adding threads
to an application.
 Prevents certain thread operations
(e.g. Thread.isAlive()).
java.lang.ThreadGroup
 When an exception is emitted by the
Thread.run method, the method
ThreadGroup.uncaughtException is called.
 Derive a class from ThreadGroup.
 Override the uncaughtException
method, relaying the thread and
exception information.
 Java provides multithreading.
 Threads are light weight ,they share the same
address space & share the same heavy wt. process.
 In java a main thread is created which controls the
other threads.
 wait() : This method tells
the calling thread to give up
the monitor & go to sleep
until some other threads
enter the same monitor &
calls notify().
 notify(): Wakes up the first
thread that called wait() on
the same object.
 notifyAll() :Wakes up all the
thread that called wait() on
the same object ,the highest
priority thread will run first.
Exception Handling
 When an exception is emitted out of the run() method,
the thread is terminated.
 The exception is consumed by the JVM because once
Thread.start() is called, the created thread is split from
the caller.
REFERENCES
&
WIKIPEDIA
QUERRIES
THANKYOU

More Related Content

What's hot (20)

Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
Tim Penhey
Thread
ThreadThread
Thread
Sajid Hussain
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
Haldia Institute of Technology
Multithreading
MultithreadingMultithreading
Multithreading
sagsharma
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
Neeraj Kaushik
Thread 1
Thread 1Thread 1
Thread 1
RAVI MAURYA
Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)
Prakhar Maurya
Thread presentation
Thread presentationThread presentation
Thread presentation
AAshish Ojha
Multithread
MultithreadMultithread
Multithread
vuhaininh88
MULTITHREADING CONCEPT
MULTITHREADING CONCEPTMULTITHREADING CONCEPT
MULTITHREADING CONCEPT
RAVI MAURYA
Thread priorities35
Thread priorities35Thread priorities35
Thread priorities35
myrajendra
Python multithreaded programming
Python   multithreaded programmingPython   multithreaded programming
Python multithreaded programming
Learnbay Datascience
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Sachintha Gunasena
Chapter 4 - Threads
Chapter 4 - ThreadsChapter 4 - Threads
Chapter 4 - Threads
Wayne Jones Jnr
Multi Threading
Multi ThreadingMulti Threading
Multi Threading
Ferdin Joe John Joseph PhD
Thread (Operating System)
Thread  (Operating System)Thread  (Operating System)
Thread (Operating System)
kiran Patel
Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in Java
Allan Huang
Multithreading
MultithreadingMultithreading
Multithreading
GowriLatha1
Multi threading
Multi threadingMulti threading
Multi threading
Maryam Ansari
Threading
ThreadingThreading
Threading
Melick Baranasooriya
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
Tim Penhey
Multithreading
MultithreadingMultithreading
Multithreading
sagsharma
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
Neeraj Kaushik
Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)
Prakhar Maurya
Thread presentation
Thread presentationThread presentation
Thread presentation
AAshish Ojha
MULTITHREADING CONCEPT
MULTITHREADING CONCEPTMULTITHREADING CONCEPT
MULTITHREADING CONCEPT
RAVI MAURYA
Thread priorities35
Thread priorities35Thread priorities35
Thread priorities35
myrajendra
Python multithreaded programming
Python   multithreaded programmingPython   multithreaded programming
Python multithreaded programming
Learnbay Datascience
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Sachintha Gunasena
Thread (Operating System)
Thread  (Operating System)Thread  (Operating System)
Thread (Operating System)
kiran Patel
Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in Java
Allan Huang
Multithreading
MultithreadingMultithreading
Multithreading
GowriLatha1

Similar to Threads in java (20)

Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
BalasundaramSr
Java Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHatJava Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHat
Scholarhat
Java Threads
Java ThreadsJava Threads
Java Threads
Hamid Ghorbani
Java threading
Java threadingJava threading
Java threading
Chinh Ngo Nguyen
際際滷 7 Thread-1.pptx
際際滷 7 Thread-1.pptx際際滷 7 Thread-1.pptx
際際滷 7 Thread-1.pptx
ajmalhamidi1380
Thread model in java
Thread model in javaThread model in java
Thread model in java
AmbigaMurugesan
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
Vikram Kalyani
Multithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming languageMultithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming language
arnavytstudio2814
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
bleh23
Java Thread
Java ThreadJava Thread
Java Thread
DeeptiJava
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbjava.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
jakjak36
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
nimbalkarvikram966
Threads in Java
Threads in JavaThreads in Java
Threads in Java
HarshaDokula
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
ssusere538f7
Concept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptxConcept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptx
SahilKumar542
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
Arulmozhivarman8
U4 JAVA.pptx
U4 JAVA.pptxU4 JAVA.pptx
U4 JAVA.pptx
madan r
java threads for easy learn and develop
java  threads for easy learn and developjava  threads for easy learn and develop
java threads for easy learn and develop
pavanrock859
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01
Aravindharamanan S
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
Rakesh Jha
Java Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHatJava Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHat
Scholarhat
際際滷 7 Thread-1.pptx
際際滷 7 Thread-1.pptx際際滷 7 Thread-1.pptx
際際滷 7 Thread-1.pptx
ajmalhamidi1380
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
Vikram Kalyani
Multithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming languageMultithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming language
arnavytstudio2814
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
bleh23
Java Thread
Java ThreadJava Thread
Java Thread
DeeptiJava
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbjava.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
jakjak36
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
nimbalkarvikram966
Threads in Java
Threads in JavaThreads in Java
Threads in Java
HarshaDokula
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
ssusere538f7
Concept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptxConcept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptx
SahilKumar542
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
Arulmozhivarman8
U4 JAVA.pptx
U4 JAVA.pptxU4 JAVA.pptx
U4 JAVA.pptx
madan r
java threads for easy learn and develop
java  threads for easy learn and developjava  threads for easy learn and develop
java threads for easy learn and develop
pavanrock859
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01
Aravindharamanan S
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
Rakesh Jha

Recently uploaded (20)

GROUP-3-GRID-CODE-AND-DISTRIBUTION-CODE.pptx
GROUP-3-GRID-CODE-AND-DISTRIBUTION-CODE.pptxGROUP-3-GRID-CODE-AND-DISTRIBUTION-CODE.pptx
GROUP-3-GRID-CODE-AND-DISTRIBUTION-CODE.pptx
meneememoo
GM Meeting 070225 TO 130225 for 2024.pptx
GM Meeting 070225 TO 130225 for 2024.pptxGM Meeting 070225 TO 130225 for 2024.pptx
GM Meeting 070225 TO 130225 for 2024.pptx
crdslalcomumbai
CFOT Fiber Optics FOA CERTIFICATION.pptx
CFOT Fiber Optics FOA CERTIFICATION.pptxCFOT Fiber Optics FOA CERTIFICATION.pptx
CFOT Fiber Optics FOA CERTIFICATION.pptx
MohamedShabana37
04 MAINTENANCE OF CONCRETE PAVEMENTS.ppt
04  MAINTENANCE OF CONCRETE PAVEMENTS.ppt04  MAINTENANCE OF CONCRETE PAVEMENTS.ppt
04 MAINTENANCE OF CONCRETE PAVEMENTS.ppt
sreenath seenu
UNIT 1FUNDAMENTALS OF OPERATING SYSTEMS.pptx
UNIT 1FUNDAMENTALS OF OPERATING SYSTEMS.pptxUNIT 1FUNDAMENTALS OF OPERATING SYSTEMS.pptx
UNIT 1FUNDAMENTALS OF OPERATING SYSTEMS.pptx
KesavanT10
Equipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding ProcessEquipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding Process
AhmadKamil87
Lecture -3 Cold water supply system.pptx
Lecture -3 Cold water supply system.pptxLecture -3 Cold water supply system.pptx
Lecture -3 Cold water supply system.pptx
rabiaatif2
Cloud Computing concepts and technologies
Cloud Computing concepts and technologiesCloud Computing concepts and technologies
Cloud Computing concepts and technologies
ssuser4c9444
Industrial Valves, Instruments Products Profile
Industrial Valves, Instruments Products ProfileIndustrial Valves, Instruments Products Profile
Industrial Valves, Instruments Products Profile
zebcoeng
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
Thane Heins NOBEL PRIZE WINNING ENERGY RESEARCHER
Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...
Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...
Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...
ASHISHDESAI85
CS3451-OPERATING-SYSTEM NOTES ALL123.pdf
CS3451-OPERATING-SYSTEM NOTES ALL123.pdfCS3451-OPERATING-SYSTEM NOTES ALL123.pdf
CS3451-OPERATING-SYSTEM NOTES ALL123.pdf
PonniS7
Air pollution is contamination of the indoor or outdoor environment by any ch...
Air pollution is contamination of the indoor or outdoor environment by any ch...Air pollution is contamination of the indoor or outdoor environment by any ch...
Air pollution is contamination of the indoor or outdoor environment by any ch...
dhanashree78
Cyber Security_ Protecting the Digital World.pptx
Cyber Security_ Protecting the Digital World.pptxCyber Security_ Protecting the Digital World.pptx
Cyber Security_ Protecting the Digital World.pptx
Harshith A S
Embedded System intro Embedded System intro.ppt
Embedded System intro Embedded System intro.pptEmbedded System intro Embedded System intro.ppt
Embedded System intro Embedded System intro.ppt
23ucc580
Soil Properties and Methods of Determination
Soil Properties and  Methods of DeterminationSoil Properties and  Methods of Determination
Soil Properties and Methods of Determination
Rajani Vyawahare
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
Thane Heins NOBEL PRIZE WINNING ENERGY RESEARCHER
Power Point Presentation for Electrical Engineering 3-phase.ppt
Power Point Presentation for Electrical Engineering 3-phase.pptPower Point Presentation for Electrical Engineering 3-phase.ppt
Power Point Presentation for Electrical Engineering 3-phase.ppt
Aniket_1415
The Golden Gate Bridge a structural marvel inspired by mother nature.pptx
The Golden Gate Bridge a structural marvel inspired by mother nature.pptxThe Golden Gate Bridge a structural marvel inspired by mother nature.pptx
The Golden Gate Bridge a structural marvel inspired by mother nature.pptx
AkankshaRawat75
CONTRACTOR ALL RISK INSURANCESAR (1).ppt
CONTRACTOR ALL RISK INSURANCESAR (1).pptCONTRACTOR ALL RISK INSURANCESAR (1).ppt
CONTRACTOR ALL RISK INSURANCESAR (1).ppt
suaktonny
GROUP-3-GRID-CODE-AND-DISTRIBUTION-CODE.pptx
GROUP-3-GRID-CODE-AND-DISTRIBUTION-CODE.pptxGROUP-3-GRID-CODE-AND-DISTRIBUTION-CODE.pptx
GROUP-3-GRID-CODE-AND-DISTRIBUTION-CODE.pptx
meneememoo
GM Meeting 070225 TO 130225 for 2024.pptx
GM Meeting 070225 TO 130225 for 2024.pptxGM Meeting 070225 TO 130225 for 2024.pptx
GM Meeting 070225 TO 130225 for 2024.pptx
crdslalcomumbai
CFOT Fiber Optics FOA CERTIFICATION.pptx
CFOT Fiber Optics FOA CERTIFICATION.pptxCFOT Fiber Optics FOA CERTIFICATION.pptx
CFOT Fiber Optics FOA CERTIFICATION.pptx
MohamedShabana37
04 MAINTENANCE OF CONCRETE PAVEMENTS.ppt
04  MAINTENANCE OF CONCRETE PAVEMENTS.ppt04  MAINTENANCE OF CONCRETE PAVEMENTS.ppt
04 MAINTENANCE OF CONCRETE PAVEMENTS.ppt
sreenath seenu
UNIT 1FUNDAMENTALS OF OPERATING SYSTEMS.pptx
UNIT 1FUNDAMENTALS OF OPERATING SYSTEMS.pptxUNIT 1FUNDAMENTALS OF OPERATING SYSTEMS.pptx
UNIT 1FUNDAMENTALS OF OPERATING SYSTEMS.pptx
KesavanT10
Equipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding ProcessEquipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding Process
AhmadKamil87
Lecture -3 Cold water supply system.pptx
Lecture -3 Cold water supply system.pptxLecture -3 Cold water supply system.pptx
Lecture -3 Cold water supply system.pptx
rabiaatif2
Cloud Computing concepts and technologies
Cloud Computing concepts and technologiesCloud Computing concepts and technologies
Cloud Computing concepts and technologies
ssuser4c9444
Industrial Valves, Instruments Products Profile
Industrial Valves, Instruments Products ProfileIndustrial Valves, Instruments Products Profile
Industrial Valves, Instruments Products Profile
zebcoeng
Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...
Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...
Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...
ASHISHDESAI85
CS3451-OPERATING-SYSTEM NOTES ALL123.pdf
CS3451-OPERATING-SYSTEM NOTES ALL123.pdfCS3451-OPERATING-SYSTEM NOTES ALL123.pdf
CS3451-OPERATING-SYSTEM NOTES ALL123.pdf
PonniS7
Air pollution is contamination of the indoor or outdoor environment by any ch...
Air pollution is contamination of the indoor or outdoor environment by any ch...Air pollution is contamination of the indoor or outdoor environment by any ch...
Air pollution is contamination of the indoor or outdoor environment by any ch...
dhanashree78
Cyber Security_ Protecting the Digital World.pptx
Cyber Security_ Protecting the Digital World.pptxCyber Security_ Protecting the Digital World.pptx
Cyber Security_ Protecting the Digital World.pptx
Harshith A S
Embedded System intro Embedded System intro.ppt
Embedded System intro Embedded System intro.pptEmbedded System intro Embedded System intro.ppt
Embedded System intro Embedded System intro.ppt
23ucc580
Soil Properties and Methods of Determination
Soil Properties and  Methods of DeterminationSoil Properties and  Methods of Determination
Soil Properties and Methods of Determination
Rajani Vyawahare
Power Point Presentation for Electrical Engineering 3-phase.ppt
Power Point Presentation for Electrical Engineering 3-phase.pptPower Point Presentation for Electrical Engineering 3-phase.ppt
Power Point Presentation for Electrical Engineering 3-phase.ppt
Aniket_1415
The Golden Gate Bridge a structural marvel inspired by mother nature.pptx
The Golden Gate Bridge a structural marvel inspired by mother nature.pptxThe Golden Gate Bridge a structural marvel inspired by mother nature.pptx
The Golden Gate Bridge a structural marvel inspired by mother nature.pptx
AkankshaRawat75
CONTRACTOR ALL RISK INSURANCESAR (1).ppt
CONTRACTOR ALL RISK INSURANCESAR (1).pptCONTRACTOR ALL RISK INSURANCESAR (1).ppt
CONTRACTOR ALL RISK INSURANCESAR (1).ppt
suaktonny

Threads in java

  • 1. GYAN GANGA COLLAGE OFGYAN GANGA COLLAGE OF TECHNOLOGYTECHNOLOGY Bhoomika Thakur Roll no:0208cs091024
  • 2. 2 Processes & Threads PROCESS : A process consists of an execution environment together with one or more threads. An execution environment is the unit of resource management: a collection of local kernel managed resources to which its threads have access.
  • 3. 3 Processes and Threads Threads : Threads are schedulable activities attached to processes. Threads can be helpful within servers: Concurrent processing of clients requests can reduce the tendency for servers to become bottleneck. E.g. one thread can process a clients request while a second thread serving another request waits for a disk access to complete.
  • 4. 4 Client & Server With Threads Server N threads Input-output Client Thread 2 makes T1 Thread 1 requests to server generates results Requests Receipt & queuing
  • 5. 5 Processes and Threads Processes vs. Threads 1. Processes are heavy weighted & Threads are light weighted. 2. Processes are expensive to create but threads are easier to create and destroy.
  • 6. 6 Thread programming is concurrent programming. Java provides methods for creating, destroying and synchronizing threads. The Java thread class includes the management methods and object synchronization methods .
  • 7. Alive Thread State Diagram New Thread Dead Thread Running Runnable new CounterThread1(max); run() method returns while () { } Blocked Object.wait() Thread.sleep() blocking IO call waiting on a monitor cntThread.start();
  • 8. 8 Java Thread Methods 1.setPriority(int newPriority) : Set return the threads priority. 2. getPriority(): Return the threads priority. 3.run() : A thread executes the run() method . 4.start() : It starts the thread by calling run() method. 5.sleep(int millisecs) :Suspend a thread for a period of time. 6.yield() : Enter the READY state and invoke the scheduler. 7.destroy() : Destroy the thread.
  • 9. 9 Java Thread Methods 8.thread.join(int millisecs): Blocks the calling thread for up to the specified time until thread has terminated. 9.thread.interrupt(): Interrupts thread: causes it to return from a blocking method call such as sleep(). 10.object.wait(long millisecs, int nanosecs): Blocks the calling thread until a call made to notify() or notifyAll() on object wakes the thread, or the thread is interrupted, or the specified time has elapsed. 11.object.notify(), object.notifyAll() Wakes, respectively, one or all of any threads that have called wait() on object.
  • 10. 1.By creating thread class: Class mythread extends Thread { } 2.By converting a class to thread: Class myThread implements runnable { }
  • 11. java.lang.Runnable An adapter which enables any given class to operate as a thread. Requires one method be implemented: public void run() Most common method for adding threads to an application. Prevents certain thread operations (e.g. Thread.isAlive()).
  • 12. java.lang.ThreadGroup When an exception is emitted by the Thread.run method, the method ThreadGroup.uncaughtException is called. Derive a class from ThreadGroup. Override the uncaughtException method, relaying the thread and exception information.
  • 13. Java provides multithreading. Threads are light weight ,they share the same address space & share the same heavy wt. process. In java a main thread is created which controls the other threads.
  • 14. wait() : This method tells the calling thread to give up the monitor & go to sleep until some other threads enter the same monitor & calls notify(). notify(): Wakes up the first thread that called wait() on the same object. notifyAll() :Wakes up all the thread that called wait() on the same object ,the highest priority thread will run first.
  • 15. Exception Handling When an exception is emitted out of the run() method, the thread is terminated. The exception is consumed by the JVM because once Thread.start() is called, the created thread is split from the caller.