Project. Threads. Plan for today. Before we begin. Thread. Thread. Minimum submission. Synchronization TSP. Thread synchronization. Any questions?

Similar documents
Java Collections Framework reloaded

Lecture 6 Collections

CMSC 132: Object-Oriented Programming II

Introduction to Java Threads

Principles of Software Construction: Concurrency, Part 2

Programming in Parallel COMP755

Java Threads. COMP 585 Noteset #2 1

Basics of. Multithreading in Java

An Introduction to Programming with Java Threads Andrew Whitaker University of Washington 9/13/2006. Thread Creation

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

Threads and Parallelism in Java

COMP 346 WINTER Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION

CS193k, Stanford Handout #8. Threads 3

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

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

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Note: Each loop has 5 iterations in the ThreeLoopTest program.

System Programming. Practical Session 4: Threads and Concurrency / Safety

Chapter 32 Multithreading and Parallel Programming

Java Threads. Introduction to Java Threads

Multiple Inheritance. Computer object can be viewed as

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

CS 556 Distributed Systems

Lecture 9: Introduction to Monitors

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 8. Nadia Polikarpova

04-Java Multithreading

Michele Van Dyne MUS 204B Concurrency Issues

Exercise Session Week 8

Threads Chate Patanothai

Reintroduction to Concurrency

CS 351 Design of Large Programs Threads and Concurrency

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

Exercise Session Week 8

Concurrency. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

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

MVP1: Introduction to concurrency in JAVA

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

Concurrency. Fundamentals of Computer Science

JAVA EXAMPLES - SOLVING DEADLOCK

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

User Space Multithreading. Computer Science, University of Warwick

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Generics and collections

Lecture 4. The Java Collections Framework

Principles of Software Construction: Concurrency, Part 1

7. MULTITHREDED PROGRAMMING

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

Synchronization SPL/2010 SPL/20 1

cs Java: lecture #6

Synchronization in Java

Deadlock. Only one process can use the resource at a time but once it s done it can give it back for use by another process.

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

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

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Concurrency in Object Oriented Programs 1. Object-Oriented Software Development COMP4001 CSE UNSW Sydney Lecturer: John Potter

Principles of Software Construction: Concurrency, Part 1

CSCI 201L Final Written Spring % of course grade

CS360 Lecture 10 Multithreading

1) Discuss the mutual exclusion mechanism that you choose as implemented in the chosen language and the associated basic syntax

Multithreading Pearson Education, Inc. All rights reserved.

Synchronization. Announcements. Concurrent Programs. Race Conditions. Race Conditions 11/9/17. Purpose of this lecture. A8 released today, Due: 11/21

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

Advanced Concepts of Programming

Concurrency Quiz: Java Threads

Multi-threading in Java. Jeff HUANG

The Java Collections Framework. Chapters 7.5

Parallel & Concurrent Programming

Threads and Java Memory Model

Parallel Programming Languages COMP360

Module Contact: Dr Anthony J. Bagnall, CMP Copyright of the University of East Anglia Version 2

Them Threads, Them Threads, Them Useless Threads

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Writing Parallel Programs COMP360

Info 408 Distributed Applications Programming Exercise sheet nb. 4

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015

Le L c e t c ur u e e 7 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Multithreading

COMP346 Winter Tutorial 4 Synchronization Semaphores

i219 Software Design Methodology 11. Software model checking Kazuhiro Ogata (JAIST) Outline of lecture

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

Lecture 8: September 30

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

Part IV Other Systems: I Java Threads

Principles of Software Construction: Concurrency, Part 2

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

Synchronization Lecture 23 Fall 2017

CP122 CS I. Iteration

Performance Throughput Utilization of system resources

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

Robotics and Autonomous Systems

Threads and Locks. CSCI 5828: Foundations of Software Engineering Lecture 09 09/22/2015

Multi-Threaded Programming Design CSCI 201 Principles of Software Development

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2012

MODULE 6q - Exceptions

Multithreaded Programming

Today: Synchronization. Recap: Synchronization

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

COURSE 11 PROGRAMMING III OOP. JAVA LANGUAGE

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

Question Points Score Total 100

Transcription:

Project Threads Synchronization Minimum submission Deadline extended to tonight at midnight Early submitters 10 point bonus TSP Still due on Tuesday! Before we begin Plan for today Thread synchronization Any questions? Questions with threads? Thread A thread is a single sequential flow of control within a program Sometimes called: Execution context Lightweight process Thread A thread is not a program It cannot run on its own Runs within a program

Thread Thread States Multiple threads can be running at the same time within a single program The start() method places a thread in the ready state Ready The scheduler selects a thread and places it in the running state Waiting Running A thread that is waiting for I/O, was suspended, is sleeping, blocked, or otherwise is unable to do any more work is placed in the waiting state join() join () is NOT a static method Calling join() on a thread will make the caller wait until the thread is done join() public class WorkerThread extends Thread { private int result = 0; // Perform a complicated time consuming calculation // and store the answer in the variable result public static void main(string args[]) { WorkerThread t = new WorkerThread(); t.start(); try { t.join(); catch ( InterruptedException ex ) { System.out.println( result ); One possible solution

Another possible scenerio Critical section Critical section block of code that must be executed by at most 1 thread at a time. The updating of common in the last example (run method) would be considered a critical section. Java locks Every Java Object contains a lock that can be held by at most one thread at any given time Allows for mutually exclusive access to a sequence of code Obtain lock here Hold lock Release lock synchronized A thread can obtain the lock on a Java object by using the synchronized statement. Example with Java locks public class ConcAccess extends Thread { private static final Integer lock = new Integer (0); synchronized (lock) {

Bad Example with Java locks Threads and Locks private Integer lock; public ConcAcess () { lock = new Integer(0); synchronized (lock) { Questions? Java locks with this An object can choose to grab a lock to itself when running one of its methods public somemethod() { synchronized( this ) { Synchronized methods Declaring a method to be synchronized is a shorthand for grabbing the lock on this. public synchronized somemethod() { Is the same as public somemethod() { synchronized( this ) { Synchronized Static Methods Java also provides synchronized static methods. Before a synchronized static method is executed, the calling thread must first obtain the class lock. Since there is only one class lock, at most one thread can hold the lock for the class (object locks can be held by different threads locking on different instances of the class). Synchronization and Java Collections From HashSet javadoc: Note that this implementation is not synchronized. If multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. Meaning Different threads can manipulate the collection concurrently.

Wrapper Implementations Wrapper implementations add some functionality on top of what a collection offer Synchronization Unmodifiable Wrappers simply delegate all of their real work to a specified collection synchronization wrappers The synchronization wrappers, will make the Collection thread safe. It is imperative that the user manually synchronize on the returned set when iterating over it: Set s = Collections.synchronizedSet(new HashSet());... synchronized(s) { Iterator i = s.iterator(); // Must be in the block while (i.hasnext()) foo(i.next()); synchronization wrappers Synchronization public static Collection synchronizedcollection(collection c); public static Set synchronizedset(set s); public static List synchronizedlist(list list); public static Map synchronizedmap(map m); public static SortedSet synchronizedsortedset(sortedset s); public static SortedMap synchronizedsortedmap(sortedmap m); Questions? Let s go through some code.