Top Banner
©SoftMoore Consulting Slide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded up by artificially introducing concurrency, but from the fact that the real world functions by the execution of concurrent activities.” P. Wegner
28

©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

Dec 16, 2015

Download

Documents

Annis Pearson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 1

Appendix D:Java Threads

"The real payoff of concurrent execution arises notfrom the fact that applications can be speeded up by artificially introducing concurrency, but from the fact that the real world functions by the execution of concurrent activities.”

– P. Wegner

Page 2: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 2

Importance of ConcurrentProgramming Facilities

• Concurrent execution (on multiprocessing hardware) can improve program performance.

• Many applications are modeled more naturally as systems of concurrently executing threads. A concurrent program preserves the structure of a concurrent problem.

• Concurrent programming facilities enhance the programmer’s ability to “invent” concurrent solutions.

Page 3: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 3

Threads

• A thread (a.k.a., a lightweight process) is a single sequential flow of control within a Java program.

• A thread may operate in parallel with other threads.

• Concurrency may be real (if the underlying computer has more than one processor) or simulated (via interleaved execution on a single processor).

• Conceptually it is best to think of each thread as having its own personal processor.

• Java provides support for concurrency at the class level, not at the statement level.

Page 4: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

Creating Threads

• Two ways to create a thread:1. Create a class that implements the Runnable interface.

public interface Runnable { void run(); }

2. Extend the Thread class and override its run() method.

• If your class must extend some other class, you will need to use the first alternative.

©SoftMoore Consulting Slide 4

Page 5: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 5

Creating a Thread by Implementingthe Runnable Interface

1. Create a class that implements the Runnable interface, and place the code for the thread in the run() method. public class SimpleRunnable extends SomeClass implements Runnable { public SimpleRunnable() { ... }

public void run() { ... } }

Page 6: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 6

Creating a Thread by Implementingthe Runnable Interface (continued)

2. Construct an object of the class. Runnable r = new SimpleRunnable();

3. Obtain an Executor object (e.g., from java.util.concurrent.ExecutorService). Executor e = ...;

4. Execute the thread. e.execute(r);

Page 7: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 7

Creating a Thread by Implementingthe Runnable Interface (continued)

Alternate steps 3 and 4 (required prior to Java 5)

3. Construct a Thread object from the Runnable object. Thread t = new Thread(r);

4. Start the thread. t.start();

Page 8: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 8

Creating a Thread byExtending the Thread Class

public class SimpleThread extends Thread { public SimpleThread() { ... }

public void run() { ... } }

// elsewhere (in another class)Thread t = new SimpleThread();t.start();

Page 9: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 9

Example: Simple Thread

public class SimpleRunnable implements Runnable { private String message;

public SimpleRunnable(String message) { this.message = message; }

public void run() { for (int i = 0; i < 10; ++i) { System.out.println(i + " " + message); try { Thread.sleep((int)(Math.random()*100)); } catch (InterruptedException e) {} } } }

Page 10: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 10

Example: Simple Thread(continued)

public class ThreadTest { public static void main (String[] args) { Runnable r1 = new SimpleRunnable("Hello"); Runnable r2 = new SimpleRunnable("World");

Thread t1 = new Thread(r1); Thread t2 = new Thread(r2);

t1.start(); t2.start(); } }

Page 11: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 11

Thread States

A thread can be in one of four states:

1. New – the thread object has been created, but its start() method has not been called.

2. Runnable – the thread is ready to runCan be in one of two substates:

a. running: currently assigned to a processor and actively executing

b. ready: unblocked and waiting for processing

3. Blocked – sleeping, waiting for a notification, or blocking on I/O

4. Dead – no longer active

Page 12: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 12

Thread States

New

Dead

BlockedReady Running

processoravailable

yield

Runnable

sleep

wake up

block on I/O

I/O complete

wait for lock

lock available

wait

notify

suspend

resume

runmethod

exitsstop

gray = deprecated

start

Page 13: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

Example: Monitoring Water Temperature

public class WaterMonitor extends Thread { private static final long INTERVAL = 5*SECONDS; ...

public void run() { long nextTime = System. currentTimeMillis(); float temperature;

while (true) { Thread.sleep(nextTime – System.currentTimeMillis()); temperature = getWaterTemperature(); if (temperature > MAX_TEMPERATURE) activateAlarm(): nextTime = nextTime + INTERVAL; } } }

©SoftMoore Consulting Slide 13

Page 14: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 14

Thread Priority

• Each thread has a priority. A higher value indicates a higher degree of urgency.

• If two threads are in the ready state, the one with the highest priority will be selected to run.

• Constants in class Threadpublic static final int MAX_PRIORITYpublic static final int MIN_PRIORITYpublic static final int NORM_PRIORITY // default

• Methodspublic void setPriority(int newPriority)public int getPriority()

Page 15: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 15

Thread Interaction

Threads need to interact with each other for severalreasons:

• Information Exchange

• Activity Synchronization– to coordinate the activities of parallel threads executing

asynchronously

• Mutual Exclusion– to get exclusive access to a shared resource

Page 16: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 16

Protecting Shared Resources

• Whenever several threads have access to a shared resource, we must maintain the integrity of the operations and data.

• Mutual exclusion means that only one thread of control can operate upon that resource at a time – multiple threads of control are serialized.

• In order to prevent simultaneous updates by different threads, we must provide exclusive access to a shared resource.

“The basic concurrent programming problem ismutual exclusion.”

– M. Ben-Ari

Page 17: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 17

Synchronization

• Objects have implicit locks.

• Synchronization is based on objects.

• Synchronized code is atomic – only one thread at a time can execute the synchronized code.

Page 18: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 18

Synchronization(continued)

• Synchronized block... // statementssynchronized(someObject) // Note: someObject is { // often this … // statements

}

• Synchronized methodspublic synchronized void someMethod() { … // statements }

Page 19: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 19

Deadlock

• Deadlock is the situation in which one or more threads become permanently blocked waiting for resources that will never become available.

• Examplethread1

synchronize(resource1) {

synchronize(resource2) { ... } }

timethread2

synchronize(resource2) {

synchronize(resource1) { ... } }

Page 20: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 20

Deadlock(continued)

• Conditions for deadlock– mutual exclusion– partial resource allocation– nonpreemptive scheduling– circular waiting

• Reasons for deadlock– poor program design– occurrence of unanticipated events

Page 21: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 21

Race Conditions

• A race condition is a set of circumstances in which the relative speeds of two threads can influence the result of program execution. It can occur when two concurrent threads operate on a shared resource in overlapping time intervals.

• Example

thread1

if (!stack.isEmpty())

x = stack.pop();

thread2

if (!stack.isEmpty())

x = stack.pop();

time

Page 22: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 22

Race Conditions(continued)

• Race conditions typically have irreproducible results, making the construction of reliable concurrent systems difficult.

Page 23: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

©SoftMoore Consulting Slide 23

Asynchronous Communication

• Asynchronous communication between two threads can be achieved by defining a third thread as a mailbox.

• Examplepublic class MailBox { public synchronized void write(String message) {…} public synchronized String read() {…} … // other implementation details }

Mailboxes could be created dynamically as needed.

A blocking queue could be used to implement the mailbox.

thread1

thread2

mailbox

Page 24: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

Thread Safety(Brian Goetz)

• A class is thread-safe if it behaves correctly when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of those threads by the runtime environment, and with no additional synchronization or other coordination on the part of the calling code.

• Writing thread-safe code is about managing access to shared, mutable state, where an object’s state is its data.

• Thread-safe classes encapsulate any needed synchronization so that clients need not provide their own.

©SoftMoore Consulting Slide 24

Page 25: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

More on Thread Safety

• Stateless objects and immutable objects are always thread-safe.

• Parameters and local variables (declared within a method) are stored on the run-time stack, and each thread has its on stack. A method that uses only local variables or parameters is always thread safe as long as it doesn’t use parameters to modify mutable objects.

• An object that is accessed by only one thread need not be thread-safe.– simpler, better performance– but … are you sure that it never will be accessed by multiple

threads?

©SoftMoore Consulting Slide 25

Page 26: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

Levels of Thread Safety(Joshua Bloch)

• Immutable

• Unconditionally thread-safe– class has sufficient internal synchronization

• Conditionally thread-safe– some methods require external synchronization

• Not thread-safe– require external synchronization– examples: ArrayList and HashMap

• Thread-hostile– usually not-intentional– example: System.runFinalizersOnExit() method

©SoftMoore Consulting Slide 26

Page 27: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

Concurrent Collections(in java.util.concurrent)

• Interfaces– BlockingDeque– BlockingQueue– ConcurrentMap

• Implementations (classes)– ArrayBlockingQueue– ConcurrentHashMap– ConcurrentLinkedQueue– CopyOnWriteArrayList– CopyOnWriteArraySet– LinkedBlockingDeque– LinkedBlockingQueue– PriorityBlockingQueue

©SoftMoore Consulting Slide 27

Page 28: ©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.

Guidelines for Effective Java(Joshua Bloch)

• Synchronize access to shared mutable dataSynchronization is required for reliable communication between threads as well as for mutual exclusion.

• Avoid excessive synchronizationAs a rule, do as little work as possible inside synchronized regions.

• Prefer executors to tasks and threads(More on this when we study networking.)

• Prefer concurrency utilities to wait and notifye.g., use ConcurrentHashMap in preference to Collections.synchronizedMap or Hashtable

• Document thread safety

©SoftMoore Consulting Slide 28