threads. java threads a thread is not an object a thread is a flow of control a thread is a series...
TRANSCRIPT
![Page 1: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/1.jpg)
Threads
![Page 2: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/2.jpg)
Java Threads
• A thread is not an object
• A thread is a flow of control
• A thread is a series of executed statements
• A thread is a nested sequence of method calls
![Page 3: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/3.jpg)
The Thread Object
• A thread is not an object
• A Thread is an object
void start()– Creates a new thread and makes it runnable
void run()– The new thread begins its life inside this method
![Page 4: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/4.jpg)
Thread Creation Diagram
Thread t = new BThread();
t.start();
doMoreStuff();
Object A
BThread() {}
void start() {// create thread
}
void run() {doSomething();
}
Object BThread (extends Thread)
![Page 5: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/5.jpg)
Thread Creation Diagram
Thread t = new BThread();
t.start();
doMoreStuff();
Object A
BThread() {}
void start() {// create thread
}
void run() {doSomething();
}
Object BThread (extends Thread)
![Page 6: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/6.jpg)
Thread Creation Diagram
Thread t = new BThread();
t.start();
doMoreStuff();
Object A
BThread() {}
void start() {// create thread
}
void run() {doSomething();
}
Object BThread (extends Thread)
![Page 7: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/7.jpg)
Thread Creation Diagram
Thread t = new BThread();
t.start();
doMoreStuff();
Object A
BThread() {}
void start() {// create thread
}
void run() {doSomething();
}
Object BThread (extends Thread)
![Page 8: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/8.jpg)
Thread Creation Diagram
Thread t = new BThread();
t.start();
doMoreStuff();
Object A
BThread() {}
void start() {// create thread
}
void run() {doSomething();
}
Object BThread (extends Thread)
![Page 9: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/9.jpg)
Thread Creation Diagram
Thread t = new BThread();
t.start();
doMoreStuff();
Object A
BThread() {}
void start() {// create thread
}
void run() {doSomething();
}
Object BThread (extends Thread)
![Page 10: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/10.jpg)
Thread Creation Diagram
Thread t = new BThread();
t.start();
doMoreStuff();
Object A
BThread() {}
void start() {// create thread
}
void run() {doSomething();
}
Object BThread (extends Thread)
![Page 11: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/11.jpg)
Thread Creation Diagram
Thread t = new BThread();
t.start();
doMoreStuff();
Object A
BThread() {}
void start() {// create thread
}
void run() {doSomething();
}
Object BThread (extends Thread)
![Page 12: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/12.jpg)
Thread Creation Diagram
Thread t = new BThread();
t.start();
doMoreStuff();
Object A
BThread() {}
void start() {// create thread
}
void run() {doSomething();
}
Object BThread (extends Thread)
![Page 13: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/13.jpg)
Runnable Interface
• A helper to the thread object
• The Thread object’s run() method calls the Runnable object’s run() method
• Allows threads to run inside any object, regardless of inheritance
![Page 14: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/14.jpg)
Runnable Example
Talker talker = new Talker();
Thread t = new Thread(talker);
t.Start();
---
class Talker implements Runnable {
public void run() {
while (true) {
System.out.println(“yakitty yak”);
}
}
}
![Page 15: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/15.jpg)
Thread Lifecycle
Born
BlockedRunnable
Dead
stop()
start()
stop()
Active
block on I/O
I/O available
JVM
sleep(500)
wake up
suspend()
resume()
wait
notify
![Page 16: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/16.jpg)
Blocking Threads
• When reading from a stream, if input is not available, the thread will block
• Thread is suspended (“blocked”) until I/O is available
• Allows other threads to automatically activate• When I/O available, thread wakes back up again
– Becomes “runnable”
– Not to be confused with the Runnable interface
![Page 17: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/17.jpg)
Thread Scheduling
• In general, the runnable thread with the highest priority is active (running)
• Java is priority-preemptive– If a high-priority thread wakes up, and a low-priority
thread is running– Then the high-priority thread gets to run
immediately
• Allows on-demand processing– Efficient use of CPU
![Page 18: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/18.jpg)
Thread Starvation
• If a high priority thread never blocks
• Then all other threads will starve
• Must be clever about thread priority
![Page 19: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/19.jpg)
Thread Priorities: General Strategies
• Threads that have more to do should get lower priority
• Counterintuitive
• Cut to head of line for short tasks
• Give your I/O-bound threads high priority– Wake up, immediately process data, go back to
waiting for I/O
![Page 20: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/20.jpg)
Race Conditions
• Two threads are simultaneously modifying a single object
• Both threads “race” to store their value
• In the end, the last one there “wins the race”
• (Actually, both lose)
![Page 21: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/21.jpg)
Race Condition Example
class Account {
int balance;
public void deposit(int val)
{
int newBal;
newBal = balance + val;
balance = newBal;
}
}
![Page 22: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/22.jpg)
Thread Synchronization
• Language keyword: synchronized
• Takes out a monitor lock on an object– Exclusive lock for that thread
• If lock is currently unavailable, thread will block
![Page 23: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/23.jpg)
Thread Synchronization
• Protects access to code, not to data– Make data members private
– Synchronize accessor methods
• Puts a “force field” around the locked object so no other threads can enter
• Actually, it only blocks access to other synchronizing threads
![Page 24: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/24.jpg)
Thread Deadlock
• If two threads are competing for more than one lock
• Example: bank transfer between two accounts– Thread A has a lock on account 1 and wants to
lock account 2
– Thread B has a lock on account 2 and wants to lock account 1
![Page 25: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/25.jpg)
Avoiding Deadlock
• No universal solution
• Ordered lock acquisition
• Encapsulation (“forcing directionality”)
• Spawn new threads
• Check and back off
• Timeout
• Minimize or remove synchronization
![Page 26: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/26.jpg)
Wait and Notify
• Allows two threads to cooperate
• Based on a single shared lock object
![Page 27: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/27.jpg)
Wait and Notify: Code
• Consumer:
synchronized (lock) {
while (!resourceAvailable()) {
lock.wait();
}
consumeResource();
}
![Page 28: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/28.jpg)
Wait and Notify: Code
• Producer:
produceResource();
synchronized (lock) {
lock.notifyAll();
}
![Page 29: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/29.jpg)
Wait/Notify Sequence
Lock Object
ConsumerThread
ProducerThread
1. synchronized(lock){
2. lock.wait();
3. produceResource()4. synchronized(lock) {5. lock.notify();6.}
7. Reacquire lock8. Return from wait()
9. consumeResource();10. }
![Page 30: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/30.jpg)
Wait/Notify Sequence
Lock Object
ConsumerThread
ProducerThread
1. synchronized(lock){
2. lock.wait();
3. produceResource()4. synchronized(lock) {5. lock.notify();6.}
7. Reacquire lock8. Return from wait()
9. consumeResource();10. }
![Page 31: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/31.jpg)
Wait/Notify Sequence
Lock Object
ConsumerThread
ProducerThread
1. synchronized(lock){
2. lock.wait();
3. produceResource()4. synchronized(lock) {5. lock.notify();6.}
7. Reacquire lock8. Return from wait()
9. consumeResource();10. }
![Page 32: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/32.jpg)
Wait/Notify Sequence
Lock Object
ConsumerThread
ProducerThread
1. synchronized(lock){
2. lock.wait();
3. produceResource()4. synchronized(lock) {5. lock.notify();6.}
7. Reacquire lock8. Return from wait()
9. consumeResource();10. }
![Page 33: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/33.jpg)
Wait/Notify Sequence
Lock Object
ConsumerThread
ProducerThread
1. synchronized(lock){
2. lock.wait();
3. produceResource()4. synchronized(lock) {5. lock.notify();6.}
7. Reacquire lock8. Return from wait()
9. consumeResource();10. }
![Page 34: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/34.jpg)
Wait/Notify Sequence
Lock Object
ConsumerThread
ProducerThread
1. synchronized(lock){
2. lock.wait();
3. produceResource()4. synchronized(lock) {5. lock.notify();6.}
7. Reacquire lock8. Return from wait()
9. consumeResource();10. }
![Page 35: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/35.jpg)
Wait/Notify Sequence
Lock Object
ConsumerThread
ProducerThread
1. synchronized(lock){
2. lock.wait();
3. produceResource()4. synchronized(lock) {5. lock.notify();6.}
7. Reacquire lock8. Return from wait()
9. consumeResource();10. }
![Page 36: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/36.jpg)
Wait/Notify Sequence
Lock Object
ConsumerThread
ProducerThread
1. synchronized(lock){
2. lock.wait();
3. produceResource()4. synchronized(lock) {5. lock.notify();6.}
7. Reacquire lock8. Return from wait()
9. consumeResource();10. }
![Page 37: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/37.jpg)
Wait/Notify Sequence
Lock Object
ConsumerThread
ProducerThread
1. synchronized(lock){
2. lock.wait();
3. produceResource()4. synchronized(lock) {5. lock.notify();6.}
7. Reacquire lock8. Return from wait()
9. consumeResource();10. }
![Page 38: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/38.jpg)
Wait/Notify Sequence
Lock Object
ConsumerThread
ProducerThread
1. synchronized(lock){
2. lock.wait();
3. produceResource()4. synchronized(lock) {5. lock.notify();6.}
7. Reacquire lock8. Return from wait()
9. consumeResource();10. }
![Page 39: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/39.jpg)
Wait/Notify Sequence
Lock Object
ConsumerThread
ProducerThread
1. synchronized(lock){
2. lock.wait();
3. produceResource()4. synchronized(lock) {5. lock.notify();6.}
7. Reacquire lock8. Return from wait()
9. consumeResource();10. }
![Page 40: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/40.jpg)
Wait/Notify: Details
• Often the lock object is the resource itself
• Sometimes the lock object is the producer thread itself
![Page 41: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/41.jpg)
Wait/Notify: Details
• Must loop on wait(), in case another thread grabs the resource...– After you are notified– Before you acquire the lock and return from
wait()
• Use lock.notifyAll() if there may be more than one waiting thread
![Page 42: Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence](https://reader035.vdocument.in/reader035/viewer/2022081503/56649ea35503460f94ba8235/html5/thumbnails/42.jpg)
Wait/Notify Example: Blocking Queue
class BlockingQueue extends Queue {
public synchronized Object remove() {
while (isEmpty()) {
wait(); // really this.wait()
}
return super.remove();
}
public synchronized void add(Object o) {
super.add(o);
notifyAll(); // this.notifyAll()
}
}