multi-threaded applications se-28111. se-2811 dr. mark l. hornick 2 what se1011 students are told…...

29
Multi-threaded applications SE-2811 1

Upload: jacey-postle

Post on 15-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

Multi-threaded applications

SE-2811 1

Page 2: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

SE-2811Dr. Mark L. Hornick

2

What SE1011 students are told…

When the main() method is called, the instructions within the method begin to execute in sequenceThe program terminates when the main()

method finishes executing

Is this really true?Sometimes it is….

Page 3: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

SE-2811Dr. Mark L. Hornick

3

The ugly truth…

When the main() method is called, the instructions within the method begin to execute in sequence on the primary threadThe program terminates when the

primary thread, and any additional threads, finish executing

Page 4: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

What’s a Thread?First, let’s define Process:

A Process is most easily understood as a program or application running on your PC

A process generally has a complete, private set of basic run-time resources, in particular: Its own memory space Execution priority A list of Threads that execute within it A set of credentials with which to execute

(usually yours); this provides authorization to access various resources, such as files

SE-2811 4

Page 5: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

By default, a Process creates and executes a single primary Thread

BUT:A Process can create and execute more than one Thread

The JVM works with the OS to create Processes and Threads The underlying OS provides the essential

multiprocessing support

SE-2811 5

Page 6: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

Modern operating systems are all capable of running multiple Processes simultaneously

(On single-CPU PC’s) each Process runs individually for a discrete time period

while one Process runs, other Processes sleep The Process currently executing changes very

rapidly - every few millisecondsOperating systems use a Scheduler (basically, an ISR that executes on a timer interrupt) to distribute CPU time among Processes

The net effect is that you (the user) observe all processes running simultaneously and continuously

SE-2811 6

Page 7: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

When you run a Java application, the JVM creates a Process and a Primary Thread The Primary Thread begins executing the

main() method in the main classNote: other java programs, like applets, begin

execution with an init() method

If no other Threads are created, the Process terminates when the Primary Thread terminatesThat is, when there are no more instructions

to execute on that Thread

SE-2811 7

Page 8: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

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 heremethod_B();return;

}private void method_B() {

return;}private void method_C() {

// more code here}

}

SE-2811 8

Page 9: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

Where do other Threads come from?

1. You implicitly create additional Threads when you write a Swing-based application

Java applications that create and display windows cause Swing to create additional threads

2. You implicitly create additional Threads when you use various Java utility classes

Using the Timer class causes a Thread to be created

3. You can explicitly create additional Threads and control their execution

SE-2811 9

Page 10: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

You already know how to create a multi-threaded app using Swing

1. Create a JFrame window containing JButtons, JTextField, etc.

2. Connect the JButtons etc to an ActionListener

3. Make the window visible Once the window is visible, a second Thread is

created All calls to actionPerformed() occur on the second

Thread The Event-Dispatching Thread

SE-2811Dr. Mark L. Hornick

10

Page 11: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

Using a Timer is fairly straighforward:

Timer timer = new Timer(timeoutPeriod, eventHandler);timer.start();

The eventHandler argument to the constructor is a reference to a class that implements Timer ActionListener

That is, eventHandler contains an actionPerformed() method.

This is similar to how Swing events are handled

Whenever the Timer generates a timeout event, the JVM invokes actionPerformed() on another thread JVM uses the Event Dispatch thread when available; otherwise a

“worker” thread is created

SE-2811Dr. Mark L. Hornick

11

Page 12: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

Explicitly creating additional Threads is pretty easy:Thread t = new Thread( r );t.start();The r argument to the Thread constructor is a reference to

a class that implements the Runnable interface Runnable declares a single method: public void run()

When the Thread’s start() method is called, the instructions in the run() method begin executing on the new thread.

The start() method returns essentially immediately; it does not wait for the started thread to finish execution.

SE-2811 12

Page 13: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

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 heremethod_B();return;

}private void method_B() {

Thread t = new Thread(this); // App is runnable!t.start(); // start executing the run() methodreturn;

}public void run() {

// more code here}

} SE-2811 13

Page 14: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

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 heremethod_B();return

}private void method_B() {

Thread t = new Thread(this);t.start();// execute run() on new threadreturn;

}public void run() {

// more code here}

} SE-2811 14

Page 15: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

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() {

ThreadRunner tr = 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 15

Page 16: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

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 16

Page 17: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

An application may be designed to execute the same instructions on more than one thread at the same timepublic class App implements Runnable {

public static void main(String[] args) {App me = new App()me.method_A();

} private void method_A() {

ThreadRunner tr = new ThreadRunner();Thread t = new Thread(tr);t.start(); // execute run() on new secondary threadmethod_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 17

Page 18: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

Question:

Is it a good idea to let two (or more) threads execute the same code at the same time?

SE-2811 18

Page 19: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

Fortunately, Java supports several mechanisms for synchronizing the execution of multiple threads

SE-2811 19

S

Keeping code thread-safe

Page 20: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

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 20

Page 21: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

If a method is declared to be synchronized, it will only be run on a single thread at a timepublic class App implements Runnable {

public static void main(String[] args) {App me = new App()me.method_A();

} private void method_A() {

ThreadRunner tr = 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 21

Page 22: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

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>

}

SE-2811 22

Thread x Thread y Thread z

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.

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.

Page 23: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

Is synchronized the solution to everything?

Can you think of any disadvantage to making a method synchronized?

SE-2811 23

Page 24: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

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>

}

SE-2811 24

Thread x Thread y Thread z

When Thread x leaves the block, the Scheduler arbitrarily allows one of the other threads to enter.

Page 25: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

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 25

Page 26: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

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>

}

SE-2811 26

Thread x Thread y Thread z

Page 27: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

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>

}

SE-2811 27

Thread x Thread y Thread z

Page 28: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

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( int i=0; i<100; i++ ) { synchronized( guard ) { // gate

down <unsafe statement 3> <unsafe statement 4> <unsafe statement 5> } // gate

up} // end for <safe statement 6>

}SE-2811 28

Thread x Thread y Thread z

Page 29: Multi-threaded applications SE-28111. SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions

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( int i=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>

} SE-2811 29

Thread x Thread y Thread z