SlideShare a Scribd company logo
Today’s Agenda
 Quick Review
 Finish Java Threads
 The CS Problem
Advanced Topics in Software Engineering 1
Quick Review
 What are the two approaches to creating Java
threads?
Advanced Topics in Software Engineering 2
Advanced Topics in Software Engineering 3
Java Threads
 Thread Creation
 Thread Synchronization
 Thread States And Scheduling
 Short Demo
Advanced Topics in Software Engineering 4
Thread Creation
There are two ways to create a thread in Java:
 Extend the Thread class
 Implement the Runnable interface
Advanced Topics in Software Engineering 5
The Thread class
class A extends Thread {
public A (String name) { super (name); }
public void run () {
System.out.println(“My name is ” + getName());
}
}
class B {
public static void main (String [] args) {
A a = new A (“mud”);
a.start ();
}
}
Advanced Topics in Software Engineering 6
The Runnable interface
class A extends ... implements Runnable {
public void run () {
System.out.println(“My name is ” + getName () );
}
}
class B {
public static void main (String [] args) {
A a = new A ();
Thread t = new Thread (a, “mud, too”);
t.start ();
}
}
Advanced Topics in Software Engineering 7
Java Threads
 Thread Creation
 Thread Synchronization
 Thread States And Scheduling
 Short Demo
Advanced Topics in Software Engineering 8
Lock
Each Java object is implicitly associated with a lock.
To invoke a synchronized method of an object, a
thread must obtain the lock associated with this
object.
The lock is not released until the execution of the
method completes.
The locking mechanism ensures that at any given
time, at most one thread can execute any
synchronized method of an object.
Important: Lock is per object (NOT per method)!
Advanced Topics in Software Engineering 9
wait, notify and notifyAll
The execution of wait on an object causes the
current thread to wait until some other thread to
call notify or notifyAll.
A thread must own the object lock before it invokes
wait on an object. The execution of wait will also
release the lock.
When a waiting thread is notified, it has to compete
and reacquire the object lock before it continues
execution.
What’s the difference between notify and notifyAll?
Advanced Topics in Software Engineering 10
join
A thread t1 can wait until another thread t2 to
terminate.
t1 t2
t2.join ()
end
Advanced Topics in Software Engineering 11
interrupt
interrupt allows one thread to send a signal to
another thread.
It will set the thread’s interrupt status flag, and will
throw a ThreadInterrupted exception if necessary .
The receiver thread can check the status flag or
catch the exception, and then take appropriate
actions.
Advanced Topics in Software Engineering 12
Other Thread methods
Method sleep puts the running thread into sleep,
releasing the CPU.
Method suspend suspends the execution of a thread,
which can be resumed later by another thread using
method resume.
Method stop ends the execution of a thread.
Note that suspend, resume, and stop has been
deprecated in Java 2. (For more info, refer to
http://java.sun.com/j2se/1.4.1/docs/guide/misc/threadPrimitiveDeprecation.html.)
Advanced Topics in Software Engineering 13
Daemon Thread
A daemon thread is used to perform some services
(e.g. cleanup) for other threads.
Any thread can be marked as a daemon thread using
setDaemon (true).
A program terminates when all its non-daemon
threads terminate, meaning that daemon threads die
when all non-daemon threads die.
Advanced Topics in Software Engineering 14
Java Threads
 Thread Creation
 Thread Synchronization
 Thread States And Scheduling
 Short Demo
Advanced Topics in Software Engineering 15
State Transition Diagram
suspend
blocked
new runnable running
suspended blocked
dead
start scheduled
CS, yield
IO, sleep
wait, join
s
t
o
p
suspend
stop, run ends
s
u
s
p
e
n
d
r
e
s
u
m
e
stop
IO completes,
sleep expires,
notify, notifyAll,
join completes
IO
com
pletes
stop
s
u
s
p
e
n
d
stop
resume
Advanced Topics in Software Engineering 16
Scheduling
In general, there are two types of scheduling: non-
preemptive scheduling, and preemptive scheduling.
In non-preemptive scheduling, a thread runs until it
terminates, stops, blocks, suspends, or yields.
In preemptive scheduling, even if the current thread
is still running, a context switch will occur when its
time slice is used up.
Advanced Topics in Software Engineering 17
Priorities
Each thread has a priority that also affects their
scheduling to run.
If a thread of a higher priority enters the runnable
set, the currently running thread will be preempted
by the new thread.
Advanced Topics in Software Engineering 18
Java Threads
 Thread Creation
 Thread Synchronization
 Thread States And Scheduling
 Short Demo

More Related Content

Similar to java threads for easy learn and develop (20)

PPTX
Multithreading in java
Monika Mishra
 
PPTX
MULTI THREADING IN JAVA
VINOTH R
 
PDF
Java threading
Chinh Ngo Nguyen
 
PPTX
Multithreading
sagsharma
 
PPTX
Multithreading in java
JanmejayaPadhiary2
 
PPTX
Slide 7 Thread-1.pptx
ajmalhamidi1380
 
PPTX
econtent thread in java.pptx
ramyan49
 
PDF
Java Threads: Lightweight Processes
Isuru Perera
 
PPTX
Object-Oriented-Prog_MultiThreading.pptx
NasreenTaj20
 
PPTX
84694646456445645645645665656465464sdd.pptx
mhp821023
 
PPTX
Multithreadingppt.pptx
HKShab
 
PDF
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
universitypresidency
 
PPTX
Multithreading
SanthiNivas
 
PPT
Threads
Abhishek Khune
 
PPTX
multithreading to be used in java with good programs.pptx
PriyadharshiniG41
 
PPT
Session 7_MULTITHREADING in java example.ppt
TabassumMaktum
 
PPT
Java multi threading
Raja Sekhar
 
PPT
Lec7!JavaThreads.ppt
HemantSharma134028
 
PPT
Lec7!JavaThreads.ppt java multithreading
kavitamittal18
 
PPTX
Multithreading in java
Kavitha713564
 
Multithreading in java
Monika Mishra
 
MULTI THREADING IN JAVA
VINOTH R
 
Java threading
Chinh Ngo Nguyen
 
Multithreading
sagsharma
 
Multithreading in java
JanmejayaPadhiary2
 
Slide 7 Thread-1.pptx
ajmalhamidi1380
 
econtent thread in java.pptx
ramyan49
 
Java Threads: Lightweight Processes
Isuru Perera
 
Object-Oriented-Prog_MultiThreading.pptx
NasreenTaj20
 
84694646456445645645645665656465464sdd.pptx
mhp821023
 
Multithreadingppt.pptx
HKShab
 
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
universitypresidency
 
Multithreading
SanthiNivas
 
multithreading to be used in java with good programs.pptx
PriyadharshiniG41
 
Session 7_MULTITHREADING in java example.ppt
TabassumMaktum
 
Java multi threading
Raja Sekhar
 
Lec7!JavaThreads.ppt
HemantSharma134028
 
Lec7!JavaThreads.ppt java multithreading
kavitamittal18
 
Multithreading in java
Kavitha713564
 

Recently uploaded (20)

PPTX
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PPTX
Innowell Capability B0425 - Commercial Buildings.pptx
regobertroza
 
PPTX
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PDF
6th International Conference on Machine Learning Techniques and Data Science ...
ijistjournal
 
PPTX
ISO/IEC JTC 1/WG 9 (MAR) Convenor Report
Kurata Takeshi
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
PPTX
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Innowell Capability B0425 - Commercial Buildings.pptx
regobertroza
 
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
6th International Conference on Machine Learning Techniques and Data Science ...
ijistjournal
 
ISO/IEC JTC 1/WG 9 (MAR) Convenor Report
Kurata Takeshi
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
Design Thinking basics for Engineers.pdf
CMR University
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Ad

java threads for easy learn and develop

  • 1. Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1
  • 2. Quick Review  What are the two approaches to creating Java threads? Advanced Topics in Software Engineering 2
  • 3. Advanced Topics in Software Engineering 3 Java Threads  Thread Creation  Thread Synchronization  Thread States And Scheduling  Short Demo
  • 4. Advanced Topics in Software Engineering 4 Thread Creation There are two ways to create a thread in Java:  Extend the Thread class  Implement the Runnable interface
  • 5. Advanced Topics in Software Engineering 5 The Thread class class A extends Thread { public A (String name) { super (name); } public void run () { System.out.println(“My name is ” + getName()); } } class B { public static void main (String [] args) { A a = new A (“mud”); a.start (); } }
  • 6. Advanced Topics in Software Engineering 6 The Runnable interface class A extends ... implements Runnable { public void run () { System.out.println(“My name is ” + getName () ); } } class B { public static void main (String [] args) { A a = new A (); Thread t = new Thread (a, “mud, too”); t.start (); } }
  • 7. Advanced Topics in Software Engineering 7 Java Threads  Thread Creation  Thread Synchronization  Thread States And Scheduling  Short Demo
  • 8. Advanced Topics in Software Engineering 8 Lock Each Java object is implicitly associated with a lock. To invoke a synchronized method of an object, a thread must obtain the lock associated with this object. The lock is not released until the execution of the method completes. The locking mechanism ensures that at any given time, at most one thread can execute any synchronized method of an object. Important: Lock is per object (NOT per method)!
  • 9. Advanced Topics in Software Engineering 9 wait, notify and notifyAll The execution of wait on an object causes the current thread to wait until some other thread to call notify or notifyAll. A thread must own the object lock before it invokes wait on an object. The execution of wait will also release the lock. When a waiting thread is notified, it has to compete and reacquire the object lock before it continues execution. What’s the difference between notify and notifyAll?
  • 10. Advanced Topics in Software Engineering 10 join A thread t1 can wait until another thread t2 to terminate. t1 t2 t2.join () end
  • 11. Advanced Topics in Software Engineering 11 interrupt interrupt allows one thread to send a signal to another thread. It will set the thread’s interrupt status flag, and will throw a ThreadInterrupted exception if necessary . The receiver thread can check the status flag or catch the exception, and then take appropriate actions.
  • 12. Advanced Topics in Software Engineering 12 Other Thread methods Method sleep puts the running thread into sleep, releasing the CPU. Method suspend suspends the execution of a thread, which can be resumed later by another thread using method resume. Method stop ends the execution of a thread. Note that suspend, resume, and stop has been deprecated in Java 2. (For more info, refer to http://java.sun.com/j2se/1.4.1/docs/guide/misc/threadPrimitiveDeprecation.html.)
  • 13. Advanced Topics in Software Engineering 13 Daemon Thread A daemon thread is used to perform some services (e.g. cleanup) for other threads. Any thread can be marked as a daemon thread using setDaemon (true). A program terminates when all its non-daemon threads terminate, meaning that daemon threads die when all non-daemon threads die.
  • 14. Advanced Topics in Software Engineering 14 Java Threads  Thread Creation  Thread Synchronization  Thread States And Scheduling  Short Demo
  • 15. Advanced Topics in Software Engineering 15 State Transition Diagram suspend blocked new runnable running suspended blocked dead start scheduled CS, yield IO, sleep wait, join s t o p suspend stop, run ends s u s p e n d r e s u m e stop IO completes, sleep expires, notify, notifyAll, join completes IO com pletes stop s u s p e n d stop resume
  • 16. Advanced Topics in Software Engineering 16 Scheduling In general, there are two types of scheduling: non- preemptive scheduling, and preemptive scheduling. In non-preemptive scheduling, a thread runs until it terminates, stops, blocks, suspends, or yields. In preemptive scheduling, even if the current thread is still running, a context switch will occur when its time slice is used up.
  • 17. Advanced Topics in Software Engineering 17 Priorities Each thread has a priority that also affects their scheduling to run. If a thread of a higher priority enters the runnable set, the currently running thread will be preempted by the new thread.
  • 18. Advanced Topics in Software Engineering 18 Java Threads  Thread Creation  Thread Synchronization  Thread States And Scheduling  Short Demo