Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 ©SoftMoore ConsultingSlide 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

2 ©SoftMoore ConsultingSlide 2 Importance of Concurrent Programming 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.

3 ©SoftMoore ConsultingSlide 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.

4 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 ConsultingSlide 4

5 ©SoftMoore ConsultingSlide 5 Creating a Thread by Implementing the 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() {... } }

6 ©SoftMoore ConsultingSlide 6 Creating a Thread by Implementing the 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);

7 ©SoftMoore ConsultingSlide 7 Creating a Thread by Implementing the 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();

8 ©SoftMoore ConsultingSlide 8 Creating a Thread by Extending the Thread Class public class SimpleThread extends Thread { public SimpleThread() {... } public void run() {... } // elsewhere (in another class) Thread t = new SimpleThread(); t.start();

9 ©SoftMoore ConsultingSlide 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) {} } } }

10 ©SoftMoore ConsultingSlide 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(); }

11 ©SoftMoore ConsultingSlide 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 run Can 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

12 ©SoftMoore ConsultingSlide 12 Thread States New Dead Blocked ReadyRunning processor available yield Runnable sleep wake up block on I/O I/O complete wait for lock lock available wait notify suspend resume run method exits stop gray = deprecated start

13 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 ConsultingSlide 13

14 ©SoftMoore ConsultingSlide 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 Thread public static final int MAX_PRIORITY public static final int MIN_PRIORITY public static final int NORM_PRIORITY // default Methods public void setPriority(int newPriority) public int getPriority()

15 ©SoftMoore ConsultingSlide 15 Thread Interaction Threads need to interact with each other for several reasons: Information Exchange Activity Synchronization –to coordinate the activities of parallel threads executing asynchronously Mutual Exclusion –to get exclusive access to a shared resource

16 ©SoftMoore ConsultingSlide 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 is mutual exclusion.” – M. Ben-Ari

17 ©SoftMoore ConsultingSlide 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.

18 ©SoftMoore ConsultingSlide 18 Synchronization (continued) Synchronized block... // statements synchronized(someObject) // Note: someObject is { // often this … // statements } Synchronized methods public synchronized void someMethod() { … // statements }

19 ©SoftMoore ConsultingSlide 19 Deadlock Deadlock is the situation in which one or more threads become permanently blocked waiting for resources that will never become available. Example thread1 synchronize(resource1) { synchronize(resource2) {... } } time thread2 synchronize(resource2) { synchronize(resource1) {... } }

20 ©SoftMoore ConsultingSlide 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

21 ©SoftMoore ConsultingSlide 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

22 ©SoftMoore ConsultingSlide 22 Race Conditions (continued) Race conditions typically have irreproducible results, making the construction of reliable concurrent systems difficult.

23 ©SoftMoore ConsultingSlide 23 Asynchronous Communication Asynchronous communication between two threads can be achieved by defining a third thread as a mailbox. Example public 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. thread 1 thread 2 mailbox

24 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 ConsultingSlide 24

25 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 ConsultingSlide 25

26 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 ConsultingSlide 26

27 Concurrent Collections (in java.util.concurrent ) Interfaces –BlockingDeque –BlockingQueue –ConcurrentMap Implementations (classes) –ArrayBlockingQueue –ConcurrentHashMap –ConcurrentLinkedQueue –CopyOnWriteArrayList –CopyOnWriteArraySet –LinkedBlockingDeque –LinkedBlockingQueue –PriorityBlockingQueue ©SoftMoore ConsultingSlide 27

28 Guidelines for Effective Java (Joshua Bloch) Synchronize access to shared mutable data Synchronization is required for reliable communication between threads as well as for mutual exclusion. Avoid excessive synchronization As 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 notify e.g., use ConcurrentHashMap in preference to Collections.synchronizedMap or Hashtable Document thread safety ©SoftMoore ConsultingSlide 28


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

Similar presentations


Ads by Google