threads doing several things at once. threads n what are threads? n two ways to obtain a new thread...
TRANSCRIPT
![Page 1: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/1.jpg)
Threads
Doing Several Things at Once
![Page 2: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/2.jpg)
Threads
What are Threads? Two Ways to Obtain a New Thread The Lifecycle of a Thread Four Kinds of Thread Programming
![Page 3: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/3.jpg)
What are Threads?
A Thread is a unit of program execution that runs independently from other threads.- lightweight process
Threads behave as if they were running on different CPUs
Extend the concept of time-sharing. Garbage collectors and listeners run in
separate threads
![Page 4: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/4.jpg)
Thread Context
Context switched when JVM switched to a different thread
Not as costly as switching processes Threads can make a program run faster Threads allow a program to do more
that one thing at a time- one processor?
• the system handles the sharing
![Page 5: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/5.jpg)
Two Ways to Obtain a New Thread
Extend the java.lang.Thread Class
Implement the “Runnable” interface- Applets extend the class Applet by
definition. an applet must always use the second
technique; extends is taken by (J)Applet
![Page 6: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/6.jpg)
Extension of java.lang.Thread
Create a class extending Thread Class Put the code for the task into the run
method of the new class Create an instance of that class Call the start method for that instance The start method will call the run method The thread executes until the run method
terminates.
![Page 7: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/7.jpg)
Extending the Thread Class
Class AThread extends Thread
{
…
public void run()
{
// your code here
}
}
![Page 8: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/8.jpg)
Running a Thread In another thread, create the thread
AThread t1 = new AThread(); Start the thread by calling its start method
t1.start(); The call to start creates and schedules the
thread to execute. The run method is called by the JVM when it is the thread’s turn to execute
See Java Documentation on Thread See:
- NameThread.java- ThreadTest.java
![Page 9: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/9.jpg)
The Runnable Interface
public interface Runnable
{ // Must write run()
public abstract void run();
}
![Page 10: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/10.jpg)
Implement Runnable Create a class ARunnable which implements the
“runnable” interface.public class ARunnable implements Runnable
{
public void run() { … }
}
Use that class in a call to the Thread constructorThread t = new Thread( new ARunnable() );
t.start(); // start the thread
See Java Doc on Runnable See NameUsingThread.java
![Page 11: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/11.jpg)
Notes on Runnable
Statements within the Runnable Interface implementation of run() can’t invoke Thread methods like “sleep()”, “getName()”, etc. because no “this” object is available in Runnable.- the object isn’t the thread
![Page 12: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/12.jpg)
Remarks
A call to currentThread() can appear anywhere in Java code. Any of the methods of that thread can be called via the thread returned in currentThread().
Runnable vs Thread: Use Runnable when only the “run()” method will be overridden. Classes should not be extended unless there is a fundamental enhancement of behavior.
![Page 13: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/13.jpg)
Terminating Threads
A thread terminates when its run method returns – the normal way
A thread can be interrupted (never stopped) by using the thread.interrupt() method.
A thread’s run method should occasionally check for the interrupt signal
![Page 14: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/14.jpg)
Interrupt Coding
public void run(){
try // DO NOT PUT try in a loop, but loop in try{ // do some work}catch (InterruptedException e){ // do whatever needs to be done}// clean up
}
![Page 15: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/15.jpg)
Parameters to Threads
The run method cannot have parameters
The constructor can have parameters The Thread constructor can have
certain parameters- see API for java.lang.Thread
![Page 16: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/16.jpg)
Thread Lifecycle
A thread is created, then “started” The thread “dies” when
- The run method terminates:• normally• through an exception or return
- A thrown exception will cause the thread to terminate but not the parent
![Page 17: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/17.jpg)
Priorities A thread runs at the priority of its parent unless its
priority is changed. A higher priority executes first if they are both
ready to run Java threads
- can be preempted.- may or may not be time-sliced
Computationally intensive threads should “yield” periodically
Raising a thread’s priority does not affect other (heavyweight) processes, only parent, sibling, children threads (competing lightweight processes)
![Page 18: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/18.jpg)
Four Kinds of Thread Programming
1. Unrelated Threads
2. Related Unsynchronized Threads
3. Mutually-Exclusive Threads
4. Communicating Mutually-Exclusive Threads
![Page 19: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/19.jpg)
Thread Diagram
A ClassA Class
An ObjectAn Object
An ObjectAn Object
An Object has MethodsAn Object has Methods
An Object has MethodsAn Object has Methods
Thread runs in a methodThread runs in a method
![Page 20: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/20.jpg)
Unrelated Threads
The simplest thread programs involves threads that do different things
Don’t interact with one another Coffee_Tea Example
- Change sleep time; run on PC and Suns to see difference
![Page 21: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/21.jpg)
Related, Unsynchronized Threads
The problem is partitioned into subproblems A thread solves each subproblem The threads don’t interact They don’t work on shared data
- Example: testPrime.java A server connection for each socket
connection is a good example A “Work-to-order” thread is called a daemon
- always on
![Page 22: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/22.jpg)
Mutally-Exclusive Threads
Threads that access common data concurrently need to be controlled so that the correct state of the data is preserved at all times. Such a situation occurs frequently in the real world.
![Page 23: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/23.jpg)
The Pressure Gauge
Consider reading and setting a pressure gauge. The gauge should not be set above 20 psi. But the pressure is set by pressure setting objects, independent of one another. Each must check the gauge and if it is safe to do so, increase the pressure by a fixed amount. - Example:
• BadPressure.java
![Page 24: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/24.jpg)
BadPressure output
>java BadPressure
Gauge reads 150, safe limit is 20
What happened?- code appeared to restrict gauge value, but didn’t
• Setting the gauge is a critical section– Mutual exclusion required
![Page 25: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/25.jpg)
Mutual Exclusion Simultaneous reading and setting of the
pressure gauge by different threads- set followed check; all did check, THEN did
change• check-change must be atomic
one object needs to lock others out until it is finished
can be done at the class level, method level, or on a block of code
![Page 26: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/26.jpg)
The keyword synchronized
This keyword obtains a mutex (mutual exclusion) lock for the executing thread on the named object. The code is then executed, and the lock is released. If another object already holds the lock, the thread is suspended until the lock is released. Lock-competing threads are queued.
![Page 27: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/27.jpg)
Some considerations
Java programmers don’t need to do the low level details of creating, acquiring, and releasing locks- Unix semaphores, locks
Specify the portion of code (critical section) for mutual exclusion, and the object that must be exclusively locked
The region of code must be as small as possible
![Page 28: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/28.jpg)
Mutual Exclusion over an Entire Class apply the keyword synchronized to a class method.
- e.g. static synchronized void RaisePressure()
Only one static synchronized method for a given class can be running at any given time in the JVM, regardless of how many objects there are of that class
the class object is used to synchronize the threads There is one lock for static synchronized methods, and a
different lock for synchronized methods. One thread could have the static lock while another thread has a method lock, while other threads could be running other unsynchronized methods- Example: SetPressure.java
![Page 29: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/29.jpg)
Class Mutual Exclusion
From SetPressure.java
// Only one thread may be executing in herestatic synchronized void raisePressure() { if(BadPressure.pressureGauge < BadPressure.SafetyLimit - 15) { ....
>java SetPressureGauge reads 15, safe limit is 20
![Page 30: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/30.jpg)
Mutual Exclusion over a Block
use the keyword synchronized before a block of code
Use a static object for the lock any available, convenient object will do cannot be a local object or instance
variable
![Page 31: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/31.jpg)
Block Mutual Exclusion
class pressure extends Thread{ static Object lock = new Object(); void raisePressure() { synchronized(lock); Mutex ‘til end { if(SetPressure.pressureGauge < SetPressure.SafetyLimit - 15) { ... } ...}
![Page 32: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/32.jpg)
Mutual Exclusion over a Method use synchronized keyword on the instance
method. guarantees that only one of the perhaps
many synchronized instance methods will be executing at any one time on a given instance of that class
equivalent to synchronized(this) over a block- Examples:
• raisePressure• Monitor in MessagePass.java
![Page 33: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/33.jpg)
Method Synchronization
synchronized void raisePressure() { if(p.pressureGauge < p.SafetyLimit - 15) { ...same as void raisePressure() { synchronized(this) // on this object { if(p.pressureGauge < p.SafetyLimit - 15) { ... } }
![Page 34: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/34.jpg)
Communicating Mutually Exclusive Threads
Threads that need to access common data, but also communicate with one another
the hardest kind of thread programming Producer/Consumer type of problem I produce, you consume. Don’t try to produce
until there is something to consume. Don’t produce too much, until something is consumed
Wait/Notify is the solution
![Page 35: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/35.jpg)
Wait / Notify
Wait says “I have the lock, but there is nothing to consume. I give up the lock and wait
Notify says “I just produced something, I will place it in common area, release the lock and wait”
![Page 36: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/36.jpg)
Producer Pseudo-code
// producer thread
enter synchronized code // get lock
while(buffer_full)
wait();
produce_items()
notify() // tell waiting consumers
leave synchronized code // release lock
![Page 37: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/37.jpg)
consumerPseudo-code
// consumer thread
enter synchronized code // get lock
while(no_items)
wait();
consume_items()
leave synchronized code
![Page 38: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/38.jpg)
Notes: Examples
- plum.java• inelegant dependant producer-consumer
- Operator Examples• MessagePass.java
– Operator with Monitor
• Operator.java– Operator w/o monitor (inelegant, too)
- Tally Examples• TallyWrong.java
• TallyRight.java– Note how inner classes compile
- PutGet.java • vary relative wait times and see what happens
- Deadlock.java• oh, oh; somewhat inelegant – works with zero buffer
![Page 39: Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread](https://reader035.vdocuments.us/reader035/viewer/2022062217/5697bf701a28abf838c7dc41/html5/thumbnails/39.jpg)
Examples, etc;
CommunicatingThreads.java- applet with piped communication
between two threads Exercise
- Try the PutGet example with multiple producers/consumers. Change the constructors so that you know who is producing what and who is consuming what.