1 / 48

Threads

Threads. Download as Power Point file for saving or printing. Threads Overview. A thread is an execution. All programs have at least one thread of execution control. Java supports additional multiple, light-weight threads of execution.

Télécharger la présentation

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 Download as Power Point file for saving or printing. Threads

  2. Threads Overview • A thread is an execution. • All programs have at least one thread of execution control. • Java supports additional multiple, light-weight threads of execution. • Running two separate programs at the same time is an example of a heavy-weight threads, each is a single program in execution, the internal environments of other heavy-weight threads are isolated from one another. • A heavy-weight program thread can have multiple light-weight threads, each being switched to execute for a time then suspended while another thread executes. • The prime distinction between heavy and light weight threads are that light threads share a portion of a common environment where heavy-weight threads are isolated. • When light-weight threads are switched to allow one to execute there is less individual thread information to store and switch since much is common to all. Threads

  3. Execute one heavy thread. Execute heavy and light thread. Thread Comparison public class one { public static void main(String args[]) { System.out.println(“heavy one"); } } public class one { public static void main(String args[]) { new two(); System.out.println("heavy one"); } } class two implements Runnable { public two() { new Thread(this).start(); } public void run() { System.out.println("light two"); } } Runnable two Threads

  4. Java thread support The key advantages of Java thread support are: • communication between threads is relatively simple • inexpensive in execution since light-weight, not necessary to save the entire program state when switching between threads • part of language, allows a program to handle multiple, concurrent operations in an operating system platform independent fashion. • Java on Windows 95/98/2000/XP and other preemptive operating systems ensure that all threads will get to execute occasionally by preempting thread execution (stopping the currently executing thread and running another) but this does not ensure that threads execute in any particular order. Threads

  5. public class one • { • public static void main(String args[]) • { • new two(); • System.out.println("heavy one"); • } • } • class two implements Runnable • { • public two() • { • new Thread(this).start(); • } • public void run() • { • System.out.println("light two"); • } • } Thread Execution • Line 5 creates new two object. • Line 14 creates new Thread object. • Line 14 calls start() which calls run()and returns immediately. • Line 19 run()completes execution but does not return. • Possible execution sequences: • 5, 14, 6, 18 • 5, 14, 18, 6 Runnable two run() Threads

  6. A Single Heavy-Weight Thread • We'll use a bouncing ball (number) to illustrate differences in multi-threaded execution. • Our thread example uses a single heavy weight thread to bounce a single ball off the sides of the display window. • The Ball class implements the ball bouncing, since a single thread is used, only one ball can be bounced at a time. The Ball class implements two public methods: • Ball() - constructs a Ball object. • move() - moves a Ball a predefined distance in the current direction and changes direction to a complementary angle if a wall is encountered. • Try it. Threads

  7. A Single Heavy-Weight Thread Only one thread is running. The key part of the program is: • public void run() { • for (int i = 1; i <= 1000; i++) { • move(); • try { Thread.sleep(10); } catch(Exception e) {} • } • } invoked once as a normal function call from onClick( ) function by clicking the Toss button. The button click: • executes the run() method • the ball is moved ( move is inherited from the Ball class), • the iteration in run()moves the ball • Thread.sleep(10); puts the main, heavy weight thread to sleep for at least 10 milliseconds; giving up the CPU allowing other threads to run. • With 1000 iterations (each sleeping 10 milliseconds), the ball moves for at least 10 seconds. Threads

  8. public class SingleThread extends UserInterface { • public SingleThread() { addButton("Toss"); } • public void onClick(char c) { • OneThread ot = new OneThread(this); • ot.run(); • } • } • class OneThread extends Ball { • public OneThread(UserInterface ui) { • super(ui); • } • public void run() { • for (int i = 1; i <= 1000; i++) { • move(); • try { Thread.sleep(10); } catch(Exception e) {} • } • } • } Exercise 1 • Exercise 1 • List in order line(s) executed when Toss button is clicked. • How many threads execute? Ball SingleThread OneThread

  9. Multiple Light-Weight Threads • With only minor changes to the previous example, multiple light-weight threads can support many simultaneously bouncing balls. Key changes are: • Implement the Runnable class for light weight threads. • Extend OneThread class to start a new thread for each ball constructed when Toss clicked. class MultiThread extends OneThread implements Runnable { public MultiThread(UserInterface ui) { super(ui); new Thread(this).start(); } } • Try it. Ball move() OneThread run() Runnable MultiThread Threads

  10. Ball move() Runnable interface OneThread run() Runnable MultiThread • MultiThread subclass of OneThread and Runnable: class MultiThread extends OneThread implements Runnable • implements Runnable contract to implement method definition: public void run() • Thread constructed by: new Thread(this) • run() invoked by start() • start(); starts a thread and returns immediately • new Thread(this).start(); constructs and starts a new thread. • Thread.sleep(10); causes a thread to give up the CPU. Threads

  11. public class Example extends UserInterface { • public Example() { • addButton("Toss"); • } • public void onClick(char c) { • MultiThread mt = new MultiThread(this); • } • } • class MultiThread extends OneThread implements Runnable{ • public MultiThread(UserInterface ui) { • super(ui); • new Thread(this).start(); • } • } Multiple Threads – Exercise 2 Example run run • Where is the run() method? • List the lines executed when Toss is clicked the first time. See next slide. • What happens to a thread at the end of the run( ) method? • What does new Thread(this).start( ) do, since you can press Toss repeatedly and another ball starts bouncing?

  12. public class Example extends UserInterface { • public Example() { • addButton("Toss"); • } • public void onClick(char c) { • MultiThread mt = new MultiThread(this); • } • } • class MultiThread extends OneThread implements Runnable{ • public MultiThread(UserInterface ui) { • super(ui); • new Thread(this).start(); • } • } • class OneThread extends Ball { • public OneThread(UserInterface ui) { • super(ui); • } • public void run() { • for (int i = 1; i <= 1000; i++) { • move(); • try { Thread.sleep(10); } catch(Exception e) {} • } • } Multiple Threads Ball move() OneThread run() Runnable MultiThread Threads

  13. 4 Executions >java ST 2 1 3 4 >java ST 2 1 3 4 > java ST 1 3 2 4 > java ST 1 2 4 3 Execution Non-deterministic • class ST { • public static void main(String args[]) { • new Simple(1); • new Simple(2); • new Simple(3); • new Simple(4); } • } • class Simple implements Runnable { • int n; • Simple(int n) { • this.n = n; • new Thread(this).start(); • } • public void run() { • try {Thread.sleep(10); } • catch(Exception e) {}; • System.out.println( n ); • } • }

  14. Array of Threads A thread is an object that can be stored in any data structure. class AT { public static void main(String args[]) { Simple sa[] = new Simple[10]; for (inti=0; i<10; i++) sa[ i ] = new Simple( i ); } } class Simple implements Runnable { int n; Simple(int n) { this.n = n; new Thread(this).start(); } public void run() { try { Thread.sleep(10); } catch(Exception e) {}; System.out.print ( n ); } } 3 Executions >java AT 0123456897 > java AT 0132547698 >java AT 1204365789

  15. Array of Greedy Threads • Threads naturally run asynchronously to one another with no cooperation. • Only one thread executes at a time on the CPU • Race conditions can develop as multiple threads attempt to occupy the CPU exclusively • Example of many threads racing to complete • Only significant changes are: • 10 threads started without artificial delay. • No artificial delay introduced through the Thread.sleep(10); • Try it. Threads

  16. public class ArrayOfGreedyThreads extends UserInterface { • public ArrayOfGreedyThreads() { addButton("Toss"); } • public void onClick(char c) { • GreedyThread gt[] = new GreedyThread[10]; • for(int i=0;i<10;i++) • gt[i] = new GreedyThread(this); • } • } • class GreedyThread extends Greedy • implements Runnable { • public GreedyThread(UserInterface ui) { • super(ui); • new Thread(this).start(); • } • } • class Greedy extends Ball { • public Greedy (UserInterface ui) { • super(ui); } • public void run() { • for (int i = 1; i <= 1000; i++) { move(); } • } • } Greedy Threads – Exercise 3 Exercise 3 What sequence of line(s) are executed when Toss is clicked (note for statements in onClick() and run() methods)?

  17. Threads compete for the CPU. Below, no thread is able to complete before another obtains the CPU. Thread Race Sequential Execution A 1A 2A 3A 4A 5B 1 B 2 B 3 B 4 B 5 main 1 main 2 main 3 main 4 main 5 Threaded Execution main 1 B 1 A 1 main 2 B 2 A 2 B 3 A 3 main 3 B 4 A 4 B 5 main 4 A 5 main 5 class Race { public static void main(String args[]) { new Simple("A"); new Simple("B"); for (int i=1; i<=5; i++) System.out.println("main "+ i); } } class Simple implements Runnable { String name; Simple(String name) { this.name = name; new Thread(this).start(); } public void run() { for (int i=1; i<=5; i++) { System.out.println( name+" "+i ); try {Thread.sleep(10); } catch(Exception e) {};} } }

  18. Thread Race Conditions • Threads compete for execution time • Scheduled for execution non-deterministically (i.e. order of execution is not predetermined). • Unless thread execution completes, the thread is suspended and another thread given the CPU • Threads can execute independently • Race conditions occur when two or more threads interfere with other thread’s results. • Example illustrates how thread execution is arbitrary suspended by the run time system. • Executing the example produces differences in printed results. • Try it. Threads

  19. Race Output • There are 3 threads • ABrace in onClick() • A • B • Note the far right column • The threads a racing each other to execute Threads

  20. public class ABrace extends UserInterface { public ABrace() { addButton("Start"); addButton("Clear"); } public void init() { Display.initialize(this); } public void onClick(char c) { Race A, B; switch(c) { case 'S' : Display.heading(30); A = new Race("A"); B = new Race("B"); for (int i=1; i<=5; i++) Display.println("onClick "+i); break; case 'C' : Display.clear(); } } } class Race implements Runnable { String name; public Race( String name ) { this.name = name; new Thread(this).start(); } public void run() { for (int i=1; i<=5; i++) Display.println(name +"="+i); } } Thread Race Conditions – Exercise 4 • Exercise 4 • Execute the program several times.  • How many threads are executing? Explain how you know. • Can the results of 5 executions be identical? Explain.

  21. Synchronized Threads • Thread execution can be: • independent of other threads (i.e. parallel or asynchronous execution) • dependent (i.e. serialized or synchronous execution) where one thread executes to the exclusion of the other threads • Thread execution is controlled in Java using the synchronized statement which limits execution to a single thread. • The synchronization mechanism used by Java is termed a monitor (versus a semaphore, task, or other mechanism) • A monitor allows only one thread to have access to an object at a time. • The keyword synchronized defines a statement or method where one thread at a time has exclusive access to the object. Threads

  22. Race >java RT [1[2[3[41] 3] 2 ] 4 ] >java RT [1[2[3[44] 3] 1] 2] >java RT [1[3[2[42] 1] 3] 4] • class RT { • public static void main(String args[]) { • new Racer(1); • new Racer(2); • new Racer(3); • new Racer(4); • } • } • class Racer implements Runnable { • int n; • Racer(int n) { • this.n = n; • new Thread(this).start(); • } • public void run() { • System.out.print( "[" + n ); • try {Thread.sleep(10); } • catch(Exception e) {}; • System.out.println( n + “]" ); • } • }

  23. Non-cooperating Synchronized Example >java ST C:\d [11] [22] [44] [33] >java ST [11] [33] [22] [44] • class ST { • public static void main(String args[]) { • new Racer(1); • new Racer(2); • new Racer(3); • new Racer(4); • } • } • class Racer implements Runnable { • static String common=“common”; • int n; • Racer(int n) { • this.n = n; • new Thread(this).start(); • } • public void run() { • synchronized( common ) { • System.out.print( "[" + n ); • try {Thread.sleep(10); } • catch(Exception e) {}; • System.out.println( n + “]" ); • } • } • }

  24. Example • In the example above the object common is accessible to the threads 1, 2, 3 and 4. • In the run method access to common is synchronized so that only one thread can execute the code block: class Racer implements Runnable { static String common=“common”; int n; public void run() { synchronized( common ) { } } System.out.print( "[" + n ); try {Thread.sleep(10); } catch(Exception e) {}; System.out.println( n + “]" );

  25. Example • In the example below the object common is accessible to the two threads A and B. • In the run method, access to common is synchronized so that only one thread can execute the code block: public void run() { synchronized(common) { for (int i=1; i<=5; i++) Display.println(name +"="+i); } } Threads

  26. Synchronized Results • Thread A or B will have exclusive access to synchronized area of run() • The output shows that once thread A or B enters the synchronized area, the other will not enter • The ABsynchRace thread in onClick() is not synchronized. • Try it. Threads

  27. public class ABsynchRace extends UserInterface { : : public void onClick(char c) { SynchRace A, B; String common = "common"; switch(c) { case 'S' : A = new SynchRace("A", common); B = new SynchRace("B", common); for (int i=1; i<=5; i++) Display.println("onClick "+i); break; case 'C' : Display.clear(); } } } Synchronized Threads – Exercise 5

  28. class SynchRace implements Runnable { String name; String common; public SynchRace(String name, String common ) { this.name = name; this.common = common; new Thread(this).start(); } public void run() { synchronized(common) { for (int i=1; i<=5; i++) Display.println(name +"="+i); } } } Synchronized Threads – Exercise 5 • Exercise 5 • How does execution differ from the unsynchronized version? • What effect does the synchronized construct produce? • How can the printing in onClick( ) be synchronized? • What would be an example of unexpected results?

  29. Non-Cooperating Threads • Example of three independent threads: • the main heavy-weight • Producer that puts onto a common queue • Consumer that gets from the common queue • Consumer thread may get from the queue before the Producer thread puts onto the queue • Any thread execution may be preempted at any time • No guarantee a put or get finished before another thread runs • Most obvious when the printed output is intermixed as in: • [put: 1 {get:0}] rather than: • [put: 1]{get: 0} • Try it. Threads

  30. Non-Cooperating Threads • put and get output intermixed, one not completed before other starts • Need synchronized access to queue object • Even worse, get executed whenqueue is empty • Need to force get execution to wait when queue is empty Threads

  31. Exercise 6 • The Producer and Consumer are both threads, each having a run( ) method. Starting in the run( ) of each, list the sequence of lines executed that would print:             [put: 1{get:0]} • Again, starting in the run( ) of each thread, list the sequence that would print: {get:-1 [put: 0]    } • public class ProducerConsumer extends UserInterface{ • public void onClick(char c) { • switch(c) { • case 'S' : Q q = new Q( ); • new Consumer(q); • new Producer(q); • break; • case 'C' : Display.clear(); • } • } • } • class Q { • int n = 0; • void put() { • n = n + 1; • Display.print("[put: " + n); • Display.println("]"); • } • void get() { • n = n - 1; • Display.print("{get: " + n); • Display.println("}"); • } • } • class Producer extends Thread { • Q q; • Producer( Q q ) { • this.q = q; • new Thread(this).start(); • } • public void run() { • for (int i=1; i<=10; i++) • q.put(); // Produce • } • } • class Consumer extends Thread { • Q q; • Consumer( Q q ) { • this.q = q; • new Thread(this).start(); • } • public void run() { • for (int i=1; i<=10; i++) • q.get(); // Consume • } • }

  32. public class ProducerConsumer extends UserInterface { • public void onClick(char c) { • switch(c) { • case 'S' : Q q = new Q( ); • new Consumer(q); • new Producer(q); • break; • case 'C' : Display.clear(); • } • } • } • class Q { • int n = 0; • void put() { • n = n + 1; • Display.print("[put: " + n); Display.println("]"); • } • void get() { • n = n - 1; • Display.print("{get: " + n); Display.println("}"); • } • } Unsynchronized Queue Producer q Q Consumer q

  33. class Producer extends Thread { • Q q; • Producer( Q q ) { • this.q = q; • new Thread(this).start(); • } • public void run() { • for (int i=1; i<=10; i++) q.put(); // Produce • } • } • class Consumer extends Thread { • Q q; • Consumer( Q q ) { • this.q = q; • new Thread(this).start(); • } • public void run() { • for (int i=1; i<=10; i++) q.get(); // Consume • } • } Producer Consumer Producer q Q Consumer q Threads

  34. Monitors - Synchronized Producer Consumer • Threads interact when simultaneously accessing common resource. • Java supports a control mechanism called a monitor that allows only one thread at a time to execute a synchronized method on an object. • By adding synchronized to methods, a thread entering such a method has exclusive access to that object in synchronized class methods. • When a synchronized method completes, other threads may enter. • For a queue object, the synchronized put and get method ensures either completes before another thread enters either, for an object. • With two queue objects, one thread could have access to one queue object while another thread accessed the other queue object. • This does not ensure that something has been put into the queue before a thread attempts to get it, that is another problem. • Beyond cosmetics, the only change is the addition of: synchronized void put( ); synchronized void get( ); • Try it Threads

  35. Monitor Behavior • Monitor controls access to a synchronized object. • Monitor allows only one thread to access object; all other threads are blocked. • When thread exits synchronized method other threads can access synchronized object. • Blocked threads are automatically allowed to attempt to access synchronized object again. • Threads can still access object through unsynchronized methods. • Methods are not synchronized, objects are. • No conflict when threads access different objects. Threads

  36. class Q { • int n = 0; • synchronized void put( ) { • n = n + 1; • Display.print("[put: " + n); • Display.println("]"); • } • synchronized void get( ) { • n = n - 1; • Display.print("{get: " + n); • Display.println("}"); • } • } • Producer p = new Producer(q); • : • q.put(); • Consumer c = new Consumer(q); • : • q.get(); Monitor Producer q Q Consumer q Entered Producer using q Q Consumer blocked for q Consumer blocked when common Q object in use by Producer

  37. Synchronized Queue • class Q { • int n = 0; • synchronized void put( ) { • n = n + 1; • Display.print("[put: " + n); • Display.println("]"); • } • synchronized void get( ) { • n = n - 1; • Display.print("{get: " + n); • Display.println("}"); • } • } • Exercise 7 • Only one thread executes any synchronized method on a single object. Assume Consumer thread is executing Line 9 when Producer thread executes Line 3; list the lines then executed in the above. • Is synchronizing the get method but not the put method sufficient to prevent intermixed output? • Are there still problems between the Consumer and Producer?

  38. wait, notify and notifyAll • waitsuspends thread until notifyor notifyAll executed by another thread • notifysends signal to one waiting thread • Which thread notified is non-deterministic • notifyAllsends signal to all waiting threads • Only one notified thread can execute any synchronized method on an object at a time • A notified thread continues from point where waitexecuted • Thread enters a dead state when run completes • The monitor controls access to a synchronized object • No conflict when threads access different objects Threads

  39. Monitor Behavior • Monitor controls access to synchronized object. • Difference between threads blocked because monitor busy with another thread and threads that explicitly called wait • When synchronized method completes, blocked threads automatically can re-attempt object access • Threads that called wait can only proceed by another thread calling notify or notifyAll • Try it Threads

  40. class Q { • int n = 0; • synchronized void put( ) { • n = n + 1; • Display.print("[put:"+ n); • Display.println("]"); • notify( ); • } • synchronized void get( ) { • while ( n==0 ) { • try { wait(); } • catch (Exception e) { } • } • n = n - 1; • Display.print("{get: " + n); • Display.println("}"); • notify( ); • } • } • Producer p = new Producer(q); … q.put(); • Consumer c = new Consumer(q); … q.get(); • Consumer enters get. • Consumer places itself on wait. • Producer enters put. • Producer sends notify(). • Consumer released from wait. Monitor Producer using q Q Consumer waits for q Consumer waits when n==0

  41. class Q { • int n = 0; • synchronized void put( ) { • n = n + 1; • Display.print("[put: " + n); • Display.println("]"); • notify( ); • } • synchronized void get( ) { • while ( n==0 ) { • try { wait(); } • catch (Exception e) { } • } • n = n - 1; • Display.print("{get: " + n); • Display.println("}"); • notify( ); • } • } Cooperating Producer Consumer • wait();suspends thread until notify();executed by another thread • notify( );sends signal to waiting thread • Continues from point where wait();executed. • Exercise 8 • while ( n==0 ) isneeded. Why? • What happens without line 7? • Without line 17? Threads

  42. class Q { • int n = 0; • synchronized void put( ) { • n = n + 1; • Display.print("[put: " + n); • Display.println("]"); • } • synchronized void get( ) { • while ( n==0 ); • n = n - 1; • Display.print("{get: " + n); • Display.println("}"); • } • } Cooperating Producer Consumer Busy Wait Exercise 8 3. Line 17 not needed. • Exercise 9 • This seems to prevent consuming before producing. How? • What is the problem? Threads

  43. Multiple inheritance: Thread class versus Runnable interface class MultiThread extends OneThread implements Runnable • At the start of our thread discussion we had a class OneThread that bounced a one ball around the screen. To bounce lots of balls we needed a class just like OneThread but that also supported threads. • We could rework OneThread to do threads but better solution is to inherit from OneThread to bounce Balls, and a thread class; multiple inheritance. • Java does not directly support multiple inheritance. • Java solution is interface. • Class MultiThread is a subclass of Thread by implementingRunnable. • Implementing Runnable requires that class MultiThreadmust define method: public void run() • Thread class implements Runnable. Threads

  44. Examples: Thread and Runnable public class example { public static void main(String args[]) { new Simple(); } } class Simple implements Runnable { public Simple() { new Thread(this).start(); } public void run() { System.out.println("Simple"); } } A simple example illustrates the syntactic differences between implementing Runnable versus inheriting Thread. Thread inheritance is simpler to use but prohibits other inheritance. Runnable allows inheritance (and implementing other interfaces) but is a little more complicated. Both require a method: public void run() public class example { public static void main(String args[]) { new Simple().start(); } } class Simple extends Thread { public void run() { System.out.println("Simple"); } } Threads

  45. Deadlock • Deadlock occurs when one thread cannot complete. • One example is deadly embrace (or circular wait) where two threads each hold a resource required by the other. • t1 and t2 threads require both resources r1 and r2 to complete. • t1 thread holds r1 resource; t2 thread holds r2 resource. • t1 and t2 both deadlocked. t1 r2 r1 t2 Threads

  46. Deadlock • t1,t2,t3, t4 – Execute synchronized method on common object. • t1 executes wait(); • t3, t4 blocked while t2 holds object. • notify() not executed. • t1 never completes. t3 t4 Blocked object t2 Wait list t1 Threads

  47. Deadlock Exercise 9 • waitObject wo = new waitObject(); • for(int i=0;i<10;i++) new ExampleThread( wo ); • class ExampleThread extends Thread • { waitObject wo; • ExampleThread( waitObject wo ) • { this.wo = wo; • new Thread(this).start(); • } • public void run() • { wo.exit(); • wo.enter(); • } • } • class waitObject • { synchronized void enter( ) • { System.out.println("enter"); • notify( ); • } • synchronized void exit( ) • { try { wait(); } catch(Exception e) {} • System.out.println("exit"); • } • } • This always deadlocks. Why? • Changing to the following still deadlocks. Why? • public void run() • { wo.enter(); • wo.exit(); • } • Would notify() at line 22 prevent deadlock? notifyAll()? Threads

  48. Multi-thread Summary • Thread creation - Extend Thread class or implement Runnable interface • Serialization – Monitor automatically limits execution of synchronized method on a shared object to one thread • Basic thread control • Wait – Places a thread on wait list for object. • Notify – Releases an arbitrary thread from wait list for object. • NotifyAll – Releases all threads from wait list for object. • Sleep – Suspends thread execution for a minimum specified time. Threads

More Related