Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619.

Slides:



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

Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Concurrency 101 Shared state. Part 1: General Concepts 2.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
1 Chapter 8 Three Interfaces: Cloneable, Serializable, and Runnable.
1 L49 Multithreading (1). 2 OBJECTIVES  What threads are and why they are useful.  How threads enable you to manage concurrent activities.  The life.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Multithreading.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
Threads some important concepts Simon Lynch
Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Internet Software Development Controlling Threads Paul J Krause.
Threading Eriq Muhammad Adams J
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Synchronizing threads, thread pools, etc.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Distributed and Parallel Processing George Wells.
Threads in Java Two ways to start a thread
Multithreading / Concurrency
Multi Threading.
Multithreading.
Background on the need for Synchronization
PA1 Discussion.
Multithreaded Programming in Java
23 Multithreading.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Multithreading Chapter 23.
Synchronization Lecture 23 – Fall 2017.
Monitors Chapter 7.
Multithreading.
Multithreading Tutorial
COP 4600 Operating Systems Fall 2010
Race Conditions & Synchronization
Multithreading.
Monitors Chapter 7.
Monitors Chapter 7.
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
Multithreading in java.
NETWORK PROGRAMMING CNET 441
Threads and Multithreading
Electrical and Computer Engineering
some important concepts
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619

Agenda Some General Concurrency Mechanisms Concurrency in Java Synchronization in Java Rules for Threads

Concepts to be covered Threads, concurrency Interleaving, race conditions Atomicity of execution Mutual exclusion Thread states Synchronization, locks Inter-thread communication

Some General Concurrency Mechanisms Rendevous (Ada) Obviates need for certain low level primitives In practice: Web Servers Single server, scores of clients! Waiting time? Threads Subject of today’s talk Database mechanisms Concurrency entirely hidden from user ACID properties

Interleaving Suppose 2 threads are created c.inc(); // c is a counter c.inc(); System.out.println(c.get()); Suppose 2 threads are created Each executes the 3 statements Order in which they are executed? Does thread 1 get to execute all 3 before thread 2?

Atomicity of execution What is the smallest unit of execution that cannot be interleaved? Can we ensure atomicity across multiple statements? // broken because of interleaving private static int nextSerialNumber = 0; // in short nSN public static int generateSerialNumber(){ return nextSerialNumber++; } // nSN = nSN + 1

Atomic read and write Thread 1 reads nSN = 4 Thread 2 reads nSN = 4 //should read 5 Thread 3 reads nSN = 4 //should read 6 Thread 1 writes nSN = 5 Thread 2 writes nSN = 5 //should write 6 Thread 4 reads nSN = 5 //should read 7 Time

Concurrency in Java Various mechanisms to obtain a Thread Extend the Thread class class T extends Thread { … public void run() {…} } Thread t = new T(); t.start(); // start() calls run() Implement the Runnable (or Callable) interface Use an ExecutorService Examples: Counter1.java, Counter2.java, Counter3.java, Counter4.java

Thread states Initial – prior to start() Runnable – started, but not necessarily running (may be preempted) Blocked – waiting for some event to occur Stop – the run() method has returned (avoid other ways of entering Stop state) The join() method waits for a thread to stop Or ExecutorService shutdown(), get() methods

Synchronization in Java Threads use a “lock” model Lock is associated with an object “this” for synchronized methods Specified object for synchronized block Security/Reliability implications for public locks Only one thread at a time may hold the lock Examples Wacky.java, Wacky1.java BoundedQueue.java SyncBoundedQueue.java

Java Synchronization Every Java object has an associated lock “synchronized” makes a method lockable A thread that locked a lock is said to be holding the lock When a thread unlocks a lock, the thread is said to release the lock Synchronization: mutual exclusion and signal-wait

Communication among Threads wait() – enter blocked state and release the lock notify() – wake up some (other) blocked thread notifyAll() – wake up all the (other) blocked threads. This is harder than it looks! Example: BoundedQueueWithGuards.java

What can go Wrong? Deadlock – no progress possible Starvation – no access to a resource Simultaneous access to shared resources – unpredictable behavior Various tools to analyze these issues