Li Tak Sing (Lecture 3) COMPS311F 1. Flexible synchronization with locks Synchronized methods and statement blocks: Advantages: Relatively easy to use.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Made with love, by Zachary Langley Applets The Graphics Presentation.
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
CMSC 330: Organization of Programming Languages Threads Classic Concurrency Problems.
1Deadlock DeadlockedBankAccount –withdraw(){ lock.lock(); while(balance
1 Java threads: synchronization. 2 Thread states 1.New: created with the new operator (not yet started) 2.Runnable: either running or ready to run 3.Blocked:
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 24 Multithreading.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  synchronized methods and statements  wait.
Locks (Java 1.5) Only one thread can hold a lock at once –Other threads that try to acquire it block (or become suspended) until lock becomes available.
Multithreading. RHS – SWC 2 What is a thread Inside a single process, multiple threads can be executing concurrently A thread is the execution of (part.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Threads Daniel Bennett, Jeffrey Dovalovsky, Dominic Gates.
Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
1 L49 Multithreading (1). 2 OBJECTIVES  What threads are and why they are useful.  How threads enable you to manage concurrent activities.  The life.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L21 (Chapter 24) Multithreading.
Threads A thread is a program unit that is executed independently of other parts of the program A thread is a program unit that is executed independently.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Multithreading.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 19 Multithreading.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 24 Multithreading.
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
CSE 501N Fall ‘09 23: Advanced Multithreading: Synchronization and Thread-Safety December 1, 2009 Nick Leidenfrost.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
Multi-Threaded Programming Design CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Multithreading [Modified]
Multithreading in JAVA
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
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.
1 Chapter 19 Multithreading. 2 Objectives F To understand the concept of multithreading and apply it to develop animation (§19.2). F To develop thread.
Multithreading (Based on Chapter 29 (Multithreading) Liang book and Chapter 5 (Threads) Elliotte Rusty book) Dr. Tatiana Balikhina 1.
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.
Synchronization (Threads Accessing Shared Data). Contents I.The Bank Transfer Problem II.Doing a Simulation on the Bank Transfer Problem III.New Requirement:
Multithreading / Concurrency
Chapter 30 Multithreading and Parallel Programming
Chapter 13: Multithreading
Multi Threading.
Multithreaded Programming in Java
Multithreading Chapter 9.
Threads Chate Patanothai.
Object-Oriented Software Engineering
Multithreading Chapter 23.
Multithreading.
Thread Synchronization
Multithreading.
Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619.
Chapter 15 Multithreading
NETWORK PROGRAMMING CNET 441
Threads and Multithreading
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Li Tak Sing (Lecture 3) COMPS311F 1

Flexible synchronization with locks Synchronized methods and statement blocks: Advantages: Relatively easy to use and understand Once a thread has entered the method or the statement block, no other thread is allowed to enter until the first threads has finished with the method. You can imagine that there is an implicit locking action at the beginning of the method and an implicit unlocking action at the end of the method. Some people also refer to the lock and unlock actions as acquiring a lock and releasing a lock respectively. 2

Flexible synchronization with locks Disadvantages: However, the implicit locking and unlocking with the use of the synchronized keyword may not be adequate for the creation of advanced multi-threaded programs. For example, you cannot specify the maximum time you want to wait to acquire a lock. You cannot perform lock and unlock actions on the same resource in two different methods. 3

Locks Java provides the features of explicit locking and unlocking with the class Lock. A lock is an instance of the Lock interface, which declares the methods for acquiring and releasing locks: void lock(); //Acquires the lock void unlock(); //Releases the lock Condition newCondition(); // Condition A condition presents a physical condition that a thread has to wait if it is not ok to proceed. For example, if you are using a fixed size buffer, a condition represents the case when it is full. The other condition represents the case when it is empty. 4

ReentrantLock The ReentrantLock is a concrete implmentation of Lock. There are two constructors: ReentrantLock(); ReentrantLock(boolean fair); The second constructor allows the setting of the fairness policy. If it is true, then threads waiting for longer period will have higher priority in acquiring the lock. Otherwise, the waiting time is not used in selecting the one to go ahead with the lock. The first constructor is the same as the second one with fair equals to false. 5

Fairness Programs using fair locks accessed by many threads may have poorer overall performance than those using the default setting, but have smaller variances in times to obtain locks and prevent starvation. 6

An example using synchronized methods public class Account { private int balance; synchronized public int balance() {return balance;} synchronized public void setBalance(int b) { balance=b;} The above example is one that uses synchronized method. Now, we can change it to one that uses Lock. 7

An example using locks public class Account { private int balance; private ReentrantLock lock=new ReentrantLock(); public int balance() { try { lock.lock(); return balance; } finally { lock.unlock(); } 8

An example using locks(cont.) public void setBalance(int b) { lock.lock(); balance=b; lock.unlock();} 9

Condition A thread can specify what to do under a certain condition. Conditions are objects created by invoking the newCondition() method on a Lock object. Condition cond=lock.newCondition(); Once a condition is created, you can use its await(), signal(), and signalAll() methods for thread communications. void await(); //Causes the current thread to wait until the condition is signaled. void signal(); //Wakes up one waiting thread. void signalAll(); //Wakes up all waiting threads. 10

Fixed size buffer example public class Buffer { private ReentrantLock lock = new ReentrantLock(); private Condition full = lock.newCondition(); private Condition empty = lock.newCondition(); private int data[] = new int[10]; private int no = 0; 11

public void put(int i) { lock.lock(); try { while (no == 10) { try { full.await(); } catch (Exception e) { } data[no++]=i; empty.signal(); } finally { lock.unlock(); } 12

public int get() { lock.lock(); try { while (no==0) {try {empty.await();} catch (Exception e) {}} full.signal(); return data[--no]; } finally { lock.unlock(); } } 13

Avoiding deadlocks Sometimes two or more threads need to acquire the locks on several shared objects. This could cause a deadlock, in which each thread has the lock on one of the objects and is waiting for the lock on the other objects. 14

Avoiding deadlocks Deadlock is easily avoided by using a simple technique known as resource ordering. With this technique, you assign an order to all the objects whose locks must be acquired and ensure that each thread acquires the locks in that order. 15

A case study: ProgressBarDemo We will demonstrate a program which find all the prime numbers under A progress bar will be used to show the progress. The program can be accessed at 010/build/classes/ProgressBar.html There are two threads in the programs: A thread for finding the prime numbers. A thread for setting the value of the progress bar. 16

Attributes of ProgressBar You can get the source code at: 0/src/ProgressBar.java We are using a JApplet so that we can demonstrate it over the Web. ProgressBar has the following attributes: int no; // this is the current integer being processed int max; //this is the maximum integer to be processed. JProgressBar; //the progressBar Vector primes; //found primes 17

Thread that find prime numbers th = new Thread() { public void run() { for (no = 2; no < max; no++) { int i; for (i = 0; i < primes.size(); i++) { if (no % primes.elementAt(i) == 0) { break; } 18

Thread that find prime numbers if (i == primes.size()) { primes.add(no); if (primes.size() % 10 == 0) { area.append("\n"); } area.append(no + " "); } }; 19

Thread that update the progress bar Thread th = new Thread() { public void run() { try { do { bar.setValue(no); Thread.sleep(100); ProgressBar.this.repaint(); } while (no<max); } catch (InterruptedException e) { } }; 20

Growing circles example In this example, when the user clicks on the window of the program, a circle of size 0 is created at that point. Then, the radius of the circle increases at a rate of 10 per second. When the circle touches the border of the window, it will disappear. 21

Growing circles example You can try the program at: 010/build/classes/GrowingCircle.html The source can be found at 010/src/GrowingCircle.java 22

Inner classes of GrowingCircle It has two inner classes: Circle: this class represent a circle. It is a subclass of thread. The thread will increase the radius of the circle by 10 every second. MyPanel: this is the drawing panel. It also have a thread which will repaint itself 10 times a second. 23

Attributes of GrowingCircle MyPanel panel; //the drawing panel Vector circles; //all created circles. 24

The inner class Circle The declaration of the class: private class Circle extends Thread The attributes: int x, y; //x,y coordinates of the circle int radius; //radius of the circle 25

The constructor of Circle Circle(int x, int y) { this.x = x; this.y = y; radius = 0; synchronized (circles) { circles.add(this); } start(); } 26

The run method of Circle public void run() { try { while (true) { radius += 1; int height = panel.getHeight(); int width = panel.getWidth(); if (y + radius > height || y - radius width || x - radius < 0) { synchronized (circles) { circles.remove(this); } 27

The run method of Circle } Thread.sleep(100); } } catch (InterruptedException e) { } 28

The inner class MyPanel The declaration: private class MyPanel extends JPanel { 29

The constructor of MyPanel public MyPanel() { Thread th = new Thread() { public void run() { try { while (true) { repaint(); Thread.sleep(100); } } catch (InterruptedException e) { } }; th.start(); } 30

The mouseClicked method of the mouse listener private void panelMouseClicked(java.awt.event.MouseEvent evt) { Circle c = new Circle(evt.getX(), evt.getY()); panel.repaint(); } 31