Top Banner
Threads Threads A thread is single sequence of A thread is single sequence of executable statements within a program executable statements within a program eg.) a typical application is a thread, eg.) a typical application is a thread, with with the flow of control beginning with the the flow of control beginning with the first statement in the main method first statement in the main method Although there are control structures Although there are control structures and method invocations involved, you can and method invocations involved, you can trace the sequential flow of execution trace the sequential flow of execution from one statement to the next. from one statement to the next.
28

Threads A thread is single sequence of executable statements within a program eg.) a typical application is a thread, with the flow of control beginning.

Dec 17, 2015

Download

Documents

Julius Wheeler
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

ThreadsThreads

A thread is single sequence of executable A thread is single sequence of executable statements within a programstatements within a program

eg.) a typical application is a thread, witheg.) a typical application is a thread, with

the flow of control beginning with the first the flow of control beginning with the first statement in the main methodstatement in the main method

Although there are control structures and Although there are control structures and method invocations involved, you can trace method invocations involved, you can trace the sequential flow of execution from one the sequential flow of execution from one statement to the next.statement to the next.

Page 2: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

ThreadsThreads

Java supports Java supports concurrent concurrent processes (multi-thread processes (multi-thread programs)programs)

Single CPU handles this by alternating its resources Single CPU handles this by alternating its resources between threads (managed by JVM and operating between threads (managed by JVM and operating system)system)

The JVM is itself a multithreaded program (the The JVM is itself a multithreaded program (the garbage collector is a thread, which reclaims garbage collector is a thread, which reclaims memory taken up by objects that are not being used)memory taken up by objects that are not being used)

Page 3: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

Steps to Writing a Thread ClassSteps to Writing a Thread Class

Implement a class that Implement a class that extendsextends the the ThreadThread class class

You will inherit a start() method and a run() methodYou will inherit a start() method and a run() method

Override the run() method and place the code for your Override the run() method and place the code for your task into the run() method of task into the run() method of youryour class class

A code in your run() method will be executed in a new A code in your run() method will be executed in a new Thread once an object of this type has been Thread once an object of this type has been instantiated, and the start() method has been called.instantiated, and the start() method has been called.

Do NOT call the run method directly, this will not start Do NOT call the run method directly, this will not start a new thread……a new thread……

Page 4: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

Example:Example:

public class NumberThread extends Thread {public class NumberThread extends Thread {

private int num;private int num;

private int total;private int total;

public NumberThread(int n, int ttl) {public NumberThread(int n, int ttl) {

num = n;num = n;

total = ttl;total = ttl;

} }

//this code will run in it’s own thread //this code will run in it’s own thread

//when ‘started’//when ‘started’

public void run(){ public void run(){

for (int count = 0; count < total; count++)for (int count = 0; count < total; count++)

System.out.println(num);System.out.println(num);

} }

}}

Page 5: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

public class Numbers {public class Numbers {

public static void main (String[] args){public static void main (String[] args){

NumberThread numOne, numTwo, numThree;NumberThread numOne, numTwo, numThree;

numOne = new NumberThread(11,10);numOne = new NumberThread(11,10);

numTwo = new NumberThread(22,10);numTwo = new NumberThread(22,10);

numThree = new NumberThread(33,10);numThree = new NumberThread(33,10);

numOne.start();numOne.start();

numTwo.start();numTwo.start();

numThree.start();numThree.start();

}}

}}

Page 6: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

You can convert any class to a Thread by having that class You can convert any class to a Thread by having that class implement the Runnable interface…and then using the implement the Runnable interface…and then using the Thread constructor: Thread constructor: Thread (Runnable r)Thread (Runnable r)

If our NumberThread class implemented If our NumberThread class implemented Runnable,Runnable,

we would start our Thread in this manner:we would start our Thread in this manner:

NumberThread num1 = new NumberThread(20,15);NumberThread num1 = new NumberThread(20,15);

Thread th1Thread th1 = new Thread (num1); = new Thread (num1);

th1.start();th1.start();

Page 7: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

Stopping a ThreadStopping a Thread• To stop a thread, you must To stop a thread, you must interrupt interrupt it. it.

public static void main (String[] args){public static void main (String[] args){

NumberThread numOne, numTwo, numThree;NumberThread numOne, numTwo, numThree;

numOne = new NumberThread(11,10);numOne = new NumberThread(11,10);

numTwo = new NumberThread(22,10);numTwo = new NumberThread(22,10);

numThree = new NumberThread(33,10);numThree = new NumberThread(33,10);

numOne.start();numOne.start();

numTwo.start();numTwo.start();

numThree.start();numThree.start();

…… ……..

numTwo.interrupt();numTwo.interrupt();

}}

Page 8: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

Thread code can check if it has been interrupted:Thread code can check if it has been interrupted:

public class NumberThread extends Thread {public class NumberThread extends Thread { private int num;private int num; private int total;private int total; public NumberThread(int n, int ttl) {public NumberThread(int n, int ttl) { num = n;num = n; total = ttl;total = ttl; } }

public void run(){ public void run(){ int count = 0;int count = 0;

while (count < total & !isInterrupted())while (count < total & !isInterrupted()) System.out.println(num);System.out.println(num); count++;count++;

} } if (isInterrupted() )if (isInterrupted() )

//clean up//clean up }}

Page 9: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

Thread BehaviorThread Behavior The The Thread.sleep()Thread.sleep() method can halt a running thread for a method can halt a running thread for a

given number of milliseconds, allowing other threads to run given number of milliseconds, allowing other threads to run

• The The Thread.yield()Thread.yield() method halts a running thread and allows method halts a running thread and allows that thread to be that thread to be preempted preempted by another, if the CPU so chooses by another, if the CPU so chooses

• The The Thread.sleep()Thread.sleep() method throws an InterruptedException if method throws an InterruptedException if the thread is interrupted while it is sleeping the thread is interrupted while it is sleeping (InterruptedException is a checked exception)(InterruptedException is a checked exception)

• Signatures for sleep methodSignatures for sleep method

static void Thread.sleep(static void Thread.sleep(long millilong milli))

static void Thread.sleep(static void Thread.sleep(long milli, long nanolong milli, long nano))

Page 10: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

Sleep call must be used in a try block…Sleep call must be used in a try block…

public class NumberThread extends Thread{public class NumberThread extends Thread{

… ….. ..

public void run(){ public void run(){

try{ try{

for (int count = 0; count < total; count++)for (int count = 0; count < total; count++)

System.out.println(num);System.out.println(num);

Thread.sleep((long) Math.random() *1000);Thread.sleep((long) Math.random() *1000);

}}

catch (InterruptedException e) {catch (InterruptedException e) {

any code needed to clean up loose endsany code needed to clean up loose ends

}}

}}

Page 11: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

• The The thread schedulerthread scheduler runs each thread for a short amount of runs each thread for a short amount of time called a time called a time slicetime slice

• Then the scheduler picks another thread from those that are Then the scheduler picks another thread from those that are

queued to be ready, or queued to be ready, or runnable. runnable. A thread is A thread is runnablerunnable if it is not if it is not asleep or blocked in some wayasleep or blocked in some way

Page 12: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

Let’s look at a larger application of threads..Let’s look at a larger application of threads..

Suppose a psychologist want to be able to conduct an experiment to measure user response to a visual cue.

An application is needed which can draw black dots a random positions on the screen, and, after a random time interval, begins drawing red dots. The test begins when the patient clicks on a Draw button. As soon as the red dots appear, the patient is to click on a Clear button, which clears the drawing area.

The program must provide how many red dots have appeared before the user clicked in order to measure the patient reaction time.

Page 13: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

An object of type DotPanel draws dots on a panel An object of type DotPanel draws dots on a panel when it’s draw method is called.when it’s draw method is called.

An object of type DotPanel clears the panel when it’s An object of type DotPanel clears the panel when it’s clear method is called.clear method is called.

The Stimuli class uses the DotPanel class to draw dots The Stimuli class uses the DotPanel class to draw dots on a panel.on a panel.

Run Stimuli.java with DotPanel.java.Run Stimuli.java with DotPanel.java.

What happens when the Clear button is What happens when the Clear button is pressed while the dots are being drawn??pressed while the dots are being drawn??

Page 14: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

That’s right. Nothing happens.

That is because the CPU is busy executing that for loop, and there is no chance for it to see a user action.

We can fix this by:

1.) executing the drawing in a separate thread

2.) allowing that new thread to sleep occasionally, so the CPU will recognize user intervention

Take a look at DotPanel2.java and Simuli2.java

Page 15: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

Thread SynchronizationThread Synchronization

• Threads are Threads are asynchronousasynchronous .. That is, there is no .. That is, there is no

guarantee about the order in which threads are guarantee about the order in which threads are executed, or when they might get executed, or when they might get pre-emptedpre-empted..

• In many cases it is essential to be able to control the In many cases it is essential to be able to control the thread execution to some extent -- ie.) thread execution to some extent -- ie.) synchronize synchronize them.them.

Page 16: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

Let’s code a small simulation of a bakery, which uses a Take-A-Number machine to manage a waiting line. Customers will take a number when they arrive, and the clerk announces who’s next by looking at the device. All this is happening at the same time.

Many cooperative applications are based on the producer/consumer model where two threads cooperate at producing and consuming a particular resource or piece of data. The producer thread creates some result or message, and the consumer result uses that result or message. In our simulation, the customer is the producer, and the clerk is the consumer. The work cooperatively -- the customer generatinga range of numbers waiting to be served, while the clerk uses these numbers.

Page 17: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

We will need 4 classes: the Bakery class (our application) the TakeANumber class (an object of this type is shared by customers and the clerk) the Customer class the Clerk class

As mentioned before, the the customers and clerk work simultaneously, and so these objects should each run in their own thread.

Page 18: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

This class must keep track of the next number given AND which customer is to beserved next. It is essential that only one customer be able to take a number at a time.

As many Customer objects will have access to this object, how do we insure that only one take a number at a time??

class TakeANumber { private int next = 0; private int serving = 0;

public int getNum() { next = next + 1; return next; }

public int nextCustomer () { ++serving; return serving; }

synchronized

When a synchronized method is called, a lock is placed on that object. Whilethe lock is in place, no other synchronized method can run in that object.

Page 19: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

Customer thread should model the taking a number from the TakeANumber gadget. (To keep this program simple we will just assume that after taking a number the customer prints it out.) public class Customer extends Thread{

private int id; //to distinguish one customer from another private static int number = 0; //initial customer id number private TakeANumber takenum; //store reference to machine

public Customer (TakeANumber takeRef){ id = number; number = number + 1; takenum = takeRef; }

public void run(){ try{ sleep( (int)(Math.random() * 1000)) ; //customer walks to machine System.out.println((“Customer “+ id + “takes ticket “+takenum.getNum()); } catch ( InterruptedException e) {} }}

Page 20: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

The Clerk thread must simulate the behavior of serving the next customer. This thread will repeatedly get the nextCustomer and serve that customer. ( for simplicity we will print a message of who is being served)

public class Clerk extends Thread { private TakeANumber takeref;

public Clerk (TakeANumber gadget) { takeref = gadget; }

public void run() { while(true && !isInterrupted() ){ try{ sleep ( (int) (Math.random() * 50)); System.out.println(“Clerk serving” + takeref.nextCustomer()); } catch(InterruptedException e) { } } } }

Page 21: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

public class Bakery { public static void main (String [] args) { System.out.println(“Starting simulation”); TakeANumber machine = new TakeANumber(); Clerk clerk = new Clerk(machine); clerk.start(); for (int i = 1; i < = 5; i++){ Customer cust = new Customer(machine); cust.start(); } }}

Page 22: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

Let’s run this………………….

What happens???

The clerk is serving non-existent customers!!

Page 23: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

How can we fix this??

The clerk is needs to check IF there are customers waiting before getting the next customer. The clerk thread is getting a lot more attention than the customer.

Let’s add a customerWaiting() method to the machine, which can correspond to the clerk looking to see if there are other customers..

Page 24: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

public class Clerk extends Thread { private TakeANumber takeref;

public Clerk (TakeANumber gadget) { takeref = gadget; }

public void run() { while(true){ try{ sleep ( (int) (Math.random() * 50)); if (takeref.customerWaiting() ) System.out.println(“Clerk serving” + takeref.nextCustomer()); } catch(InterruptedException e) { break; } } } }

Let’s see if this helps…………

Page 25: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

What happened??????????

Maybe it worked fine. MAYBE it didn’t. It is is possible that a thread got pre-empted while executing: System.out.println( “Customer “ + id + “takes ticket “ + takeref.getNum() ):

in which case a customer being served is ‘bumped’ by another. We have forgotten that we are writing asynchronous code.

This is a CRITICAL SECTION of our application, and should not be interrupted when executing.

We can use synchronized methods to help..

Page 26: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

class TakeANumber { private int next = 0; private int serving = 0;

public synchronized int getNum(int id) { next = next + 1; System.out.println(“Customer “ + id + “ takes ticket “ + next); return next; }

public synchronized int nextCustomer () { ++serving; System.out.println(“Clerk serving “ + serving); return serving; }

public boolean customerWaiting() { return next > serving; }

Let’s put the I/O functionality in the machine, and have itreport what it does !!

Page 27: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

One last modification…………..

The Clerk run method uses a busy waiting algorithm, which is a terrible waste of CPU time!!

It would be better if the Clerk thread waited untilthere was a customer, without using CPU time !!

Thread class methods will help:

wait() - puts a thread into waiting state

notify() - takes a thread out of waiting and places it back into the waiting queue

Page 28: Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.

If the clerk calls nextCustomer(), and there is NO customer waiting, then the clerk can wait.

public class TakeANumber { private int next = 0; private int serving = 0;

public synchronized int nextCustomer () { try{ while( next <=serving) wait(); //thread deactivated, object lock released } catch (InterruptedException e){} finally{ ++serving; System.out.println(“Clerk serving “ + serving); return serving; } } //How will the Clerk thread by notified??

public synchronized int getNum(int id) { next = next + 1; System.out.println(“# “+ id +“ takes ticket “+ next); notify(); //clerk waiting is activated return next; }