public void run ( ) { i f ( this. id == 0) System. out. p r i n t ( 3 ) ; bro. j o i n ( ) ; else System. out. p r i n t ( 2 ) ;

Similar documents
CMSC 132, Object-Oriented Programming II Summer Lecture 6:

CMSC 132, Object-Oriented Programming II Summer Lecture 10:

Lecture 5: Sep. 23 &25

CMSC 132, Object-Oriented Programming II Summer Lecture 12

INF Models of concurrency

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Design Patterns and Refactoring

Operating Systems. VII. Synchronization

Lists, Stacks, and Queues (plus Priority Queues)

Lecture 14: Nov. 11 & 13

Clojure Concurrency Constructs, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 13 10/07/2014

Safety and Liveness. Thread Synchronization: Too Much Milk. Critical Sections. A Really Cool Theorem

Elementary Sorts 1 / 18

Blocking Synchronization: Streams Vijay Saraswat (Dec 10, 2012)

CMSC 132, Object-Oriented Programming II Summer Lecture 11:

ITI Introduction to Computing II

1 Java Night Countdown (30%)

SDS developer guide. Develop distributed and parallel applications in Java. Nathanaël Cottin. version

Java Programming. Final Examination on December 13, 2015 Fall 2015

Extensibility Patterns: Extension Access

Distributed Systems Part II Solution to Exercise Sheet 10

CS-140 Fall 2018 Test 2 Version Practice Nov. 12, Name:

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

ITI Introduction to Computing II

1. Write a program to calculate distance traveled by light

DNHI Homework 2 Solutions Recursion

Lecture 5: Jun. 10, 2015

CSE 311: Foundations of Computing. Lecture 10: Set Operations & Representation, Modular Arithmetic

Techniques of Java Programming

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10

CS162 Operating Systems and Systems Programming Lecture 7 Semaphores, Conditional Variables, Deadlocks"

Lecture 4: Stacks and Queues

Maps performance tips On server: Maintain DB connections, prepared statements (per thread/request!)

Real Time Operating Systems

Sharing Objects. Pieter van den Hombergh. Fontys Hogeschool voor Techniek en Logistiek. February 15, 2017

Deadlock (2) Dave Eckhardt Brian Railing Roger Dannenberg

CS Java. Introduction to Java. Andy Mroczkowski Department of Computer Science Drexel University

Algorithmic verification

CS-140 Fall 2018 Test 2 Version Practice Nov. 12, 2018

CSE 311: Foundations of Computing. Lecture 10: Set Operations & Representation, Modular Arithmetic

CMSC 132, Object-Oriented Programming II Summer Lecture 1:

Report of Dragable Notebook

CIS 4930/6930: Principles of Cyber-Physical Systems

Exercise 1 (15 points)

LTL Model Checking. Wishnu Prasetya.

Threadsafe design Confinement (revisited) Java monitor pattern. Delegating thread safety. Untitled Sheets HOM. Threadsafe design

Introduction to Programming (Java) 3/12

Exercise 1: Formal Tools and Techniques (10 points)

Real Time Operating Systems

Introduction to Computing II (ITI1121) FINAL EXAMINATION

CSE 311: Foundations of Computing. Lecture 27: Undecidability

The Midterm Exam. Hsuan-Tien Lin. Department of CSIE, NTU. OOP Class, April 26-27, 2010

Discovering Spam On Twitter

Object Oriented Software Design (NTU, Class Even, Spring 2009) Final Examination Problem Sheet TIME: 06/16/2009, 14:20 17:20

Multicore Semantics and Programming

CSE 311: Foundations of Computing. Lecture 10: Set Operations & Representation, Modular Arithmetic

INF 4140: Models of Concurrency Series 3

1 ListElement l e = f i r s t ; / / s t a r t i n g p o i n t 2 while ( l e. next!= n u l l ) 3 { l e = l e. next ; / / next step 4 } Removal

process arrival time CPU burst time priority p1 0ms 25ms 3 p2 1ms 9ms 1 p3 20ms 14ms 4 p4 32ms 4ms 2

Problem Decomposition: One Professor s Approach to Coding

More About Methods. Hsuan-Tien Lin. Deptartment of CSIE, NTU. OOP Class, March 8-9, 2010

DM507 - Algoritmer og Datastrukturer Project 1

Complexity of automatic verification of cryptographic protocols

Design Pattern. Observer Pattern

Abstractions and Decision Procedures for Effective Software Model Checking

Instance Methods and Inheritance (1/2)

Simply Typed Lambda Calculus

EDA045F: Program Analysis LECTURE 10: TYPES 1. Christoph Reichenbach

Introduction to Algorithmic Complexity. D. Thiebaut CSC212 Fall 2014

Deadlock checking by a behavioral effect system for lock handling. Research Report No Ka I Pun, Martin Steffen, and Volker Stolz

Automata-Theoretic Model Checking of Reactive Systems

CS 6112 (Fall 2011) Foundations of Concurrency

Predicate Abstraction: A Tutorial

L43-Mon-12-Dec-2016-Rev-Cpt-4-for-Final-HW44-and-Rev-Cpt-5-for-Final-HW45 Page 27. L43-Mon-12-Dec-2016-Rev-Cpt-4-HW44-and-Rev-Cpt-5-for-Final-HW45

Übung Informatik I - Programmierung - Blatt 7

Binary Search Trees. Motivation

CS-206 Concurrency. Lecture 13. Wrap Up. Spring 2015 Prof. Babak Falsafi parsa.epfl.ch/courses/cs206/

Voortgezet Programmeren

CSE505, Fall 2012, Final Examination December 10, 2012

Lecture 16 More Profiling: gperftools, systemwide tools: oprofile, perf, DTrace, etc.

Outline Resource Introduction Usage Testing Exercises. Linked Lists COMP SCI / SFWR ENG 2S03. Department of Computing and Software McMaster University

Code Listing for Problem 4

Is the sum of two integers positive, negative, or zero? How can you tell? ACTIVITY: Adding Integers with the Same Sign

DM49-2. Obligatoriske Opgave

Parameterised! Linearisability Andrea Cerone

Apache Quarks for Developers April 13, 2016

An introduction to Uppaal and Timed Automata MVP5 1

Introduction to Computer Programming, Spring Term 2018 Practice Assignment 1 Discussion:

Introduction to Statics

Richard Gibson CSC 421 Assignment #4 Apr. 4, 2006 #

PySy: A Python Package for Enhanced Concurrent Programming. TODD WILLIAMSON B.S (University of California at Davis) 2007 THESIS

Software Engineering

Clock Analysis of X10 Programs

Chapter 4. A simple method for solving first order equations numerically

Problems, and How Computer Scientists Solve Them Manas Thakur

CS Exam 1 Study Guide and Practice Exam

Distributed Architectures

Time. To do. q Physical clocks q Logical clocks

Actors for Reactive Programming. Actors Origins

Math 3361-Modern Algebra Lecture 08 9/26/ Cardinality

Transcription:

1 Unusual programs 1. Consider the following Java program : public c l a s s Thread2 extends Thread { public int id ; public Thread2 bro ; public Thread2 ( int id, Thread2 bro ) { this. id = id ; this. bro = bro ; i f ( this. id == 0) System. out. p r i n t ( 3 ) ; else { bro. j o i n ( ) ; System. out. p r i n t ( 4 ) ; System. out. p r i n t ( 0 ) ; public c l a s s Thread1 extends Thread{ public int id ; public Thread1 ( int id ){ this. id = id ; i f ( this. id == 0){ Thread t = new Thread2 ( 1, null ) ; t. bro = new Thread2 ( 0, t ) ; t. bro. s t a r t ( ) ; t. s t a r t ( ) ; else System. out. p r i n t ( 2 ) ; public static void main ( S t r i n g [ ] args ){ new Thread1 ( 0 ). s t a r t ( ) ; new Thread1 ( 1 ). s t a r t ( ) ; What are the possible outputs? 32040 Y N 34002 Y N 30402 Y N 23040 Y N 40302 Y N

2. Consider the following Java program: public c l a s s Thread1 extends Thread{ public int id ; public int r e s u l t ; public Thread1 bro ; public Thread1 ( int id, Thread1 bro ){ this. id = id ; this. r e s u l t = 1; this. bro = bro ; System. out. p r i n t l n (1 this. id ) ; i f ( id == 0){ bro. j o i n ( ) ; System. out. p r i n t l n ( bro. r e s u l t ) ; else this. r e s u l t = 2 ; System. out. p r i n t l n ( Bye ) ; public static void main ( S t r i n g [ ] args ){ Thread1 t1 = new Thread1 ( 0, null ) ; Thread1 t2 = new Thread1 ( 1, t1 ) ; t1. bro = t2 ; System. out. p r i n t l n ( Hello ) ; t1. s t a r t ( ) ; t2. s t a r t ( ) ; Give 3 valid outputs of the program. Page 2

3. Consider the following Java program: public c l a s s Example extends Thread{ public int counter ; public int l i ; public Example ( int cpt, int l i ){ this. counter = cpt ; this. l i = l i ; int i ; for ( i = l i ; i <2; i ++){ System. out. p r i n t l n ( counter = + this. counter ) ; (new Example ( this. counter +1, i + 1 ) ). s t a r t ( ) ; this. counter++; System. out. p r i n t l n ( counter = + this. counter ) ; public static void main ( S t r i n g [ ] args ){ Example e = new Example ( 0, 0 ) ; e. s t a r t ( ) ; A. What are the possible values of counter printed in the first line? B. What are the possible values of counter printed in the last line? C. How many times could the value of counter be printed? Page 3

2 Concurrency problems 4. public class LinkedList { private LinkedList next ; private f i n a l ReentrantLock l add = new ReentrantLock ( ) ; private f i n a l ReentrantLock l remove = new ReentrantLock ( ) ; public LinkedList ( ) { this. next = null ; public LinkedList add ( ) { l a d d. l o c k ( ) ; LinkedList r e s = new LinkedList ( ) ; r e s. next = this ; l a d d. unlock ( ) ; return r e s ; public LinkedList remove ( ) { l remove. l o c k ( ) ; i f ( this == null ) return null ; LinkedList r e s = this. next ; this. next = null ; l remove. unlock ( ) ; return r e s ; The implementation may deadlock Y N The implementation may crash Y N The function add always add at the beginning of the list Y N The fuction remove always remove the first element of the list Y N Using only one lock is possible Y N Page 4

5. This exercice is here to show you what to NEVER do. public class BankAccount { private int balance = 0 ; private Lock l k = new Lock ( ) ; int getbalance ( ) { int ans = balance ; return ans ; void setbalance ( int x ) { balance = x ; void withdraw ( int amount ) { int b = getbalance ( ) ; i f ( amount > b ) { throw new WithdrawTooLargeException ( ) ; setbalance ( b amount ) ; 1. Explain why, if the lock is not re-entrant, this implementation will deadlock. 2. Still considering this is not a re-entrant lock, you replace the function withdraw by : void withdraw ( int amount ) { int b = getbalance ( ) ; i f ( amount > b ) { throw new WithdrawTooLargeException ( ) ; setbalance ( b amount ) ; Does it work now? Can it be improved? Page 5

6. We implemented a new Bank Account class trying to solve the problem.we use a class Account to manage the bank account and a class Transaction class to manage the use of Account functions. public class Account { double balance ; int id ; Lock l o c k = new ReentrantLock ( ) ; Account ( int id, double balance ) { this. id = id ; this. balance = balance ; boolean withdraw ( double amount ) { i f ( this. l o c k. trylock ( ) ) { balance = amount ; this. l o c k. unlock ( ) ; return true ; return f a l s e ; boolean d e p o s i t ( double amount ) { i f ( this. l o c k. trylock ( ) ) { balance += amount ; this. l o c k. unlock ( ) ; return true ; return f a l s e ; public boolean t r y T r a n s f e r ( Account account, double amount ) { i f ( this. withdraw ( amount ) ) { i f ( account. d e p o s i t ( amount ) ) {return true ; // e l s e d e s t i n a t i o n account busy, refund source account. this. d e p o s i t ( amount ) ; return f a l s e ; public static void main ( S t r i n g [ ] args ) { Account a1 = new Account ( 1, 500d ) ; Account a2 = new Account ( 2, 500d ) ; new Thread (new Transaction ( a1, a2, 10d ), tr 1 ) ; ). s t a r t ( ) ; new Thread (new Transaction ( a2, a1, 10d ), tr 2 ) ; ). s t a r t ( ) ; class Transaction implements Runnable { private Account source, d e s t i n a t i o n ; private double amount ; Transaction ( Account source, Account d e s t i n a t i o n, double amount ) { this. s ource = source ; this. d e s t i n a t i o n = d e s t i n a t i o n ; Page 6

this. amount = amount ; while (! source. t r y T r a n s f e r ( d e s t i n a t i o n, amount ) ) ; 1. Unfortunately, this code is flawed. Can you explain the problem? 2. We change the class Transaction by class Transaction implements Runnable { private Account sourceaccount, destinationaccount ; private double amount ; private f i n a l static Lock l = new Lock ( ) ; Transaction ( Account source, Account dest, double amount ) { this. s ource = source ; this. d e s t i n a t i o n = dest ; this. amount = amount ; i f (! source. t r y T r a n s f e r ( d e s t i n a t i o n, amount ) ) { l. l o c k ( ) ; while (! source. t r y T r a n s f e r ( d e s t i n a t i o n, amount ) ) ; l. unlock ( ) ; Does it work now? If yes, explain the difference. If not, tell what is missing. Page 7