1 Concurrent Programming CS 3331 Chapter 11. 2 Outline What’re threads? How to create threads? How to controlling threads? Thread safety and synchronization.

Slides:



Advertisements
Similar presentations
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Advertisements

CS 5704 Fall 00 1 Monitors in Java Model and Examples.
CS Concurrent Programming CS 3331 Fall 2009.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 11.
Definitions Process – An executing program
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-
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.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Multithreading.
50.003: Elements of Software Construction Week 5 Basics of Threads.
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.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
Critical Reference An occurrence of a variable v is defined to be critical reference: a. if it is assigned to in one process and has an occurrence in another.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
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.
Processes & Threads Bahareh Goodarzi. Single & Multiple Thread of control code files data code files data.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Internet Software Development Controlling Threads Paul J Krause.
Threading Eriq Muhammad Adams J
Threads.
1 Object Oriented Programming Lecture XII Multithreading in Java, A few words about AWT and Swing, The composite design pattern.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Java Thread and Memory Model
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
© Wang Bin 2004 Java Threads. © Wang Bin 2004 In this lesson, you will learn to: u Define the concepts of threads and multithreading  Identify the functions.
Multi-Threading in Java
CMSC 330: Organization of Programming Languages Threads.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
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.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
CS 151: Object-Oriented Design November 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
1 Java Programming Java Programming II Concurrent Programming: Threads ( II)
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.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
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.
Concurrent Programming in JAVA Steve Pruett - Introduction Jaime Mendez – Thread Creation Adrian Garcia – Thread Management Eric Orozco – Thread Safety.
Tutorial 2: Homework 1 and Project 1
Multithreading / Concurrency
Multi Threading.
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
CSE 501N Fall ‘09 21: Introduction to Multithreading
Multithreaded Programming in Java
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 3: Process Concept
Lecture 21 Concurrency Introduction
Multithreaded Programming in Java
Condition Variables and Producer/Consumer
Multithreading.
Condition Variables and Producer/Consumer
Multithreading.
برنامه‌نویسی چندنخی Multi-Thread Programming
Computer Science 2 06A-Java Multithreading
Threads and Multithreading
Lecture 19 Threads CSE /6/2019.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

1 Concurrent Programming CS 3331 Chapter 11

2 Outline What’re threads? How to create threads? How to controlling threads? Thread safety and synchronization BoundedQueue example

3 Background: Multitasking Efficient use of expensive CPU time  Fast CPU and slow peripherals (I/O)  Run another program while waiting for I/O  Context switching among tasks or processes  Shared memory vs. message-based (IPC)  Built-in language features vs. library-based Multithreading  Shard memory, lightweight multitasking  No context switching of virtual memory space

4 Multithreaded Programming Thread  A single sequential flow of control within a program, i.e., a lightweight flow that executes concurrently with other threads within a single process.  Communicates with other threads via shared variables and objects.  Java language feature  Managed by JVM. flow of control sequential concurrent

5 Why? Can take advantages of multiple CPUs. Allows a server to handle multiple clients simultaneously. Suitable for developing reactive system. Makes application more responsive to user inputs (e.g., GUI programs such as BouncingBall applet). Natural to model real worlds.

6 However, More difficult to program and debug (e.g., shared access to objects) Nondeterministic order of thread execution Safety and liveness problems

7 Creating Threads in Java Two approaches  By extending the java.lang.Thread class  By implementing the java.lang.Runnable interface

8 Creating Threads (Cont.) Extending the Thread class public class MyThread extends Thread { public void run() { // code to be run concurrently } // start a new thread by calling the start method MyThread thread = new MyThread(); thread.start(); // Q: What happened if run is called directly? thread.run() // Q: Design pattern used? flow of control start() run() // … The method run is the concurrent unit in Java.

9 Creating Threads (Cont.) Implementing the Runnable interface public class MyRunnable implements Runnable { public void run() { // code to be concurrently } // create a new thread by passing a Runnable object and start it, e.g., new Thread(new MyRunnable()).start(); // or using anonymous inner class new Thread(new Runnable() { public void run() { /* concurrent code */ } }).start(); // In Java 8 using lambda notation new Thread(() -> { /* concurrent code */ }).start() public interface Runnable { void run(); }

10 Example --- Simple Counter public class Counter extends Thead { private int count, inc, delay; public Counter(int init, int inc, int delay) { this.count = init; this.inc = inc; this.delay = delay; } public void run() { try { for (;;) { System.out.print(count + “ “); count += inc; sleep(delay); // suspend delay miliseconds } } catch (InterruptedException e) {} } public static void main(String[] args) { new Counter(0, 1, 33).start(); new Counter(0, -1, 100).start(); }

11 Example (Cont.) Sample output: … Q: Run the program several times. Are the outputs the same? Q: What will happen if we change the statement “new Counter().start()” to “new Counter().run()” in the main method?

12 Exercise Write a thread class named Talker that prints a hello message, say “Hello, I am Tom!”, continuously and creates two instances of Talker as separate threads. public class Talker _______________________ { private String name; public Talker(String name) { this.name = name; } }

13 Exercise Work in a group of two or three to write a utility method named mul. double[][] mul(double[][] a, double[][] b, int sizeOfSub) It computes the product of two matrices a and b concurrently, by partitioning the result matrix into a number of submatrices of size sizeOfSub * sizeOfSub and using a separate thread to compute each submatrix. Assume that arguments are rectangular or multiplication compatible; a and b are multiplication compatible if a is of size l*n and b is of size n*m. Use Thread.join() method to wait for the completion of another thread. Write a sequential version of mul and compare the time efficiency of the two; use System.currentTimeMillis().

14 Controlling Threads Life cycle of threads  New After creation but before start() call  Alive After the start() call; run() is implicitly called. Consists of two sub states  Runnable: Ready to run; maybe running or waiting for their turns  Non-runnable: Not ready to run; waiting for some events (e.g., sleeping, blocked, or waiting)  Dead When run() method returns start Non-runnable New Dead Runnable yield sleep join end of run Alive

15 Methods of Thread Class Method Description start()Enter the Runnable state from the new state *sleep()Enter the Non-runnable (or blocked) state join()Enter the Non-runnable state and wait for another thread to terminate *yield()Release CPU and remain in the Runnable interrupt()Cause InterruptedException isAlive()True if in the alive state isInterrupted()True if interrupted * static method

16 Example Coordinating concurrent jobs public class Job extends Thread { public void run() { // do some meaningful work, e.g., writing multithreaded applets. } public static void main(String[] args) { Job[] jobs = new Job[10]; for (int i = 0; i < jobs.length; i++) { jobs[i] = new Job(); jobs[i].start(); } for (Job job: jobs) { job.join(); } jobs[0] … jobs[9] main

17 Exercise Explain the execution flow of the following program. public class Job extends Thread { public void run() { // do some meaningful work, e.g., writing multithreaded applets. } public static void main(String[] args) { Job[] jobs = new Job[10]; for (int i = 0; i < jobs.length; i++) { jobs[i] = new Job(); jobs[i].start(); jobs[i].join(); }

18 Thread Safety Example --- bank accounts public class Account { private long balance; public boolean withdraw(long amount) { if (amount <= balance) { long newBalance = balance – amount; balance = newBalance; return true; } else { return false; } // other methods and fields } What might happen in the presence of multiple threads?

19 Example (Cont.) One possible scenario Assume an account, say acc, with balance $1,000,000. new Thread(() -> acc.withdraw( )).start(); time balance withdraw 1 withdraw 2 t1 1,000,000 1: amount<=balance t2 1,000,000 1: amount<=balance t3 1,000,000 2: newBalance=...; t4 1,000,000 2: newBalance=...; t5 0 3: balance=...; t6 0 3: balance=...; t7 0 4: return true; t8 0 4: return true; 1 if (amount <= balance) { 2 long newBalance = balance – amount; 3 balance = newBalance; 4 return true; }

20 Atomicity of Operations The problem  Several calls of the withdraw method may be interleaved interfering with each others. Atomic operation  An operation that can’t be interrupted, e.g., reading and assignment to (word-sized) variables.  Non-atomic operations should be explicitly synchronized to ensure atomicity.

21 Making methods atomic Synchronized methods and statements  A lock is associated with each object. // synchronized method synchronized void aMethod() { // do something } // synchronized statement synchronized (expr) { // expr should be of a reference type! // do somthing }

22 Example public class Account { private long balance; public synchronized boolean withdraw(long amount) { if (amount <= balance) { long newBalance = balance – amount; balance = newBalance; return true; } else { return false; } // other methods and fields }

23 Exercise Make withdraw method synchronized by using the synchronized statement. public class Account { private long balance; public boolean withdraw(long amount) { // WRITE YOUR CODE HERE… if (amount <= balance) { long newBalance = balance – amount; balance = newBalance; return true; } else { return false; }

24 Synchronization  Mechanism to controls the order in which threads execute  Competition vs. cooperative synchronization Competition---Mutual exclusion of threads  Each synchronized method or statement is guarded by an object.  When entering a synchronized method or statement, the object will be locked until the method is finished.  When the object is locked by another thread, the current thread must wait.

25 Example --- Bounded Queue A first-in, first-out queue with a fixed capacity public class BoundedQueue { final protected int size; // capacity of the queue protected Object rep[]; // circular array protected int front = 0; // front of the queue protected int back = -1; // back of the queue protected int count = 0; // number of objects in the queue public BoundedQueue(int size) { this.size = size; rep = new Object[size]; back = size – 1; } public boolean isEmpty() { return count == 0; }

26 Bounded Queue (Cont.) public boolean isFull() { return count == size; } public int count() { return count; } public void put(Object e) { if (!isFull()) { back = (back + 1) % size; rep[back] = e; count++; } public Object get() { Object result = null; if (!isEmpty()) { result = rep[front]; front = (front + 1) % size; count--; } return result; }

27 Making Queue Thread-Safe public class SyncBoundedQueue extends BoundedQueue { public SyncBoundedQueue(int size) { super(size); } public synchronized boolean isEmpty() { return super.isEmpty(); } public synchronized boolean isFull() { return super.isFull(); } public synchronized int count() { return super.count(); } public synchronized void put(Object e) { super.put(e); } public synchronized Object get() { return super.get(); } } Thread-safe but maybe less efficient!

28 Use of Bounded Queue Typical use in producers and consumers ThreadProducerConsumerSyncBoundedQueue

29 Producer public class Producer extends Thread { private BoundedQueue queue; private int n; // number of items to produce public Producer(BoundedQueue queue, int n) { this.queue = queue; this.n = n; } public void run() { for (int i = 0; i < n; i++) { queue.put(i); System.out.println(“produced: “ + i); try { sleep((int) (Math.random() * 100)); } catch (InterruptedException e) {} }

30 Consumer public class Consumer Thread { private BoundedQueue queue; private int n; // number of stolen items to sell public Consumer(BoundedQueue queue, int n) { this.queue = queue; this.n = n; } public void run() { for (int i = 0; i < n; i++) { Object obj = queue.get(); System.out.println(“\tconsumed: “ + obj); try { sleep((int) (Math.random() * 400)); } catch (InterruptedException e) {} }

31 Sample Main Program The main method of class SyncBoundedQueue public static void main(String[] args) { SyncBoundedQueue queue = new SyncBoundedQueue(5); new Producer(queue, 15).start(); // produce 15 items new Consumer(queue, 10).start(); // consume 10 items } Output Some of the items might be lost, as the producer produces items faster than the consumer consumes them.

32 Why Items Get Lost? public void put(Object e) { if (!isFull()) { back = (back + 1) % size; rep[back] = e; count++; } public Object get() { Object result = null; if (!isEmpty()) { result = rep[front]; front = (front + 1) % size; count--; } return result; }

33 Cooperation among Threads Guarded suspension  Before a method is executed, the guard is tested. A guard is the precondition for an action to complete successfully.  Execution continues only when the guard is true, ensuring the successful completion of the method invocation.  Execution is temporarily suspended until the guard becomes true, at which time execution may continue. Thread controlling methods  wait(), notify(), and notifyAll() defined in the class Object  Can be used to implement guarded suspension

34 Thread Methods of Class Object Method Description wait() Temporarily blocked and placed in the wait queue of the receiving object. notify() Awaken one of the threads in the wait queue associated with the receiving object and remove it from the queue. notifyAll() Same as notify() except that all threads in the wait queue are awakened. ObjectQueue obj.wait(): add current thread to obj’s wait queue obj.notify(): awake a thread from obj’s wait queue wait

35 Bounded Queue with Guarded Suspension public class BoundedQueueWithGuard extends BoundedQueue { public Producer(int size) { super(size); } public synchronized boolean isEmpty() { return super.isEmpty(); } public synchronized boolean isFull() { return super.isFull(); } public synchronized int getCount() { return super.getCount(); } > }

36 Put/Get Methods public synchronized void put(Object obj) { try { while (isFull()) { // suspend till queue becomes not full wait(); // wait for notification } } catch (InterruptedException e) {} super.put(e); notify(); } public synchronized void get(Object obj) { try { while (isEmpty()) { // suspend till queue becomes not full wait(); // wait for notification } } catch (InterruptedException e) {} super.get(e); notify(); }

37 Smart Producer and Consumer public static void main(String args[]) { BoundedQueueWithGuard queue = new BoundedQueueWithGuard(5); new Producer(queue, 15).start(); // produce 15 items. new Consumer(queue, 15).start(); // consume 15 times } Output: No lost items!

38 Exercise Implement a thread-safe, bounded Stack class with guarded suspension. Define operations such as pop, push, isFull, and isEmpty. Test your code by using classes similar to Producer/Consumer classes. Extend your JavaChat application so that when a user sends an RSVP message the user is prohibited from sending a further message until at least one response is received.  RSVP message: a message starting with “rsvp:”  Response: a message starting with “ack:”  Improve to use a separate thread to send messages; use BoundedQueueWithGuard.