EE 422C Multithreading & Parallel Programming

Slides:



Advertisements
Similar presentations
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Advertisements

Concurrent Programming Abstraction & Java Threads
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Multithreading The objectives of this chapter are:
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
Thread Control methods The thread class contains the methods for controlling threads Thread() create default thread Thread(target: runnable ) creates new.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 19 Multithreading.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
Java Threads Representation and Management of Data on the Internet.
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.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Advanced Programming 2004, based on LY Stefanus’s slides slide 8.1 Multithreading : Thread Scheduling ThreadGroup.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Multithreading [Modified]
Multithreading in JAVA
Java Thread and Memory Model
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.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
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.
1 Chapter 19 Multithreading. 2 Objectives F To understand the concept of multithreading and apply it to develop animation (§19.2). F To develop thread.
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.
Multithreading (Based on Chapter 29 (Multithreading) Liang book and Chapter 5 (Threads) Elliotte Rusty book) Dr. Tatiana Balikhina 1.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Java Thread Programming
Multithreading The objectives of this chapter are:
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Multithreading / Concurrency
Chapter 30 Multithreading and Parallel Programming
Multi Threading.
Java Multithreading.
Multithreading.
Multithreaded Programming in Java
Lecture 21 Concurrency Introduction
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
ITEC324 Principle of CS III
Multithreading.
Multithreading 2 Lec 24.
Java Based Techhnology
Multithreading.
Multithreaded Programming
Programming with Shared Memory Java Threads and Synchronization
Threads Chapter 4.
9. Threads SE2811 Software Component Design
Computer Science 2 06A-Java Multithreading
Multithreading in java.
NETWORK PROGRAMMING CNET 441
CS333 Intro to Operating Systems
Threads and Multithreading
Multithreading The objectives of this chapter are:
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

EE 422C Multithreading & Parallel Programming

Concurrency In real life, things happen concurrently. In hardware design, we have all parts of a circuit working at the same time. In software, things happen one after the other, in sequence.

Operating System Terminology Kernel - The central component of the computer operating systems. The kernel's responsibilities include among other things context switching. Context Switch - A context switch is the process of storing and restoring the state (context) of a CPU such that multiple processes can share a single CPU resource Preemption - preemption is the act of temporarily interrupting a task being carried out by a computer system, without requiring its cooperation, and with the intention of resuming the task at a later time.

Multithreading View a process as a collection of one or more threads that can run simultaneously All threads within the same process share the same data and resources and a part of the process’s execution context Easier to create or destroy a thread and switch between threads (of the same process) than to create/switch processes “Context switch” for a process is a bigger operation than switching threads, because threads run in the same virtual memory space, etc, shares open files, etc….

Advantages of Multithreading Reactive systems – constantly monitoring More responsive to user input – GUI application can interrupt a time-consuming task Server can handle multiple clients simultaneously Can take advantage of parallel processing

Threads vs. Processes Threads are used for small tasks, whereas processes are used for more 'heavyweight' tasks – basically the execution of applications. Another difference between a thread and a process is that threads within the same process share the same address space, whereas different processes do not.

Examples of Multithreading Typing in a word processor while it is saving or spellchecking. Chrome Each tab is a thread Massively parallel server monitoring infrastructure, for example you are required to monitor 30K servers/routers every 5 seconds Long running applications like batch jobs, do well when multithreaded. Any GUI toolkit, like Swing, Javafx, SWT/JFace all uses threads to keep track of events generating from various widgets in the GUI HTTP/JEE servers/frameworks like vert.x, wildfly, tomcat, jetty uses multithreads along with NIO to achieve high throughput. JUnit uses threads to run test cases in parallel. Computer games, exemplary multi-threaded processing.

Single vs. multiple CPU

Thread Scheduling An operating system’s thread scheduler determines which thread runs next. Most operating systems use timeslicing for threads of equal priority. Preemptive scheduling: when a thread of higher priority enters the running state, it preempts the current thread. Starvation: Higher-priority threads can postpone (possible forever) the execution of lower-priority threads.

Thread States Start Ready Running Waiting Sleeping Dead USC CSCI 201L When a thread is started Ready When the OS/JVM switches the thread out of the CPU or the thread yields the CPU When the OS/JVM switches the thread into the CPU When a thread is signaled/notified based on the resource on which it is waiting When the amount of time specified for sleeping has elapsed Running When a thread waits on a resource to become available When a thread puts itself to sleep for a certain amount of time When a thread has completed execution Waiting Sleeping Dead USC CSCI 201L

Thread termination A thread becomes Not Runnable when one of these events occurs: Its sleep method is invoked. The thread calls the wait method to wait for a specific condition to be satisifed. The thread is blocking on I/O.

EE 422C

Deadlock EE 422C

Starvation If a thread is not granted CPU time because other threads grab it all, it is called "starvation". The thread is "starved to death" because other threads are allowed the CPU time instead of it. The solution to starvation is called "fairness" - that all threads are fairly granted a chance to execute.

Race Condition What, then, caused the error in the example? Here is a possible scenario: The effect of this scenario is that Task 1 did nothing, because in Step 4 Task 2 overrides Task 1's result. Obviously, the problem is that Task 1 and Task 2 are accessing a common resource in a way that causes conflict. This is a common problem known as a race condition in multithreaded programs. A class is said to be thread-safe if an object of the class does not cause a race condition in the presence of multiple threads. As demonstrated in the preceding example, the Account class is not thread-safe.

Java Tasks and Threads In Java, each task is an instance of the Runnable interface, also called a runnable object. A thread is essentially an object that facilitates the execution of a task.

Tasks and Threads A Task object implements the Runnable interface. A run method in the Task object describes the task that has to run in that thread. A Thread object is created with the task object as parameter. thread_object.start() tells the JVM that the thread is ready to run.

Using a thread -- Runnable // custom task class public Class TaskClass implements Runnable { … public TaskClass(…) {…} // implement run method public void run() { // tell system how to run custom thread } … // Client class public class Client { … public void someMetd() { // create instance of TaskClass TaskClass task = new TaskClass(…); // create a thread Thread thr = new Thread(task); thr.start(); }

The Static sleep(milliseconds) Method The sleep(long mills) method puts the thread to sleep for the specified time in milliseconds. For example, suppose you modify the code in Lines 53-57 in TaskThreadDemo.java as follows: public void run() { for (int i = 1; i <= lastNum; i++) { System.out.print(" " + i); try { if (i >= 50) Thread.sleep(1); } catch (InterruptedException ex) {   Every time a number (>= 50) is printed, the print100 thread is put to sleep for 1 millisecond.

The join() Method You can use the join() method to force one thread to wait for another thread to finish. For example, suppose you modify the code in Lines 53-57 in TaskThreadDemo.java as follows: The numbers after 50 are printed after thread printA is finished.

Thread priorities Java assigns every thread a priority. Increase or decrease the priority of any thread by using the setPriority method. Priorities are numbers ranging from 1 to 10. The Thread class has the int constants MIN_PRIORITY, NORM_PRIORITY, and MAX_PRIORITY, representing 1, 5, and 10, respectively. The priority of the main thread is Thread.NORM_PRIORITY. The JVM always picks the currently runnable thread with the highest priority. If all runnable threads have equal priorities, each is assigned an equal portion of the CPU time in a circular queue. This is called round-robin scheduling.

isAlive(), interrupt(), and isInterrupted() The isAlive() method is used to find out the state of a thread. It returns true if a thread is in the Ready, Blocked, or Running state; it returns false if a thread is new and has not started or if it is finished. The interrupt() method interrupts a thread in the following way: If a thread is currently in the Ready or Running state, its interrupted flag is set; if a thread is currently blocked, it is awakened and enters the Ready state, and an java.io.InterruptedException is thrown. The isInterrupt() method tests whether the thread is interrupted.

Synchronizing Tasks

Intrinsic Locks and Synchronization Synchronization is built around an internal entity known as the intrinsic lock or monitor. Every object has an intrinsic lock associated with it. A thread that needs exclusive and consistent access to an object's fields has to acquire the object's intrinsic lock before accessing them, and then release the intrinsic lock when it's done with them. A thread is said to own the intrinsic lock between the time it has acquired the lock and released the lock. As long as a thread owns an intrinsic lock, no other thread can acquire the same lock. The other thread will block when it attempts to acquire the lock. When a thread releases an intrinsic lock, a happens-before relationship is established between that action and any subsequent acquisition of the same lock. EE 422C

Synchronization To make a method synchronized, simply add the synchronized keyword to its declaration: public class SynchronizedCounter { private int c = 0; public synchronized void increment() { c++; } public synchronized void decrement() { c--; public synchronized int value() { return c; https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html EE 422C

Synchronization If count is an instance of SynchronizedCounter, then making these methods synchronized has two effects: It is not possible for two invocations of synchronized methods on the same object to interleave. Other threads that want the same method will wait. When a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads. Constructors cannot be synchronized. EE 422C

Synchronized Statements Synchronized statements must specify the object that provides the intrinsic lock: public void addName(String name) { synchronized(this) { lastName = name; nameCount++; } nameList.add(name); Avoid synchronizing invocations of other objects' methods. EE 422C

Synchronized Statements synchronized(this) { } The parameter to synchronized is a reference to an object. The object is locked until the statements finish executing. No changes can be made to the object. The lock is released upon finishing the statements. No other thread can interleave with the statements inside the synchronized block. EE 422C

Synchronized Collections Invoking synchronizedCollection(Collection c) returns a new Collection object, in which all the methods that access and update the original collection c are synchronized. These methods are implemented using the synchronized keyword. For example, the add method is implemented like this: public boolean add(E o { synchronized (this) { return c.add(o); } EE 422C

Reentrant Synchronization Recall that a thread cannot acquire a lock owned by another thread. But a thread can acquire a lock that it already owns. Allowing a thread to acquire the same lock more than once enables reentrant synchronization. EE 422C