Concurrency without Locks

Slides:



Advertisements
Similar presentations
Class 18: Concurrency on Mars Fall 2010 UVa David Evans cs2220: Engineering Software Image: Michael Dewey-Vogt.
Advertisements

Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Concurrent Programming Abstraction & Java Threads
CMSC 330: Organization of Programming Languages Threads Classic Concurrency Problems.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 16: Concurrent Programming.
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
G52CON: Concepts of Concurrency Lecture 2 Processes & Threads Chris Greenhalgh School of Computer Science
Thread Control methods The thread class contains the methods for controlling threads Thread() create default thread Thread(target: runnable ) creates new.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Tuple Spaces and JavaSpaces CS 614 Bill McCloskey.
Definitions Process – An executing program
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Lecture 4 : JAVA Thread Programming
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
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.
Concurrent Programming in Java Dr. Zoltan Papp. Motivation: event driven, responsive systems Sequential approach: while ( true ) { do event = getEventId()
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Internet Software Development Controlling Threads Paul J Krause.
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Threads.
Synchronizing threads, thread pools, etc.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
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
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
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.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 22: Abstractions for Concurrency.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
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.
Distributed and Parallel Processing George Wells.
Java Thread Programming
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Multithreading / Concurrency
Multi Threading.
Java Multithreading.
Locking cs205: engineering software
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Lecture 15: Concurring Concurrently CS201j: Engineering Software
Concurrency and Threads
Multithreading.
Java Based Techhnology
Multithreading.
Java Concurrency 17-Jan-19.
Chapter 15 Multithreading
Computer Science 2 06A-Java Multithreading
Java Concurrency.
CSE 153 Design of Operating Systems Winter 19
Java Concurrency.
Threads and Multithreading
Concurrent programming
Java Concurrency 29-May-19.
Software Engineering and Architecture
More concurrency issues
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Concurrency without Locks cs205: engineering software university of virginia fall 2006 Concurrency without Locks Kramer Sharp

Recap synchronized(obj) { code } Provides mutual exclusion: code inside synchronized can only run when lock of obj is held obj.wait(): Gives up lock on obj; puts current thread in waiting set for obj obj.notify(), obj.notifyAll(): Don’t give up lock; selects one (notify) or all (notifyAll) threads in waiting set for obj and wakes them up (to be scheduled)

wait and notify waiting awake, but not running Thread A Thread B synchronized (o) o.wait () synchronized (o) { waiting o.notify () awake, but not running } // end synchronized can reclaim o lock

wait and notify waiting waiting still waiting If multiple Thread C Thread A synchronized (o) Thread B If multiple threads are waiting on the same object, any one of them can be awakened o.wait () synchronized (o) waiting o.wait () synchronized (o) { waiting o.notify () awake, not running still waiting } // end synchronized

class IncThread extends Thread { private Counter c; public IncThread (Counter p_c) { c = p_c; } public void run () { while (true) { synchronized (c) { c.increment (); System.err.println ("Running inc thread: " + currentThread () + …); c.notify (); } } } } class DecThread extends Thread { … while (c.getValue () <= 0) { try { c.wait (); } catch (InterruptedException e) { ; } } c.decrement (); System.err.println ("Running dec thread: " + …);

Counter c = new Counter (); IncThread ithread = new IncThread (c); DecThread dthread = new DecThread (c); ithread.setPriority (Thread.NORM_PRIORITY); ithread.start (); dthread.setPriority (Thread.MAX_PRIORITY); dthread.start (); Running inc thread: Thread[Thread-0,5,main] / Value: 1 Running dec thread: Thread[Thread-1,10,main] / Value: 0

Priorities In general, threads with higher priorities will be scheduled preferentially. There are no guarantees: up to Java scheduler Thread class: void setPriority (int newPriority) // MODIFIES: this // EFFECTS: Changes the priority of this // thread to newPriority.

Priorities, Priorities ithread.setPriority (Thread.NORM_PRIORITY); ithread.start (); dthread.setPriority (Thread.MIN_PRIORITY); dthread.start (); The ithread should run more than the dthread, but there is no guarantee. Thread.MIN_PRIORITY Thread.NORM_PRIORITY Thread.MAX_PRIORITY

Questions from Monday’s Notes Is it possible for a thread to simultaneously be in the wait set for more than one object?

Bad Waiting synchronized (first) { if (first.isHeld ()) { try { first.wait(); } catch (InterruptedException e) { return; } } first.take(this); No guarantee !first.isHeld() here

Stopping Threads Deprecated. This method is inherently unsafe. public class java.lang.Thread { public final void stop() Deprecated. This method is inherently unsafe. Forces the thread to stop executing. …The thread represented by this thread is forced to stop whatever it is doing abnormally and to throw a newly created ThreadDeath object as an exception. …

Why deprecate stop? What should happen to all the locks a thread owns when it is stopped? What if an invariant is temporarily broken in a method?

Suspending Threads public final void suspend() Suspends this thread. If the thread is alive, it is suspended and makes no further progress unless and until it is resumed. Deprecated. This method has been deprecated, as it is inherently deadlock-prone. If the target thread holds a lock on the monitor protecting a critical system resource when it is suspended, no thread can access this resource until the target thread is resumed. If the thread that would resume the target thread attempts to lock this monitor prior to calling resume, deadlock results. Such deadlocks typically manifest themselves as "frozen" processes.

Can’t stop, can’t suspend, what can you do? public void interrupt() Interrupts this thread. If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException. … If none of the previous conditions hold then this thread's interrupt status will be set.

Being Interrupted public boolean isInterrupted() MODIFIES: nothing EFFECTS: Returns true iff this thread has been interrupted.

Counter c = new Counter (); IncThread ithread = new IncThread (c); DecThread dthread = new DecThread (c); ithread.setPriority (Thread.NORM_PRIORITY); ithread.start (); dthread.setPriority (Thread.MAX_PRIORITY); dthread.start (); dthread.interrupt (); Running inc thread: Thread[Thread-0,5,main] / Value: 1 Running dec thread: Thread[Thread-1,10,main] / Value: 0 Interrupts are just “polite” requests! The thread can ignore it and keep going…

JavaSpaces

If we can get rid of mutation, then concurrency should be easy! Concurrency Problems What is the source of all concurrency problems? Mutability! If nothing is mutable, then the order of events doesn’t matter. If we can get rid of mutation, then concurrency should be easy!

Linda David Gelernter (Yale, 1980s) Basis for JavaSpaces, Sun 1999 Jini – Java’s distributed computing environment “Network plug and play”

Basic Idea Have a shared space (“tuple space”) Processes can write, read, and take away values from this space Bag of processes, each looks for work it can do by matching values in the tuple space Get load balancing, synchronization, messaging, etc. for free!

JavaSpaces

Tuples Conventional Memory Linda/JavaSpaces Unit Bit Logical Tuple (23, “test”, false) Access Using Address (variable) Selection of values Operations read, write (mutates) take, write immutable

Tuple Space Operations write (t) – add tuple t to tuple space take (s)  t – returns and removes tuple t matching template s read (s)  t – same as take, except doesn’t remove t. Operations are atomic (even if space is distributed)

JavaSpaces (Not included in Java API, need to download) interface net.jini.space.JavaSpace { public Entry take (Entry tmpl, Transaction txn, long timeout) // EFFECTS: Takes an entry in the space // that matches the template, blocking // until one exists. Returns null if timeout // expires first.

Entry Matching An entry in the Space matches a template object if: Its type is a subtype of the template object For every field in the template object: The value is null: matches anything The value is non-null: values are identical (same bits in representation, not equals!) For our examples, we’ll use Linda model: Matching types and literals

Meaning of take take (“f”, int n) take (“f”, 23) take (“t”, bool b, int n) take (string s, int n) take (“cookie”) Tuple Space (“f”, 23) (“t”, 25) (“t”, true) (“t”, false) (“f”, 17)

Distributed Ebay Offer Item (String item, int minbid, int time): write (item, minbid, “owner”); sleep (time); take (item, formal bid, formal bidder); if (bidder  “owner”) SOLD! Bid (String bidder, String item, int bid): take (item, formal highbid, formal highbidder); if (bid > highbid) write (item, bid, bidder) else write (item, highbid, highbidder) How well would this work?

Factorial Setup: for (int i = 1; i <= n; i++) write (i); repeat n – 1 times: start FactTask FactTask: take (int i); take (int j); write (i * j); Eventually, tuple space contains one entry which is the answer. What if last two elements are taken concurrently? Better way to order Setup?

Finishing Factorial Setup: for (int i = 1; i <= n; i++) write (i); write (“workleft”, n - 1); take (“workleft”, 0); take (result); FactTask: take (“workleft”, formal w); if (w > 0) take (int i); take (int j); write (i * j); write (“workleft”, w – 1); Opps – we’ve sequentialized it!

Concurrent Finishing Factorial Setup: repeat n-1 times: start FactWorker out (“done”, 0); for (int i = 1; i <= n; i++) { write (i); if i > 1 write (“work”); } take (“done”, n-1); take (result); FactWorker: take (“work”); take (formal int i); take (formal int j); write (i * j); take (“done”, formal int n); write (“done”, n + 1);

Sorting in Linda Problem: Sort an array of n integers Initial tuple state (“A”, [A[0], ..., A[n-1]]) Final tuple state: (“A”, [A’[0], ..., A’[n-1]]) such A’ has a corresponding element for every element in A, and for all 0 <= j < k <= n-1, A’[j] <= A’[k]

Charge Computers are single-threaded machines that provide their owner the illusion of multiple threads. Brains are multi-threaded machines that provide their owner with the illusion of a single thread. Thread work = new Thread (ps5); work.setPriority (Thread.MAX_PRIORITY); work.start ();