190 likes | 276 Views
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 ) {
E N D
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
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
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
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
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
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
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
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
Fortunately, Java supports several mechanisms for synchronizing the execution of multiple threads Keeping code thread-safe S SE-2811
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
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
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
Is synchronized the solution to everything? Can you think of any disadvantage to making a method synchronized? SE-2811
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
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
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
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
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
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