Java threads. Carlo U. Nicola, IMVS With extracts from slides/publications of : Brian Goetz and Dominik Gruntz, IMVS

Similar documents
JAVA - MULTITHREADING

OBJECT ORIENTED PROGRAMMING LANGUAGE

Topics. Producing Production Quality Software. Concurrent Environments. Why Use Concurrency? Models of concurrency Concurrency in Java

Java Memory Model: Content

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, 2011 SOLUTIONS.

Multithreaded Programming

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Outline of this lecture G52CON: Concepts of Concurrency

CS11 Java. Fall Lecture 7

Java Virtual Machine Locks

Threads & Tasks: Executor Framework

Threads 1. When writing games you need to do more than one thing at once.

Tutorial: Getting Started

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Crash Course in Java

Built-in Concurrency Primitives in Java Programming Language. by Yourii Martiak and Mahir Atmis

Inheritance, overloading and overriding

The Java Virtual Machine and Mobile Devices. John Buford, Ph.D. Oct 2003 Presented to Gordon College CS 311

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (I)

Lecture 6: Semaphores and Monitors

Java Coding Practices for Improved Application Performance

Java Concurrency Framework. Sidartha Gracias

Chapter 1 Java Program Design and Development

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Parallel Programming

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

1.1. Over view Example 1: CPU Slot Distribution A example to test the distribution of cpu slot.

Chapter 8 Implementing FSP Models in Java

Java from a C perspective. Plan

Traditional Software Development. Model Requirements and JAVA Programs. Formal Verification & Validation. What is a state?

Design Pattern for the Adaptive Scheduling of Real-Time Tasks with Multiple Versions in RTSJ

J a v a Quiz (Unit 3, Test 0 Practice)

Monitors, Java, Threads and Processes

Operating Systems Concepts: Chapter 7: Scheduling Strategies

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

An Overview of Java. overview-1

ODROID Multithreading in Android

RTOS Debugger for ecos

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

The BSN Hardware and Software Platform: Enabling Easy Development of Body Sensor Network Applications

Real Time Programming: Concepts

Java Interview Questions and Answers

Process Scheduling CS 241. February 24, Copyright University of Illinois CS 241 Staff

public static void main(string[] args) { System.out.println("hello, world"); } }

2! Multimedia Programming with! Python and SDL

Performance Improvement In Java Application

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

A Survey of Parallel Processing in Linux

Java Basics: Data Types, Variables, and Loops

Network/Socket Programming in Java. Rajkumar Buyya

Introduction to Java

Building a Multi-Threaded Web Server

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

3. Java Nuggets in a Nutshell

Object-Oriented Programming in Java

Chapter 2: Processes, Threads, and Agents

Getting to know Apache Hadoop

The Java Virtual Machine (JVM) Pat Morin COMP 3002

STM32JAVA. Embedded Java Solutions for STM32

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Fachbereich Informatik und Elektrotechnik SunSPOT. Ubiquitous Computing. Ubiquitous Computing, Helmut Dispert

Stack vs. Heap. Introduction to Programming. How the Stack Works. Primitive vs. Reference Types. Stack

Thread Synchronization and the Java Monitor

Moving from CS 61A Scheme to CS 61B Java

Overview Motivating Examples Interleaving Model Semantics of Correctness Testing, Debugging, and Verification

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Java CPD (I) Frans Coenen Department of Computer Science

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Concurrent Programming

5.2 Q2 The control variable of a counter-controlled loop should be declared as: a.int. b.float. c.double. d.any of the above. ANS: a. int.

Chapter 6, The Operating System Machine Level

Concurrent programming in Java

A Thread Monitoring System for Multithreaded Java Programs

Lecture J - Exceptions

Habanero Extreme Scale Software Research Project

Chapter 6 Load Balancing

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

CS5233 Components Models and Engineering

First-class User Level Threads

Creating a Simple, Multithreaded Chat System with Java

Java SE 8 Programming

Multithreading and Java Native Interface (JNI)!

Distributed Embedded Systems

Project No. 2: Process Scheduling in Linux Submission due: April 28, 2014, 11:59pm

Transparent Redirection of Network Sockets 1

Threads Scheduling on Linux Operating Systems

Introduction to Java. CS 3: Computer Programming in Java

Mobile Application Development Android

D06 PROGRAMMING with JAVA

Operating Systems OBJECTIVES 7.1 DEFINITION. Chapter 7. Note:

TUTORIAL FOR INITIALIZING BLUETOOTH COMMUNICATION BETWEEN ANDROID AND ARDUINO

Socket-based Network Communication in J2SE and J2ME

Introduction to Programming

Object Oriented Software Design

How to develop your own app

Transcription:

Java threads Carlo U. Nicola, IMVS With extracts from slides/publications of : Brian Goetz and Dominik Gruntz, IMVS

Topics 1. Thread Definition 2. Thread Synchronization 3. Condition Variables 4. Volatile MAS HS12 2

What are threads? A process is an executing program that: is memory allocated by OS usually does not share memory with other processes A thread is a single sequential flow of control that: runs in the address space of a process has its own program counter has its own stack frame shares code and data with other threads. UML diagram: MAS HS12 3

java.lang.thread java.lang.thread = Infrastructure (PC, Stack) java.lang.runnable = Code Runnable run() public class R implements Runnable { private int nr; public R(int nr){this.nr = nr; public void run() {... for(int i=0; i<10; i++) { System.out.println( Hello + nr + + i); java.lang.thread Runnable r = new R(1); Thread t = new Thread(r); Thread R run() - run() has no parameters - run() returns no result - run() does not declare any checked exception MAS HS12 4

Starting the thread Constructor initializes the thread object: Thread t = new Thread(r); t.start(); calls the thread object s run method (! turn power on) when run( ) returns, the thread terminates public class Test { static void main(string[] args){ Thread t1 = new Thread(new R(1)); Thread t2 = new Thread(new R(2)); t1.start(); t2.start(); Hello1 0 Hello2 0 Hello1 1 Hello2 1 Hello2 2 Hello2 3 Hello2 4 Hello2 5 Hello2 6 Hello2 7 Hello2 8 Hello2 9 Hello1 2 Hello1 3 Hello1 4 Hello1 5 Hello1 6 Hello1 7 Hello1 8 Hello1 9 Process Exit.. MAS HS12 5

Scheduling Scheduling: Per processor, only one thread is running at any given time Scheduling = Allocation of CPU time to threads Threading models: Cooperative threading! pseudo parallelism # threads decide, when they should give up the processor to other threads. Example: yield(); sleep(1000); Pre-emptive threading! quasi parallelism # OS interrupts threads at any time (time sliced) # no thread can unfairly hog the processor JVM does not mandate a threading model Java programmers must write programs for both models! MAS HS12 6

Alternative definition of a Java thread Since the class Thread implements the interface Runnable, the method run() can be implemented in subclass of Thread. Runnable Example: run() public class T extends Thread { private int nr; public T(int nr) {this.nr = nr; public void run() { for (int i=0; i<10; i++) { System.out.println( Hello +nr + + i); Thread run() T run() MAS HS12 7

Extending Thread vs implementing Runnable Implement Runnable in a separate class: the existing class should provide code for the thread! no multiple subclassing problem you must implement run() Implementing Runnable in extension of Thread easy access to thread methods # static methods: sleep(100); Thread.sleep(100); # instance methods: getname(); Thread.currentThread().getName(); MAS HS12 8

Thread class static Thread currentthread(): returns a reference to the currently executing thread object. static void sleep(long millis): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. static void yield(): Causes the currently executing thread object to temporarily pause and allow other threads to execute. void run() void start() void join(): Waits for this thread to die. void join(long millis): Waits at most millis milliseconds for this thread to die. MAS HS12 9

Thread class join() public class Test1 { static void main(string[] args) { Thread t1 = new Thread(new R(1)); Thread t2 = new Thread(new R(2)); t1.start(); t2.start(); System.out.println("done"); What is the program s output? MAS HS12 10

Thread class join() public class Test1 { static void main(string[] args){ Thread t1 = new Thread(new R(1)); Thread t2 = new Thread(new R(2)); t1.start(); t2.start(); try { t1.join(); // waits until t1 has terminated t2.join(); // waits until t2 has terminated catch(interruptedexception e){ System.out.println("done"); What is the program s output? MAS HS12 11

Further methods of the Thread class String getname(): Returns this thread's name. void setname(string name) Changes the name of this thread to be equal to the argument name. int getpriority() Returns this thread's priority. void setpriority(int newpriority) Changes the priority of this thread. boolean isdaemon() Tests if this thread is a daemon thread. void setdaemon(boolean on) Marks this thread as either a daemon thread or a user thread. boolean isalive() Tests if this thread is alive. MAS HS12 12

Thread class: Deamons Daemon Threads daemon threads run in the background if only deamon threads are active, the process stops. public class Test1 { static void main(string[] args){ Thread t1 = new Thread(new R(1)); Thread t2 = new Thread(new R(2)); t2.setdaemon(true); t1.start(); t2.start(); What is the program s output? MAS HS12 13

Thread states MAS HS12 14

Example: unresponsive UI Button start = new Button("Start"); start.addactionlistener( new ActionListener() { public void actionperformed(actionevent e){ go(); ); public void go() { while(true){ // thread sleeps for 100ms try {Thread.sleep(100); catch (InterruptedException e){ t.settext("" + count++); MAS HS12 15

Solution: unresponsive UI Button start = new Button("Start"); start.addactionlistener( new ActionListener(){ public void actionperformed(actionevent e){ new Thread(UI.this).start(); ); public void run(){ while(true){ // thread sleeps for 100ms try {Thread.sleep(100); catch (InterruptedException e){ t.settext("" + count++); MAS HS12 16

Quiz class C { int x = 0, y = 0; void a() { x = 3; y = 4; int b() { int z = y; z = z + x; return z; One thread calls method a() on an instance of C One thread calls method b() on the same instance. Question: what results can be returned by b()? MAS HS12 17

Demo: Adder class TTest { static double x = 0.0; public static void add(double val){x = x + val; public static void main(string[] args){ class TestThread extends Thread { private double val; TestThread(double val){this.val = val; public void run(){ for(int i=0; i<1000; i++){ add(+val); add(-val); MAS HS12 18

Demo: Adder cont. TestThread t1 = new TestThread(120); TestThread t2 = new TestThread(60); TestThread t3 = new TestThread(20); TestThread t4 = new TestThread(55.5); t1.start(); t3.start(); t2.start(); t4.start(); try { t1.join(); t2.join(); t3.join(); t4.join(); catch(interruptedexception e){ System.out.println(x); MAS HS12 19

Threads and shared resources An important definition (Brian Goetz): A class is thread safe if it behaves always in the same manner (i.e. correctly) when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of these threads by the runtime environment and with no additional synchronization on the part of the calling code. Remember: Stateless objects (immutable classes) do not have fields and they do not reference fields from other classes therefore they are always thread safe. MAS HS12 20

Example: Yarn with increment class Counter { private final int count; public Counter() { count = 0; public void increment() { count++; public void decrement() { count--; public int getvalue() { return count; class IncThread extends Thread { private final Counter c; public IncThread(Counter p_c) { c = p_c; public void run() { while (true) { c.increment(); System.err.println ("Running inc thread: " + currentthread() + " / Value: " + c.getvalue()); MAS HS12 21

Example: Yarn with decrement class DecThread extends Thread { private final Counter c; public DecThread(Counter p_c) { c = p_c; public void run() { while (true) { c.decrement(); System.err.println("Running dec thread: " + currentthread() + " / Value: " + c.getvalue()); public class Yarn { public static void main(string args[]) { Counter c = new Counter(); IncThread ithread = new IncThread(c); DecThread dthread = new DecThread(c); ithread.start(); dthread.start(); MAS HS12 22

Output of Yarn Running inc thread: Thread[Thread-0,5,main] / Value: 1 Running inc thread: Thread[Thread-0,5,main] / Value: 2 Running inc thread: Thread[Thread-0,5,main] / Value: 3 Running inc thread: Thread[Thread-0,5,main] / Value: 4 Running inc thread: Thread[Thread-0,5,main] / Value: 61 Running inc thread: Thread[Thread-0,5,main] / Value: 62 Running dec thread: Thread[Thread-1,5,main] / Value: 61 Running inc thread: Thread[Thread-0,5,main] / Value: 62 Running dec thread: Thread[Thread-1,5,main] / Value: 61 Running inc thread: Thread[Thread-0,5,main] / Value: 62 Running dec thread: Thread[Thread-1,5,main] / Value: 61 Running dec thread: Thread[Thread-1,5,main] / Value: 60 Running dec thread: Thread[Thread-1,5,main] / Value: 1 Running dec thread: Thread[Thread-1,5,main] / Value: 0 Running dec thread: Thread[Thread-1,5,main] / Value: -1 Running inc thread: Thread[Thread-0,5,main] / Value: -1 Running inc thread: Thread[Thread-0,5,main] / Value: 0 Running inc thread: Thread[Thread-0,5,main] / Value: 1 Running inc thread: Thread[Thread-0,5,main] / Value: 2 Why such a mess? A shared resource (in our case the Counter c) is accessed without any order by several threads! Our small program is not thread safe. What can we do? (1) Do not share state variables across threads. (2) Make state variable immutable. (3) Use synchronization whenever you access state variables. MAS HS12 23

Any race conditions? Yes! class IncThread extends Thread { private final Counter c; public IncThread (Counter p_c) { c = p_c; public void run() { while (true) { c.increment(); System.err.println ("Running inc thread: " + currentthread() + " / Value: " + c.getvalue()); We have always thread unsafe applications when we use compound operations like: (1) Read-Modify-Write (RMW) (2) Check-Then-Act (CTA) that are not atomic! Atomic means that RMW and CTA are executed from start to finish by only one thread at a time. MAS HS12 24

How to synchronize? Synchronized blocks: Every object contains a single lock A lock is taken when synchronized section is entered If the lock is not available, thread enters a waiting queue If the lock is returned any (longest waiting?) thread is resumed public void run () { while (true) { synchronized (c) { c.increment (); System.err.println ("Running inc thread: " + currentthread () + " / Value: " + c.getvalue ()); MAS HS12 25

synchronized On instance methods: often a method is synchronized on this : public void push(int x){synchronized(this){ short form: public synchronized void push(int x){ On class methods (static) A lock is also associated with each class, this lock is different from the locks of the instances: public static synchronized void foo(){ similar to: class C { public static void foo(){synchronized(c.class){ MAS HS12 26

Monitors Data protection: A synchronized lock does not protect data but it only synchronizes threads Data can still be manipulated by direct access # declare data as private Data can still be accessed by unsynchronized threads # synchronize all methods which can access critical data sleep: sleep() does not release ownership of any lock Java does not provide monitors, but monitors can be implemented with Java MAS HS12 27

Example: Copy machine class CopyMachine { public synchronized void makecopies( Document d, int ncopies){ // only one thread at a time Original org = scanoriginal(d); for(int i=0; i<n; i++) { Paper p = getpaper(); copy(org, p); public void loadpaper (Paper[] p) { // multiple threads can access this putpaper(p) Problem: How can we intelligently synchronize the access to the paper drawer? MAS HS12 28

Fine-grained access One lock at the object level (this) may be too coarse: use dummy objects as simple locks class CopyMachine { private Object paperlock = new Object(); public synchronized void makecopies( Document d, int ncopies){ // only one thread at a time Original org = scanoriginal(d); for(int i=0; i<n; i++) { Paper p = getpaper(); copy(org, p); public void loadpaper (Paper[] p) { synchronized(paperlock){ putpaper(p) MAS HS12 29