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

Slides:



Advertisements
Similar presentations
Concurrent Programming Abstraction & Java Threads
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Concurrency 101 Shared state. Part 1: General Concepts 2.
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
1 L49 Multithreading (1). 2 OBJECTIVES  What threads are and why they are useful.  How threads enable you to manage concurrent activities.  The life.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads Just Java: C10–pages 251- C11–pages 275-
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.
1 Java Threads Instructor: Mainak Chaudhuri
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Concurrent Programming in Java Dr. Zoltan Papp. Motivation: event driven, responsive systems Sequential approach: while ( true ) { do event = getEventId()
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
1 CMSC 341: Data Structures Nilanjan Banerjee Data Structures University of Maryland Baltimore County
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading in JAVA
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Multi-Threading in Java
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
© Andy Wellings, 2004 Thread Priorities I  Although priorities can be given to Java threads, they are only used as a guide to the underlying scheduler.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Software Design 13.1 From controller to threads l Threads are lightweight processes (what’s a process?)  Threads are part of a single program, share state.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Java Thread Programming
Multithreading / Concurrency
Multithreading Lec 23.
Multi Threading.
Threads Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded up by artificially introducing.
Multithreaded Programming in Java
Multithreading Chapter 9.
Threads Chate Patanothai.
Multithreading.
Multithreading.
Multithreaded Programming
Java Concurrency 17-Jan-19.
21 Threads.
Java Concurrency.
Multithreading in java.
Java Concurrency.
Threads and Multithreading
Representation and Management of Data on the Internet
Java Concurrency 29-May-19.
CMSC 202 Threads.
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

©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

©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.

©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.

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

©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() {... } }

©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);

©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();

©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();

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

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

©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

©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

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

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

©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

©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

©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.

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

©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) {... } }

©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

©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

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

©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

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

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

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

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

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