Concurrency in Java Prof. Stephen A. Edwards

Similar documents
Concurrency COMS W4115. Prof. Stephen A. Edwards Spring 2002 Columbia University Department of Computer Science

Implementing Coroutines. Faking Coroutines in Java

Concurrency in Java COMS W Prof. Stephen A. Edwards Fall 2002 Columbia University Department of Computer Science

Parallel Programming in OpenMP and Java

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

Introduction to Java Threads

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

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

Performance Throughput Utilization of system resources

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

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

Java Monitors. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Java Threads. COMP 585 Noteset #2 1

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

CMSC 330: Organization of Programming Languages

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

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

wait with priority An enhanced version of the wait operation accepts an optional priority argument:

Models of concurrency & synchronization algorithms

Multi-threaded programming in Java

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

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

Advanced Concepts of Programming

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

ECE 462 Object-Oriented Programming using C++ and Java. Scheduling and Critical Section

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004

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

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

Synchronization in Concurrent Programming. Amit Gupta

Concurrent Programming using Threads

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

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

Threads Questions Important Questions

Chapter 13. Concurrency ISBN

What's wrong with Semaphores?

CPS 506 Comparative Programming Languages. Programming Language Paradigms

COMP346 Winter Tutorial 4 Synchronization Semaphores

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

CMSC 433 Programming Language Technologies and Paradigms. Composing Objects

Outline of lecture. i219 Software Design Methodology 10. Multithreaded programming. Kazuhiro Ogata (JAIST)

CS 351 Design of Large Programs Threads and Concurrency

CS 556 Distributed Systems

CS11 Java. Fall Lecture 7

Chapter 32 Multithreading and Parallel Programming

Threads and Parallelism in Java

Concurrency, Mutual Exclusion and Synchronization C H A P T E R 5

Dealing with Issues for Interprocess Communication

Shared-Memory and Multithread Programming

Faculty of Computers & Information Computer Science Department

Java Threads. Introduction to Java Threads

7. MULTITHREDED PROGRAMMING

CMSC 330: Organization of Programming Languages

CS 159: Parallel Processing

Concurrent Programming

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

G52CON: Concepts of Concurrency

Producing Production Quality Software. Lecture 12: Concurrent and Distributed Programming Prof. Arthur P. Goldberg Fall, 2004

Monitors; Software Transactional Memory

Model Requirements and JAVA Programs MVP 2 1

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 Chate Patanothai

Problems with Concurrency. February 19, 2014

THREADS AND CONCURRENCY

Lecture 9: Introduction to Monitors

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

Concurrency & Synchronization. COMPSCI210 Recitation 25th Feb 2013 Vamsi Thummala Slides adapted from Landon Cox

Concurrency. Chapter 5

Lecture 10 State dependency

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

What is a thread anyway?

Ch 9: Control flow. Sequencers. Jumps. Jumps

Introducing Shared-Memory Concurrency

Reintroduction to Concurrency

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

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

Programming in Parallel COMP755

User Space Multithreading. Computer Science, University of Warwick

CS18000: Programming I

Threads, Concurrency, and Parallelism

CS180 Review. Recitation Week 15

Multiple Inheritance. Computer object can be viewed as

Multithread Computing

Lesson 6: Process Synchronization

MCS-378 Intraterm Exam 1 Serial #:

Animation Part 2: MoveableShape interface & Multithreading

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

CMSC421: Principles of Operating Systems

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

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

CS 111. Operating Systems Peter Reiher

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

THREADS & CONCURRENCY

Synchronization in Java

Chapter 5 Concurrency: Mutual Exclusion. and. Synchronization. Operating Systems: Internals. and. Design Principles

CS360 Lecture 12 Multithreading

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team

Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07

Transcription:

Concurrency in Java Prof. Stephen A. Edwards

The Java Language Developed by James Gosling et al. at Sun Microsystems in the early 1990s Originally called Oak, first intended application was as an OS for TV set top boxes Main goals were portability and safety Originally for embedded consumer software

The Java Language Set-top boxes: nobody cared Next big application: applets Little programs dynamically added to web browsers Enormous Sun marketing blitz Partial failure: Incompatible Java implementations Few users had enough bandwidth Fantastically slow Java interpreters Javascript has largely taken over this role High-level scripting language Has nothing to do with the Java language

The Java Language Where does Java succeed? Corporate programming E.g., dynamic web page generation from large corporate databases in banks Environment demands simpler language Unskilled programmers, unreleased software Speed, Space not critical Tends to be run on very large servers Main objective is reduced development time

The Java Language Where does Java succeed? Education Well-designed general-purpose programming language Spares programmer from many common pitfalls Uninitialized pointers Memory management Widely known and used, not just a teaching language Embedded Systems? Jury is still out

Overview of Java Derived from C++, but incompatible Didn t want to call it C += 2? No loose functions: everything part of a class Better package support (no preprocessor) Safer object references instead of pointers Large, powerful class library Automatic garbage collection Programmer spared from memory management

Concurrency in Java Language supports threads Multiple contexts/program counters running within the same memory space All objects can be shared among threads Fundamentally nondeterministic Language provide some facilities to help avoid it

Thread Basics How to create a thread: class MyThread extends Thread { public void run() { /* thread body */ MyThread mt = new MyThread; // Create thread mt.start(); // Starts thread running at run() // Returns immediately

Thread Basics A thread is a separate program counter and stack, local variables, etc. Not an object or a collection of things Classes, objects, methods, etc. do not belong to a thread Any method may be executed by one or more threads, even simultaneously

The Sleep Method public void run() { for(;;) { try { sleep(1000); // Pause for 1 second catch (InterruptedException e) { return; // caused by thread.interrupt() System.out.println( Tick );

The Sleep Method public void run() { for(;;) { try { sleep(1000); Does this print Tick once a second? No. sleep() delay a lower bound Rest of loop takes indeterminate amount of time catch (InterruptedException e) { return; System.out.println( Tick );

Races In a concurrent world, always assume someone else is accessing your objects Other threads are your adversary Consider what can happen when simultaneously reading and writing: Thread 1 Thread 2 f1 = a.field1 f2 = a.field2 a.field1 = 1 a.field2 = 2

Races Thread 1 goes first Thread 1 reads original values Thread 1 Thread 2 f1 = a.field1 a.field1 = 1 f2 = a.field2 a.field2 = 2

Races Thread 2 goes first Thread 1 reads new values Thread 1 Thread 2 f1 = a.field1 a.field1 = 1 f2 = a.field2 a.field2 = 2

Races Interleaved execution Thread 1 sees one new value, one old value Thread 1 Thread 2 f1 = a.field1 a.field1 = 1 f2 = a.field2 a.field2 = 2

Non-atomic Operations 32-bit reads and writes are guaranteed atomic 64-bit operations may not be Therefore, int i; double d; Thread 1 Thread 2 i = 10; i = 20; i will contain 10 or 20 d = 10.0; d = 20.0; i might contain garbage

Per-Object Locks Each Java object has a lock that may be owned by at least one thread A thread waits if it attempts to obtain an already-obtained lock The lock is a counter: one thread may lock an object more than once

The Synchronized Statement A synchronized statement gets an object s lock before running its body Counter mycount = new Counter; synchronized(mycount) { mycount.count(); get the lock for mycount before calling count() Releases the lock when the body terminates Choice of object to lock is by convention

Synchronized Methods class AtomicCounter { private int _count; get the lock for the AtomicCounter object before running this method public synchronized void count() { _count++; This implementation guarantees at most one thread can increment the counter at any time

Deadlock synchronized(foo) { synchronized(bar) { /* Deadlocked */ synchronized(bar) { synchronized(foo) { /* Deadlocked */ Rule: always acquire locks in the same order

Priorities Each thread has a priority from 1 to 10 (5 typical) Scheduler s job is to keep highest-priority threads running thread.setpriority(5)

What the Language Spec. Says From The Java Language Specification Every thread has a priority. When there is competition for processing resources, threads with higher priority are generally executed in preference to threads with lower priority. Such preference is not, however, a guarantee that the highest priority thread will always be running, and thread priorities cannot be used to reliably implement mutual exclusion. Vague enough for you?

Multiple threads at same priority? Language gives implementer freedom Calling yield() suspends current thread to allow other at same priority to run maybe Solaris implementation runs threads until they stop themselves (wait(), yield(), etc.) Windows implementation timeslices

Starvation Not a fair scheduler Higher-priority threads can consume all resources, prevent lower-priority threads from running This is called starvation Timing dependent: function of program, hardware, and Java implementation

Waiting for a Condition Say you want a thread to wait for a condition before proceeding An infinite loop may deadlock the system while (!condition) { Yielding avoids deadlock, but is very inefficient while (!condition) yield();

Java s Solution: wait() and notify() wait() like yield(), but requires other thread to reawaken it while (!condition) wait(); Thread that might affect this condition calls() notify to resume the thread Programmer responsible for ensuring each wait() has a matching notify()

wait() and notify() Each object has a set of threads that are waiting for its lock (its wait set) synchronized (obj) { obj.wait(); In other thread: obj.notify(); // Acquire lock on obj // suspend // add thread to obj s wait set // relinquish locks on obj // enable some waiting thread

wait() and notify() 1. Thread 1 acquires lock on object 2. Thread 1 calls wait() on object 3. Thread 1 releases lock on object, adds itself to object s wait set 4. Thread 2 calls notify() on object (must own lock) 5. Thread 1 is reawakened: it was in object s wait set 6. Thread 1 reacquires lock on object 7. Thread 1 continues from the wait()

wait() and notify() Confusing enough? notify() non-deterministically chooses one thread to reawaken (may be many waiting on same object) What happens when there s more than one? notifyall() enables all waiting threads Much safer?

Building a Blocking Buffer class OnePlace { El value; public synchronized void write(el e) { public synchronized El read() { Idea: One thread at a time can write to or read from the buffer Thread will block on read if no data is available Thread will block on write if data has not been read

Building a Blocking Buffer synchronized void write(el e) throws InterruptedException { while (value!= null) wait(); // Block while full value = e; notifyall(); // Awaken any waiting read public synchronized El read() throws InterruptedException { while (value == null) wait(); // Block while empty El e = value; value = null; notifyall(); // Awaken any waiting write return e;

Thread States blocked I/O started waiting I/O completed wait() notify() born start() ready running yield() sleep() terminate dead timeout sleeping