1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.

Slides:



Advertisements
Similar presentations
50.003: Elements of Software Construction Week 6 Thread Safety and Synchronization.
Advertisements

Concurrent Programming Abstraction & Java Threads
CSC321 Concurrent Programming: §3 The Mutual Exclusion Problem 1 Section 3 The Mutual Exclusion Problem.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Mutual Exclusion.
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
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.
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.
1 Threads (Part I) Introduction to Threads and Concurrency Overview  Introduction to Threads of Computation  Creating Threads in java an Example  Thread.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Java Threads CS Introduction to Operating Systems.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
50.003: Elements of Software Construction Week 5 Basics of Threads.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
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 in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
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.
Lecture 2 Foundations and Definitions Processes/Threads.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Processes & Threads Bahareh Goodarzi. Single & Multiple Thread of control code files data code files data.
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.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Multithreading in JAVA
Java Thread and Memory Model
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Concurrency Properties. Correctness In sequential programs, rerunning a program with the same input will always give the same result, so it makes sense.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 More on Thread API.
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.
CMSC 330: Organization of Programming Languages Threads.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
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.
Agenda  Quick Review  Finish Introduction  Java Threads.
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.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
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.
Multithreading / Concurrency
Background on the need for Synchronization
Multithreaded Programming in Java
Threads Chate Patanothai.
Multithreading.
COP 4600 Operating Systems Fall 2010
Java Based Techhnology
Multithreading.
Multithreaded Programming
Java Thread.
Multithreading in java.
Threads and Multithreading
Foundations and Definitions
Programming with Shared Memory Specifying parallelism
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads

2 CSC321 §2 Concurrent Programming Abstraction & Java Threads 2.1: Concurrent Programming Abstraction  The execution of a concurrent program consists of multiple processes active at the same time. Each process is the execution of a sequential program.  If the computer has multiple PEs then there is parallel or real concurrent execution.  However, with a single PE the processor is switched among the processes. This interleaving is referred to as pseudo- concurrent execution.  Without loss of generality, we will always model concurrent execution as interleaved whether or not implementations run on multiple PEs.

3 CSC321 §2 Concurrent Programming Abstraction & Java Threads 2.1.1: Interleaving  The absolute time taken by an atomic instruction is ignored.  Arbitrary interleavings are allowed.  The interleaving must be fair i.e. no process is deferred forever.  A concurrent program is required to be correct under all fair interleavings. Conventional ‘debugging’ to detect and correct errors is impossible Concurrent programming abstraction is the study of interleaved execution of the atomic instructions of sequential processes.

4 CSC321 §2 Concurrent Programming Abstraction & Java Threads Interleaving Consider the following program: process P1 p1a; p1b; end P1; process P2 p2a; p2b; end P2; What are the possible interleavings? p1a; p1b; p2a; p2b; p2a; p2b; p1a; p1b; p1a; p2a; p1b; p2b; p2a; p1a; p2b; p1b; p1a; p2a; p2b; p1b; p2a; p1a; p1b; p2b;

5 CSC321 §2 Concurrent Programming Abstraction & Java Threads Interleaving Consider the following (toy) program: int c1 = 2; int c2 = 3; process P1 p1:c1 = c1 * c2; end P1; process P2 p2:c1 = c1 + c2; end P2; What is the meaning of this program? That is, what are the values of c1 and c2 after execution of the program? Two possible answers: c1 == 9; c2 == 3if p1 executes before p2 c1 == 15; c2 == 3if p2 executes before p1

6 CSC321 §2 Concurrent Programming Abstraction & Java Threads An atomic action makes an indivisible state transition : Atomic instructions It is extremely important to define exactly which instructions are being interleaved i.e. which instructions are atomic. int n=0; process P1 p1: n=n+1; end P1; process P2 p2: n=n+1; end P2; Consider the following simple program which is being executed on a 2-processor computer. Each PE has its own set of registers.

7 CSC321 §2 Concurrent Programming Abstraction & Java Threads Atomic instructions If the compiler translates n=n+1 into a single INC machine language instruction, any interleaving will give the correct result. p2: INC n p1: INC n n==2 p1: INC n p2: INC n n==2 However, if the computation is performed in registers some interleavings will give an incorrect result. p1: load n into R p2: load n into R p1: R=R+1 p2: R=R+1 p1: store R into n p2: store R into n n == 1

8 CSC321 §2 Concurrent Programming Abstraction & Java Threads Atomic instructions The previous example illustrates the lost update problem. This is an example of a race condition. Race conditions occur when two or more processes share data and the final result, which may be erroneous, depends on the interleaving of the processes’ atomic actions. Problem: A bank has 10,000 accounts. Each account has exactly £1,000. Periodically, an ATM process picks two accounts at random and moves a random amount of money from one to the other. Periodically, an Auditor process totals the banks assets to check for embezzlement of funds. What might happen ?

9 CSC321 §2 Concurrent Programming Abstraction & Java Threads 2.1.3: Correctness  Safety properties: the property must always be true (nothing bad will ever happen). Mutual exclusion: processes may not interleave certain sub- sequences of instructions e.g. at most one process is permitted to access a printer at any one instance. Absence of deadlock: deadlock occurs when all processes are blocked and are unable to proceed.  Liveness properties: the property must eventually be true (something good will eventually happen). Absence of starvation e.g. if a process posts a request to print, eventually it will be assigned a printer. Correctness of concurrent programs is defined in terms of properties of execution sequences. There are two types of correctness properties.

10 CSC321 §2 Concurrent Programming Abstraction & Java Threads 2.2: Java Threads  either its own run() method;  or the run() method of a some other object. In Java a process is represented by a thread. To make a thread run you call its start() method. This registers the thread with the thread scheduler. The scheduler, which may be part of the JVM or the host operating system, determines which thread is actually running in the CPU at any given time. Calling the start() method does not cause the thread to run immediately; it only makes it eligible to run. The thread must contend with other threads for the CPU. When a thread gets to execute, it executes a run() method:

11 CSC321 §2 Concurrent Programming Abstraction & Java Threads 2.2.1: Executing a Thread class Interleave { public static int c1 = 2; public static int c2 = 3; public static void main (String[] args) { Thread p1 = new P1 (); Thread p2 = new P2 (); p1.start (); p2.start (); } class P1 extends Thread { public void run () { Interleave.c1 = Interleave.c1 *Interleave.c2; } class P2 extends Thread { public void run () { Interleave.c1 = Interleave.c1 +Interleave.c2; } Extend the Thread class and override its run() method.

12 CSC321 §2 Concurrent Programming Abstraction & Java Threads Executing a Thread class Interleave { public static int c1 = 2; public static int c2 = 3; public static void main (String[] args) { Thread p1 = new Thread(new P1()); Thread p2 = new Thread(new P2()); p1.start (); p2.start (); } class P1 implements Runnable { public void run () { Interleave.c1 = Interleave.c1 *Interleave.c2; } class P2 implements Runnable { public void run () { Interleave.c1 = Interleave.c1 +Interleave.c2; } Sometimes it is desirable to implement the run() method in a class not derived from Thread but from the interface Runnable.

13 CSC321 §2 Concurrent Programming Abstraction & Java Threads Executing a Thread  A dead thread cannot be restarted.  A dead thread’s methods can be called. When the run() method ends the thread is considered dead. A dead thread cannot be started again, but it still exists and, like any other object, its methods and data can still be accessed.

14 CSC321 §2 Concurrent Programming Abstraction & Java Threads 2.2.2: Thread States When a thread’s start() method is called, the thread goes into a ready-to-run state and stays there until the scheduler moves it to the running state. In the course of execution the thread may temporarily give up the CPU and enter some other state. Running Monitor states Ready SuspendedAsleep Blocked

15 CSC321 §2 Concurrent Programming Abstraction & Java Threads Thread states - Yielding A thread can offer to move out of the CUP by yielding. A call to the static yield() method causes the currently executing thread to move to the Ready state. If there are any other threads in the Ready state, the thread that just yielded may have to wait before it gets to execute again. If there are no waiting threads in the Ready state the thread that just yielded will get to continue executing immediately. Running Ready Thread.yield()scheduled

16 CSC321 §2 Concurrent Programming Abstraction & Java Threads Thread states - Sleeping A call to the static sleep() method requests the currently executing thread to cease executing for an approximately specified period in milliseconds. scheduled Running Ready Thread.sleep(20) Asleep time expires or interrupted Note: when the thread finishes sleeping (20 milliseconds) it does not continue execution directly. The Thread class has an interrupt() method. A sleeping thread that receives an interrupt() call moves immediately into the Ready state; when it gets to run it will execute its InterruptedException handler.

17 CSC321 §2 Concurrent Programming Abstraction & Java Threads Thread states - Blocking If a method needs to wait for an indeterminable amount of time until some I/O occurrence takes place it should step out of the Running state. This is know as blocking. All Java I/O methods behave this way. scheduled Running Ready blocking method Blocked Blocking condition changes or interrupted A thread can also become blocked if it fails to acquire the lock for a monitor or if it issues a wait() call. This will be explained later.

18 CSC321 §2 Concurrent Programming Abstraction & Java Threads Thread states - Suspending, Resuming and Stopping The suspend() method allows any arbitrary thread to make another thread un-runnable for an indefinite period of time. The suspended thread becomes runnable when some other thread resumes it using the resume() method. The stop() method allows any arbitrary thread to kill another thread. These three methods are now deprecated. They should be avoided as they are unsafe and can easily lead to deadlock.

19 CSC321 §2 Concurrent Programming Abstraction & Java Threads 2.2.3: Thread Priorities and Scheduling Every thread has a priority (from 1..10). All newly created threads have their priority set to that of the creating thread. Higher priority threads get preference over lower priority threads. The scheduler generally chooses the the highest-priority waiting thread. If there is more than one waiting thread the scheduler chooses one of them: there is no guarantee that the one chosen is the one that has been waiting longest. int oldPriority = aThread.getPriority(); int newPriority = Math.min(oldPriority+1, Thread.MAX_PRIORITY); aThread.setPriority(newPriority); Note: The way thread priorities affect scheduling is platform dependent.

20 CSC321 §2 Concurrent Programming Abstraction & Java Threads Thread Priorities and Scheduling  Preemptive scheduling (Solaris, Windows pre JDK1.0.2) - A thread runs until: it leaves the Running state by yielding, blocking, sleeping etc; it gives way to a higher-priority thread.  Time-sliced (Mac, Windows with JDK 1.0.2) - A thread is allowed to execute for a limited amount of time (approximately 100 milliseconds). It is then moved into the Ready state where it must contend with all other ready threads. Historically, two approaches have emerged for implementing thread schedulers.