1 / 28

Threads

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 with the first statement in the main method

nenet
Download Presentation

Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 with the first statement in the main method Although there are control structures and method invocations involved, you can trace the sequential flow of execution from one statement to the next.

  2. Threads • Java supports concurrent processes (multi-thread programs) • Single CPU handles this by alternating its resources between threads (managed by JVM and operating system) • The JVM is itself a multithreaded program (the garbage collector is a thread, which reclaims memory taken up by objects that are not being used)

  3. Steps to Writing a Thread Class • Implement a class that extends the Thread class • You will inherit a start() method and a run() method • Override the run() method and place the code for your task into the run() method of your class • A code in your run() method will be executed in a new Thread once an object of this type has been instantiated, and the start() method has been called. • Do NOT call the run method directly, this will not start a new thread……

  4. Example: public class NumberThread extends Thread { private int num; private int total; public NumberThread(int n, int ttl) { num = n; total = ttl; } //this code will run in it’s own thread //when ‘started’ public void run(){ for (int count = 0; count < total; count++) System.out.println(num); } }

  5. public class Numbers { public static void main (String[] args){ NumberThread numOne, numTwo, numThree; numOne = new NumberThread(11,10); numTwo = new NumberThread(22,10); numThree = new NumberThread(33,10); numOne.start(); numTwo.start(); numThree.start(); } }

  6. You can convert any class to a Thread by having that class implement the Runnable interface…and then using the Thread constructor: Thread (Runnable r) If our NumberThread class implemented Runnable, we would start our Thread in this manner: NumberThread num1 = new NumberThread(20,15); Thread th1 = new Thread (num1); th1.start();

  7. Stopping a Thread • To stop a thread, you must interrupt it. public static void main (String[] args){ NumberThread numOne, numTwo, numThree; numOne = new NumberThread(11,10); numTwo = new NumberThread(22,10); numThree = new NumberThread(33,10); numOne.start(); numTwo.start(); numThree.start(); ……. numTwo.interrupt(); }

  8. Thread code can check if it has been interrupted: public class NumberThread extends Thread { private int num; private int total; public NumberThread(int n, int ttl) { num = n; total = ttl; } public void run(){ int count = 0; while (count < total & !isInterrupted()) System.out.println(num); count++; } if (isInterrupted() ) //clean up }

  9. Thread Behavior The Thread.sleep() method can halt a running thread for a given number of milliseconds, allowing other threads to run • The Thread.yield() method halts a running thread and allows that thread to be preempted by another, if the CPU so chooses • The Thread.sleep() method throws an InterruptedException if the thread is interrupted while it is sleeping (InterruptedException is a checked exception) • Signatures for sleep method static void Thread.sleep(long milli) static void Thread.sleep(long milli, long nano)

  10. Sleep call must be used in a try block… public class NumberThread extends Thread{ ….. public void run(){ try{ for (int count = 0; count < total; count++) System.out.println(num); Thread.sleep((long) Math.random() *1000); } catch (InterruptedException e) { any code needed to clean up loose ends } }

  11. The thread scheduler runs each thread for a short amount of time called a time slice • Then the scheduler picks another thread from those that are queued to be ready, or runnable. A thread is runnable if it is not asleep or blocked in some way

  12. 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.

  13. An object of type DotPanel draws dots on a panel when it’s draw method is called. An object of type DotPanel clears the panel when it’s clear method is called. The Stimuli class uses the DotPanel class to draw dots on a panel. Run Stimuli.java with DotPanel.java. What happens when the Clear button is pressed while the dots are being drawn??

  14. 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

  15. Thread Synchronization • Threads are asynchronous .. That is, there is no guarantee about the order in which threads are executed, or when they might get pre-empted. • In many cases it is essential to be able to control the thread execution to some extent -- ie.) synchronize them.

  16. 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 generating a range of numbers waiting to be served, while the clerk uses these numbers.

  17. 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.

  18. This class must keep track of the next number given AND which customer is to be served 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. While the lock is in place, no other synchronized method can run in that object.

  19. 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) {} } }

  20. 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) { } } } }

  21. 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(); } } }

  22. Let’s run this…………………. What happens??? The clerk is serving non-existent customers!!

  23. 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..

  24. 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…………

  25. 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..

  26. Let’s put the I/O functionality in the machine, and have it report what it does !! 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; }

  27. 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 until there 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

  28. 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; }

More Related