1 / 19

Week 3, Day 1: Processes & Threads

Week 3, Day 1: Processes & Threads. Re-introduction to making your own thread Synchronizing threads Lab tomorrow: Quiz Lab 3 : Threading!. Threads wind their way through the code until they run out of instructions to execute. public class App{ public static void main (String[] args ) {

yakov
Download Presentation

Week 3, Day 1: Processes & 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. Week 3, Day 1:Processes & Threads • Re-introduction to making your own thread • Synchronizing threads Lab tomorrow: • Quiz • Lab 3: Threading! SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder

  2. Threads wind their way through the code until they run out of instructions to execute public class App{ public static void main(String[] args) { App me = new App(); me.method_A(); } private void method_A() { // more code here method_B(); return; } private void method_B() { return; } private void method_C() { // more code here } } SE-2811

  3. The main class may implement the Runnable interface itself: public class App implements Runnable{ public static void main(String[] args) { App me = new App(); me.method_A(); } private void method_A() { // more code here method_B(); return; } private void method_B() { Thread t = new Thread(this); // App is runnable! t.start(); // start executing the run() method return; } public void run() { // more code here } } SE-2811

  4. Both threads execute simultaneously and independently after the secondary thread is started public class App implements Runnable{ public static void main(String[] args) { App me = new App() me.method_A(); } private void method_A() { // more code here method_B(); return; } private void method_B() { Thread t = new Thread(this); t.start();// execute run() on new thread return; } public void run() { // more code here } } SE-2811

  5. The secondary thread may execute a method defined in another class that implements Runnable public class App { public static void main(String[] args) { App me = new App(); me.method_A(); } private void method_A() { ThreadRunnertr = new ThreadRunner(); Thread t = new Thread(tr); t.start(); return; } private class ThreadRunner implements Runnable { // inner class public void run() { // more code here } } } SE-2811

  6. An anonymous inner class is another typical approach… public class App { public static void main(String[] args) { App me = new App(); me.method_A(); } private void method_A() { Thread t = new Thread( new Runnable() { public void run() { // more code here } } ); t.start(); return; } } SE-2811

  7. An application may be designed to execute the same instructions on more than one thread at the same time public class App implements Runnable { public static void main(String[] args) { App me = new App() me.method_A(); } private void method_A() { ThreadRunnertr = new ThreadRunner(); Thread t = new Thread(tr); t.start(); // execute run() on new secondary thread method_C(); // execute method_C on the primary thread } private void method_C() { // more code here } public void run() { // some other instructions here method_C(); // execute method_C on the secondary thread } } SE-2811

  8. Question: Is it good practice to let two (or more) threads execute the same code at the same time? Why or why not? SE-2811

  9. Fortunately, Java supports several mechanisms for synchronizing the execution of multiple threads Keeping code thread-safe S SE-2811

  10. The Thread class’s join() method is one way of synchronizing two threads: Thread t = new Thread(cref); t.start(); t.join(); // wait for 2nd thread to finish… method_C(); // …before executing next inst. • The second Thread starts executing the instructions in the run() method. • The current thread (the launcher of the second thread) waits until the second thread finishes SE-2811

  11. If a method is declared to be synchronized, it will only be run on a single thread at a time public class App implements Runnable { public static void main(String[] args) { App me = new App() me.method_A(); } private void method_A() { ThreadRunnertr = new ThreadRunner(); Thread t = new Thread(tr); t.start(); method_C(); // run method_C on the primary thread } private synchronized void method_C() { // More code here } public void run() { // some other instructions here method_C(); // run method_C on the secondary thread } } SE-2811

  12. Once a thread enters a synchronized method, no other threads can enter until the first thread completes execution of the method, and exits the method. private synchronized void method_C() { <statement 1> <statement 2> <statement 3> } Thread y Thread z Thread x If all threads get to the method at the same time, the thread that gets to enter the method first is determined arbitrarily by the Scheduler. When Thread x leaves the method, the Scheduler arbitrarily allows one of the other threads to enter the method. When the second thread exits, the Scheduler allows another waiting thread to enter. SE-2811

  13. Is synchronized the solution to everything? Can you think of any disadvantage to making a method synchronized? SE-2811

  14. If only a few statements within a method need to be guarded against simultaneous execution, use a synchronized block instead of making the entire method synchronized. private void method_C() { <safe statement 1> <safe statement 2> synchronized( <sync_object> ) { <unsafe statement 3> <unsafe statement 4> <unsafe statement 5>} <safe statement 6> } Thread y Thread z Thread x When Thread x leaves the block, the Scheduler arbitrarily allows one of the other threads to enter. SE-2811

  15. The synchronizing object can be any object • Java’s Object class incorporates the concept of something called a Monitor • Monitors are used to guard the gates of synchronized blocks • Monitors only become active within a synchronized block SE-2811

  16. Since every class derives from Object, the class containing a synchronized block can act as the Monitor for the block: private void method_C() { <safe statement 1> <safe statement 2> synchronized( this ) { // gate down <unsafe statement 3> <unsafe statement 4> <unsafe statement 5> }// gate up <safe statement 6> } Thread y Thread z Thread x SE-2811

  17. Or any generic Object can act as a Monitor private Object guard = new Object();private void method_C() { <safe statement 1> <safe statement 2> synchronized( guard) { // gate down <unsafe statement 3> <unsafe statement 4> <unsafe statement 5> } // gate up <safe statement 6> } Thread y Thread z Thread x SE-2811

  18. Consider the following code. Suppose all threads reach the for() loop simultaneously.How do the threads compete to run the for() loop? private Object guard = new Object();private void method_C() { <safe statement 1> <safe statement 2> for( inti=0; i<100; i++ ) { synchronized( guard ) { // gate down <unsafe statement 3> <unsafe statement 4> <unsafe statement 5> } // gate up } // end for <safe statement 6> } Thread y Thread z Thread x SE-2811

  19. After each thread executes the synchronized section, it can notifiy the Monitor that another thread can be allowed to enter the synchronized block as soon as it relinquishes ownership of the synchronized section by entering a wait (or exiting the synchronized section) private Object guard = new Object();private void method_C() { <safe statement 1> <safe statement 2> for( inti=0; i<100; i++ ) { synchronized( guard ) { // gate down <unsafe statement 3> <unsafe statement 4> <unsafe statement 5> guard.notify(); // signal waiting threads guard.wait(); // wait for other threads } } // end for <safe statement 6> } Thread y Thread z Thread x SE-2811

More Related