EDAF65 Processes and Threads

Similar documents
EDA095 Processes and Threads

EDA095 Processes and Threads

Time-Sharing Operating Systems. EDA095 Processes and Threads. Process Creation. The Content of a Process

Advanced Programming Methods. Lecture 6 - Concurrency in Java (1)

Techniques of Java Programming: Concurrent Programming in Java

Object Oriented Programming. Week 10 Part 1 Threads

Concurrent Programming using Threads

Unit - IV Multi-Threading

Basics of. Multithreading in Java

7. MULTITHREDED PROGRAMMING

Java Threads. Introduction to Java Threads

CMSC 132: Object-Oriented Programming II. Threads in Java

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Contents. G53SRP: Java Threads. Definition. Why we need it. A Simple Embedded System. Why we need it. Java Threads 24/09/2009 G53SRP 1 ADC

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Software Practice 1 - Multithreading

CS 556 Distributed Systems

Synchronized Methods of Old Versions of Java

Performance Throughput Utilization of system resources

Threads Questions Important Questions

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Threads Chate Patanothai

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

Thread Programming. Comp-303 : Programming Techniques Lecture 11. Alexandre Denault Computer Science McGill University Winter 2004

Programmazione Avanzata e Paradigmi Ingegneria e Scienze Informatiche - UNIBO a.a 2013/2014 Lecturer: Alessandro Ricci

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

CS455: Introduction to Distributed Systems [Spring 2019] Dept. Of Computer Science, Colorado State University

Object Oriented Programming (II-Year CSE II-Sem-R09)

Module - 4 Multi-Threaded Programming

Introduction to Java Threads

Java Threads. Written by John Bell for CS 342, Spring 2018

Multithread Computing

Multi-threading in Java. Jeff HUANG


Unit III Rupali Sherekar 2017

CISC 4700 L01 Network & Client-Server Programming Spring Cowell Chapter 15: Writing Threaded Applications

Chapter 32 Multithreading and Parallel Programming

Multithreaded Programming Part II. CSE 219 Stony Brook University, Department of Computer Science

CIS233J Java Programming II. Threads

Quiz on Tuesday April 13. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 4. Java facts and questions. Things to try in Java

Threads. Definitions. Process Creation. Process. Thread Example. Thread. From Volume II

Concurrent Programming

Parallel Programming Languages COMP360

What is a thread anyway?

CS 351 Design of Large Programs Threads and Concurrency

Overview. Processes vs. Threads. Computation Abstractions. CMSC 433, Fall Michael Hicks 1

Java Threads. COMP 585 Noteset #2 1

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Java s Implementation of Concurrency, and how to use it in our applications.

UNIT V CONCURRENT PROGRAMMING

Programming in Parallel COMP755

CMSC 330: Organization of Programming Languages

Programming Language Concepts: Lecture 11

Parallel Programming Practice

CST242 Concurrency Page 1

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

Contents. 6-1 Copyright (c) N. Afshartous

UNIT IV MULTITHREADING AND GENERIC PROGRAMMING

Advanced Concepts of Programming

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Concurrency, Thread. Dongkun Shin, SKKU

Computation Abstractions. CMSC 330: Organization of Programming Languages. So, What Is a Thread? Processes vs. Threads. A computer.

Concurrent Programming. Implementation Alternatives. Content. Real-Time Systems, Lecture 2. Historical Implementation Alternatives.

Concurrent Programming

Thread Programming 1

Only one thread can own a specific monitor

Parallel Programming Practice

Unit 4. Thread class & Runnable Interface. Inter Thread Communication

Reintroduction to Concurrency

Network Programming COSC 1176/1179. Lecture 6 Concurrent Programming (2)

Interprocess Communication By: Kaushik Vaghani

Class BinarySemaphore

Multithreaded Programming

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17

Threads, Concurrency, and Parallelism

UNIT:2. Process Management

Multithreaded Programming

User Space Multithreading. Computer Science, University of Warwick

Writing Parallel Programs COMP360

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

Multitasking. Multitasking allows several activities to occur concurrently on the computer Levels of multitasking: Process based multitasking

Concurrency - Topics. Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads

Multi-Threaded Concurrency in Java

Virtual Machine Design

Dealing with Issues for Interprocess Communication

public class Shared0 { private static int x = 0, y = 0;

CMSC 330: Organization of Programming Languages

THREADS AND CONCURRENCY

JAVA - MULTITHREADING

Faculty of Computers & Information Computer Science Department

Lecture 2 Process Management

Concurrency: a crash course

Resource management. Real-Time Systems. Resource management. Resource management

Figure 6.1 System layers

Subject: Operating System (BTCOC403) Class: S.Y.B.Tech. (Computer Engineering)

Threads. Threads The Thread Model (1) CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5

Processes The Process Model. Chapter 2. Processes and Threads. Process Termination. Process Creation

Transcription:

EDAF65 Processes and Threads Per Andersson Lund University http://cs.lth.se/per_andersson/ January 24, 2018 Per Andersson EDAF65 Processes and Threads January 24, 2018 1 / 65

Time-Sharing Operating Systems Processes are programs in execution Most operating systems can run multiple processes in parallel OSes allocate a small quantum of CPU time to each process P2 P1 Kernel t1 t2 t3 t4 T Task switching is very fast and gives the impression of simultaneous processing Per Andersson EDAF65 Processes and Threads January 24, 2018 2 / 65

Process Creation On Unix, a command interpreter a shell launches the user processes. A process can then launch other processes The creating process is the parent and the created processes are the children P 1 P 2 P 3 P 4 P 5 P 6 An initial process started at boot time is the ancestor of all the processes: system processes and user processes Per Andersson EDAF65 Processes and Threads January 24, 2018 3 / 65

The Content of a Process Running processes are located in the computer memory. They contain the program code resulting from the compiling A data area that stores the dynamic data is allocated by the program at run-time using new When the program calls functions or methods, a stack stores their parameters Execution Stack Data Code Per Andersson EDAF65 Processes and Threads January 24, 2018 4 / 65

The Process States The model of process execution is a finite-state machine Processes waiting for the CPU are ready The scheduler elects one process and runs it. On an I/O, the elected process is moved to a blocked state until the I/O is completed. Elected Ready Blocked Per Andersson EDAF65 Processes and Threads January 24, 2018 5 / 65

The Unix System The Unix states are slightly more complex Running User mode Defunct Exit System call Preempted Interrupt Running Kernel mode Blocked in memory Ready in memory Created Blocked not in memory Ready not in memory Per Andersson EDAF65 Processes and Threads January 24, 2018 6 / 65

The Scheduler The scheduler selects one process from the queue of ready processes. A scheduler runs every 10 ms or so and chooses a new process Waiting Waiting Waiting Running There are many scheduling algorithms available. Scheduling must be very fast and use relatively simple algorithms: first-come, first-served, round robin, priority. Per Andersson EDAF65 Processes and Threads January 24, 2018 7 / 65

The Process Control Block The operating system uses additional data to run a process: the process context or process control block. It contains the process state, program counter, CPU registers, etc. P2 t2 P1 t4 Kernel t3 t1 When switching tasks, the OS saves the current context and restores the context of the process that it will execute. Per Andersson EDAF65 Processes and Threads January 24, 2018 8 / 65

The Operating System Operations on Processes The operating system creates, schedules, and terminates the processes. Processes must sometimes cooperate and share data. The operating system offers communication means between processes, interprocess communications (IPC) and naming facilities: Pipes Shared memory Messages The operating system also offers means to coordinate and synchronize processes: semaphores on Unix Per Andersson EDAF65 Processes and Threads January 24, 2018 9 / 65

Threads Traditional processes are sequential: They have one execution path one thread of control Concurrent processes have multiple threads of control i.e. processes within a process. Imagine a word processor application. The process must read the keystrokes, display the text, check spelling, and so on, at the same time Difficult to manage with a single thread The idea is to allocate one thread to each task. Per Andersson EDAF65 Processes and Threads January 24, 2018 10 / 65

Benefits Easier to implement parallelism within the application Input/output does not block the process, only one thread More responsive programs using high-priority threads to manage user interaction. Threads are more economical: creating a process is 10 to 100 longer than creating a thread. But: Threads are more difficult to coordinate and prone to nasty bugs. Per Andersson EDAF65 Processes and Threads January 24, 2018 11 / 65

Java Threads Threads are a feature of Java: a Java program and virtual machine can run multiple threads. Java provides constructs to create, manage, synchronize, and terminate threads through: the Thread class http://docs.oracle.com/javase/8/docs/api/java/lang/ Thread.html or the Runnable interface http://docs.oracle.com/javase/8/docs/api/java/lang/ Runnable.html. Threads can communicate using shared objects, pipes, or through messages (sockets) Per Andersson EDAF65 Processes and Threads January 24, 2018 12 / 65

Java Threads Creating Threads: The Thread class Threads can be created using the Thread class in two steps. 1 Create a new class: Derive a new class from Thread using extends Override the run() method 2 Create and run a thread object: Create a thread object using new. Start it using the start() method Per Andersson EDAF65 Processes and Threads January 24, 2018 13 / 65

A First Program using the Thread Class public class MyThread extends Thread { MyThread() { public void run() { System.out.println("My first thread"); public static void main(string args[]) { MyThread firstthread = new MyThread(); firstthread.start(); Per Andersson EDAF65 Processes and Threads January 24, 2018 14 / 65

Creating Threads: The Runnable Interface The Runnable interface is another option to create threads. 1 Add the Runnable properties to a class: Implement the Runnable interface using implements Add a run() method 2 Create and run a thread object: Create a Runnable object using new. Create a thread that takes the runnable object as an argument Start the thread using the start() method Per Andersson EDAF65 Processes and Threads January 24, 2018 15 / 65

A First Program using the Runnable Interface public class MyRunnable implements Runnable { MyRunnable() { public void run() { System.out.println("My second thread"); public static void main(string args[]) { MyRunnable firstrunnable = new MyRunnable(); Thread mythread = new Thread(firstRunnable); mythread.start(); Per Andersson EDAF65 Processes and Threads January 24, 2018 16 / 65

Passing Data to a Thread You can pass data at creation time. Thread(String name) is a useful constructor. public class MyThread2 extends Thread { private int myint; MyThread2(String name, int myint) { super(name); this.myint = myint; public void run() { for (int i = 0; i < myint; i++) { System.out.println(getName() + ": " + i); System.out.println(getName() + " terminated"); Per Andersson EDAF65 Processes and Threads January 24, 2018 17 / 65

Passing Data to a Thread, Continued public static void main(string args[]) { MyThread2 secondthread = new MyThread2("Thread#2", 123); secondthread.start(); Per Andersson EDAF65 Processes and Threads January 24, 2018 18 / 65

Returning Data from a Thread Does this work? class MyThread { result; MyThread() { getresult() { return result; run() { result = produceresult(); class Main { thread.start(); thread.getresult(); Per Andersson EDAF65 Processes and Threads January 24, 2018 19 / 65

Returning Data from a Thread class MyThread { result; MyThread() { getresult() { return result; run() { result = produceresult(); Main.callback(result); (Launcher2.java and Thread2.java) class Main { static callback(result) { useresult(result); main() { thread.start(); Per Andersson EDAF65 Processes and Threads January 24, 2018 20 / 65

Working with Multiple Threads The Java Virtual Machine manages the scheduling. public static void main(string args[]) { int loopcount; loopcount = Integer.parseInt(args[0]); MyThread3 thirdthread = new MyThread3("Thread3", loopcount); MyThread3 fourththread = new MyThread3("Thread4", loopcount); thirdthread.start(); fourththread.start(); Per Andersson EDAF65 Processes and Threads January 24, 2018 21 / 65

The Thread API The thread API consists of 8 constructors and 40 methods, some of them deprecated. Constructors Thread() Thread(Runnable target) Thread(Runnable target, String name) Thread(String name) Thread(ThreadGroup group, Runnable target) Thread(ThreadGroup group, Runnable target, String name) Thread(ThreadGroup group, Runnable target, String name, long stacksize) Thread(ThreadGroup group, String name) Per Andersson EDAF65 Processes and Threads January 24, 2018 22 / 65

Thread Methods static int activecount() void checkaccess() static Thread currentthread() static void dumpstack() static int enumerate(thread[] tarray) ClassLoader getcontextclassloader() String getname() int getpriority() ThreadGroup getthreadgroup() static boolean holdslock(object obj) void interrupt() static boolean interrupted() boolean isalive() boolean isdaemon() boolean isinterrupted() void join() void join(long millis) void join(long millis, int nanos) void run() void setcontextclassloader(classloader cl) void setdaemon(boolean on) void setname(string name) void setpriority(int newpriority) static void sleep(long millis) static void sleep(long millis, int nanos) void start() String tostring() static void yield() public long getid() public Thread.State getstate() Per Andersson EDAF65 Processes and Threads January 24, 2018 23 / 65

Deprecated Methods int countstackframes() void stop() void resume() void stop(throwable obj) void destroy() void suspend() A frequent question is: Why can t I stop a thread? Read: http://docs.oracle.com/javase/8/docs/technotes/guides/ concurrency/threadprimitivedeprecation.html Per Andersson EDAF65 Processes and Threads January 24, 2018 24 / 65

The Thread States From Java 1.5.0: Thread.State enables to know the state. http://docs.oracle.com/javase/8/docs/api/java/lang/thread. State.html Per Andersson EDAF65 Processes and Threads January 24, 2018 25 / 65

Java Threads Scheduling The JVM schedules threads using priorities. Priorities are adjustable and range from 1 to 10: Thread.MAX PRIORITY = 10 Thread.NORM PRIORITY = 5 Thread.MIN PRIORITY = 1 The Java specifications do not describe the scheduling algorithm. They are left to the implementer. Per Andersson EDAF65 Processes and Threads January 24, 2018 26 / 65

Scheduling Algorithms Scheduling can be preemptive or cooperative: A cooperative scheduler selects the highest priority thread and runs it until it is completed unless the thread carries out an I/O or yields control using yield() A preemptive scheduler allocates time quanta to threads so that they all can run. High priority tasks should have more time than lower priority ones. Be aware of the implementation differences that are not documented. On older Java implementations, a thread cannot be taken away from the processor if it does not complete an I/O operation Per Andersson EDAF65 Processes and Threads January 24, 2018 27 / 65

Adjusting Priorities public static void main(string args[]) { int firstpriority, secondpriority; int loopcount; firstpriority = Integer.parseInt(args[0]); secondpriority = Integer.parseInt(args[1]); loopcount = Integer.parseInt(args[2]); MyThread4 fifththread = new MyThread4("Thread#5", loopcount); MyThread4 sixththread = new MyThread4("Thread#6", loopcount); fifththread.setpriority(firstpriority); sixththread.setpriority(secondpriority); fifththread.start(); sixththread.start(); Per Andersson EDAF65 Processes and Threads January 24, 2018 28 / 65

Thread Implementation The Java Virtual Machines do not implement threads the same way. Result of a program execution depends on the Java version, implementation, and OS variant. Compare the execution of: $ java Launcher5 1 10 1000000 on your Linux machines and on a Mac Per Andersson EDAF65 Processes and Threads January 24, 2018 29 / 65

Sharing the Work Between Multiple Threads Task pipeline Multiple tasks Task Tasks The task is split into subtasks and assigned to threads organized as a pipeline. The tasks are marshaled and assigned to a pool of threads: Java Executors. Per Andersson EDAF65 Processes and Threads January 24, 2018 30 / 65

A Client-Server Organization Dispatcher Server Control threads Client requests Per Andersson EDAF65 Processes and Threads January 24, 2018 31 / 65

Java Executors Executors are a very simple and handy way to manage threads They are part of the concurrent package. To create a thread pool for a RunnableClass class implementing the Runnable interface, the launcher: creates the pool with: ExecutorService service = Executors.newFixedThreadPool(2); creates the Runnable tasks: Runnable task = new RunnableClass() submits the tasks using service.submit(task) and shuts down the pool using service.shutdown(). (MyExecutors.java) Per Andersson EDAF65 Processes and Threads January 24, 2018 32 / 65

Java Executors with Future If the runnable object is to return a value, you have to modify: The task: Instead of implementing Runnable, the class implements Callable<ReturnValue> The running code: Instead of run(), you have a call() method that returns a result. The launcher program creates a pool of threads: ExecutorService service = Executors.newFixedThreadPool(2); It submits the tasks using submit(), receives the results in the form of a Future, and shuts down the pool using shutdown(). (MultithreadedMaxFinder.java) Per Andersson EDAF65 Processes and Threads January 24, 2018 33 / 65

Thread Coordination: A Simple Problem Let us suppose that we want to reverse a list of numbers (countdown). A simple and inefficient algorithm would put all the items on a stack and once it is finished, remove them from the stack. 3 2 3 2 1 1 1 2 3 In the real world, we would divide this task into two subtasks: pile the boxes and then take them from the stack. Let us try to implement it with two threads. Per Andersson EDAF65 Processes and Threads January 24, 2018 34 / 65

The Stack Class Stacks are a very common LIFO data structure. Java has a built-in Stack class. Stack has two main methods: Object push(object item) puts one item onto the top of the stack and Object pop() removes one item at the top of the stack and returns it. The empty() method is a Boolean to test the stack state. Per Andersson EDAF65 Processes and Threads January 24, 2018 35 / 65

Wrapper Types The Stack class as List, Vector, and other collections manipulates Objects It cannot store primitive types like boolean, int, float, or double that are not objects To store an integer variable, the program must associate it to an object a wrapper Each primitive type has an object counterpart: char and Char, int and Integer, etc. From Java 1.5, moving an int to an Integer and the reverse are automatic and are called boxing and autoboxing. Per Andersson EDAF65 Processes and Threads January 24, 2018 36 / 65

A Class to Create and Read a Stack class MyStack extends Stack<Integer> { int stacksize; MyStack(int stacksize) { this.stacksize = stacksize; void buildstack() { for (int count = 0; count < stacksize; count++) { this.push(count); System.out.println("Stack complete"); void printstack() { while (!this.empty()) { System.out.println(this.pop()); System.out.println("Stack printed"); Per Andersson EDAF65 Processes and Threads January 24, 2018 37 / 65

A Single Threaded Program public class Launcher9 { public static void main(string args[]) { int loopcount = 0; loopcount = Integer.parseInt(args[0]); MyStack mystack = new MyStack(loopCount); mystack.buildstack(); mystack.printstack(); Per Andersson EDAF65 Processes and Threads January 24, 2018 38 / 65

A Multi-Threaded Program Sharing a Stack Now let us create two threads to share the work: BuildingThread buildingthread = new BuildingThread(myStack); PrintingThread printingthread = new PrintingThread(myStack); buildingthread.start(); printingthread.start(); class BuildingThread extends Thread { MyStack mystack; BuildingThread(MyStack mystack) { this.mystack = mystack; public void run() { mystack.buildstack(); Per Andersson EDAF65 Processes and Threads January 24, 2018 39 / 65

A Multi-Threaded Program (Continued) class PrintingThread extends Thread { MyStack mystack; PrintingThread(MyStack mystack) { this.mystack = mystack; public void run() { mystack.printstack(); (Launcher10.java) Better design? What do you think? Per Andersson EDAF65 Processes and Threads January 24, 2018 40 / 65

The Execution Flow The scheduler shares the time between the two threads Printing Thread Building Thread Main Stack complete T The lack of coordination produces a garbled output Per Andersson EDAF65 Processes and Threads January 24, 2018 41 / 65

What Should the Execution be The scheduler must run the printstack() method after the buildstack() method is complete printstack() buildstack() Main Stack complete T The code sections where the stack is being accessed built and read are critical sections Their access must be exclusive: one thread at a time Per Andersson EDAF65 Processes and Threads January 24, 2018 42 / 65

Busy Waiting A first solution is to test continuously a condition before entering the critical section The condition is set when the task is complete class BuildingThread extends Thread { MyStack mystack; volatile boolean complete = false; BuildingThread(MyStack mystack) { this.mystack = mystack; public void run() { mystack.buildstack(); complete = true; boolean getstatus() { return complete; Per Andersson EDAF65 Processes and Threads January 24, 2018 43 / 65

Busy Waiting (Continued) The condition is tested before starting the 2 nd thread (Launcher11.java) It is called busy waiting while (buildingthread.getstatus() == false) { ; Busy waiting requires an atomic access to the condition variable This is implemented using the volatile keyword Busy waiting is generally not a good solution An improved program would test the condition in the 2 nd thread and use yield() if it is not met yield() moves the executing thread to runnable and allows the scheduler to select and run another thread It is a poor design too. Per Andersson EDAF65 Processes and Threads January 24, 2018 44 / 65

Monitors Monitors are constructs that guarantee the mutual exclusion of methods Per Brinch Hansen developed this concept of monitor in 1973 Any Java object is a potential monitor The synchronized keyword declares the object methods that are part of a monitor class MyClass { synchronized void m1() { void m2() { synchronized void m3() { Per Andersson EDAF65 Processes and Threads January 24, 2018 45 / 65

Monitors (Continued) The methods m1() and m3() are part of the monitor: myobject.m1() and myobject.m3() won t run concurrently The first method started must be finished before another one is started Similarly, two threads can t run myobject.m1() concurrently The method myobject.m2() is not part of the monitor. It can be run at any time The set of threads competing to acquire a monitor is called the entry set The Boolean method holdslock(object) returns true if the thread holds the monitor lock Per Andersson EDAF65 Processes and Threads January 24, 2018 46 / 65

The Entry Set The entry set Thread The monitor Thread Owning Thread Thread Per Andersson EDAF65 Processes and Threads January 24, 2018 47 / 65

The New Class class MyStack extends Stack { int stacksize; MyStack(int stacksize) { this.stacksize = stacksize; synchronized void buildstack() { for (int count = 0; count < stacksize; count++) { this.push(count); System.out.println("Stack complete"); synchronized void printstack() { while (!this.empty()) { System.out.println(this.pop()); System.out.println("Stack printed"); //Launcher12.java Per Andersson EDAF65 Processes and Threads January 24, 2018 48 / 65

Race Conditions What happens if threads are started the other way around? printingthread.start(); buildingthread.start(); // Launcher13.java instead of buildingthread.start(); printingthread.start(); // Launcher12.java The result depends on the particular order of the instructions This is called a race condition Can we improve the monitor to avoid it? Per Andersson EDAF65 Processes and Threads January 24, 2018 49 / 65

Introducing the wait() Method When a thread runs a synchronized method, it owns the object exclusively. The others are in the blocked state. Sometimes the object is not ready as when the stack is empty. The thread is unable to start or continue. It must wait then... The wait() method moves the thread from running to the waiting state and places it in a waiting list the wait set. All objects inherits the wait() method as potential monitors (this.wait()) Per Andersson EDAF65 Processes and Threads January 24, 2018 50 / 65

The Wait Set The entry set The wait set Thread Thread The monitor Owning Thread wait() Thread Thread Thread Per Andersson EDAF65 Processes and Threads January 24, 2018 51 / 65

The wait()method (Continued) The new code is: synchronized void printstack() { while (this.empty()) { // do not use if! try { wait(); catch (InterruptedException e) { while (!this.empty()) { System.out.println(this.pop()); System.out.println("Stack printed"); // Launcher14.java The stack is not printed! Why? Per Andersson EDAF65 Processes and Threads January 24, 2018 52 / 65

The notify() Method After a wait() call, the thread is stuck in the wait set The notify() method selects arbitrarily one thread from the wait set and moves it to the entry set and the runnable state The notifyall() method moves all the threads in the wait set to the entry set and to the runnable state synchronized void buildstack() { for (int count = 0; count < stacksize; count++) { this.push(count); System.out.println("Stack complete"); notifyall(); // Launcher15.java Per Andersson EDAF65 Processes and Threads January 24, 2018 53 / 65

The notifyall() Method Per Andersson EDAF65 Processes and Threads January 24, 2018 54 / 65

Exiting the Wait Set A thread exits the wait set when it is notified It is also possible to set a time limit to wait() using public final void wait (long milliseconds) or public final void wait (long milliseconds, int nanos) The nanos value is not reliable however This moves the thread in the timed waiting state, similar to waiting. Finally, the interrupt() method of the Thread class enables a thread to exit the wait set Per Andersson EDAF65 Processes and Threads January 24, 2018 55 / 65

The interrupt() Method Under normal running conditions, interrupt() sets the interrupt status and has not other effects When the thread is in the waiting state because of wait(), sleep(), or join(), it receives an InterruptedException Input/output blocks a running thread until the I/O is completed. With the nio package, interrupt() wakes up a thread in an I/O method. The Boolean method isinterrupted()returns the status value and interrupted() returns and clears it printingthread.start(); printingthread.interrupt(); buildingthread.start(); //(Launcher16.java) Per Andersson EDAF65 Processes and Threads January 24, 2018 56 / 65

Deadlocks Threads programming is difficult to master well Deadlocks are a major source of bugs A deadlock occurs when these conditions are met: 1 A thread has an exclusive resource that another thread is waiting for and 2 The other thread has a resource that the first thread is waiting for It is a hopeless circular wait Per Andersson EDAF65 Processes and Threads January 24, 2018 57 / 65

Deadlocks (Continued) Thread 1 Thread 2 Allocated Request R 1 R 2 Per Andersson EDAF65 Processes and Threads January 24, 2018 58 / 65

A Deadlock Example In addition to methods, blocks of code can be synchronized as: synchronized (Object) {... Objects can wait and notify using Object.wait() and Object.notify() Let us program a deadlock: A first thread acquires two synchronized objects, lock1 and lock2 and a second thread acquires the same objects the other way around Per Andersson EDAF65 Processes and Threads January 24, 2018 59 / 65

A Deadlock Example class Stuck1 extends Thread { Integer lock1, lock2; Stuck1(String name, Integer lock1, Integer lock2) { super(name); this.lock1 = lock1; this.lock2 = lock2; public void run() { synchronized (lock1) { System.out.println(getName() + " acquired lock1"); synchronized (lock2) { System.out.println(getName() + " acquired lock2"); Per Andersson EDAF65 Processes and Threads January 24, 2018 60 / 65

A Deadlock Example class Stuck2 extends Thread { Integer lock1, lock2; Stuck2(String name, Integer lock1, Integer lock2) { super(name); this.lock1 = lock1; this.lock2 = lock2; public void run() { synchronized (lock2) { System.out.println(getName() + " acquired lock2"); synchronized (lock1) { System.out.println(getName() + " acquired lock1"); Per Andersson EDAF65 Processes and Threads January 24, 2018 61 / 65

A Deadlock Example public class Launcher17 { public static void main(string args[]) { Integer lock1 = new Integer(1), lock2 = new Integer(2); Stuck1 stuck1 = new Stuck1("Stuck1", lock1, lock2); Stuck2 stuck2 = new Stuck2("Stuck2", lock1, lock2); stuck1.start(); stuck2.start(); The deadlock is not systematic. It depends on the completion speed of stuck1 Per Andersson EDAF65 Processes and Threads January 24, 2018 62 / 65

Reentrance A single thread can t deadlock itself however because Java monitors are reentrant public class Reentrant { public synchronized void a() { b(); System.out.println("Running a()"); public synchronized void b() { System.out.println("Running b()"); // Launcher18.java Per Andersson EDAF65 Processes and Threads January 24, 2018 63 / 65

Thread Death A thread terminates when it returns from the run() method. Do not use stop() Instead of using synchronized methods, we could simply have waited the end the building thread. This is possible using join() that waits for a thread to finish. isalive() tests if a thread is alive. It returns false if it is dead or not started. (Launcher19.java) Per Andersson EDAF65 Processes and Threads January 24, 2018 64 / 65

Semaphores Semaphores are another type of coordination device They are widely used although more difficult than monitors They are available on Unix in the IPC library and from version 1.5.0 of Java in the java.util.concurrent package A semaphore is a positive integer that is decremented and incremented atomically using the P and V operations A mutex is a semaphore initialized to one It enables to protect a critical section as in mutex = 1 P(mutex) criticalsection() V(mutex) Per Andersson EDAF65 Processes and Threads January 24, 2018 65 / 65