Title: COMP 346
1COMP 346 OPERATING SYSTEMS
Tutorial 4
http//www.cs.concordia.ca/k_karthi/comp346.html
2Topics
- Lab hours
- Threads Synchronizing
- Threads - Producer/Consumer Example
- Using notifyAll() and wait() methods
- Implementing Semaphores in Java
- Avoiding Starvation and Deadlock
- References
3Lab hours
H 815 --W-- 1815 2015 Mokhov, S.
----F 1400 1600 Mia, M. H
817 -T--- 1550 1740 Wahedunnabi,
M.A.F. ---J- 1800 2000 Akon, M.
4Threads - Synchronizing
Synchronized keyword. To make programs threa
d-safe, you must identify what data will be
shared across threads. For example, imagine a Jav
a application where one thread (the producer)
writes data to a file while a second thread
(consumer) reads data from the file. As the
threads share a common resource, they must be
synchronized in some way.
This is the general form of the synchronized
statement Synchronized (object) // statements
to be synchronized Here, object is a referenc
e to the object being synchronized.
5The Producer/Consumer Example
The Producer generates an integer between 0 and
9, stores it in cubbyHole object, and prints the
generated number. The Producer sleeps for random
amount of time before repeating the number
generating cycle.
public Producer(CubbyHole c, int number)
cubbyhole c this.number number
public void run() for (int i 0 i i) cubbyhole.put(i) System.out.println
("Producer " this.number " put " i)
try sleep((int)(Math.random() 100)) cat
ch (InterruptedException e)
public Consumer(CubbyHole c, int number)
cubbyhole c this.number number
public void run() int value 0 for
(int i 0 i get() System.out.println("Consumer " th
is.number " got " value)
6The Producer/Consumer example contd..
public class CubbyHole private int conten
ts private boolean available false pub
lic synchronized int get() while (av
ailable false) try
wait() catch (InterruptedException
e) available false notify
All() return contents public syn
chronized void put(int value)
while (available true)
try wait() catch
(InterruptedException e) conte
nts value available true notif
yAll()
MAIN PROGRAM public class ProducerConsumerT
est public static void main(String args)
CubbyHole c new CubbyHole() Pro
ducer p1 new Producer(c, 1)
Consumer c1 new Consumer(c, 1)
p1.start() c1.start()
cubbyHole
7Using notifyAll and wait methods
public class CubbyHole private int conten
ts private boolean available false pub
lic synchronized int get() while (av
ailable false) try
wait() catch (InterruptedException
e) available false notify
All() return contents public syn
chronized void put(int value)
while (available true)
try wait() catch
(InterruptedException e) conte
nts value available true notif
yAll()
// wait for Producer to put value wait()
// notify Producer that value has been retrieved
notifyAll()
// wait for Consumer to get value wait()
// notify Consumer that value has been set
8Implementing Semaphores in Java
class Semaphore private int count public Se
maphore(int n) this.count n pub
lic synchronized void WAIT() while(coun
t 0) try wait()
catch(InterruptedException e)
count--
public s
ynchronized void SIGNAL() count
notify()
9Avoiding Starvation and Deadlock
The dining philosophers are often used to
illustrate various problems that can occur when
many synchronized threads are competing for
limited resources.
The Dining Philosophers Problem
Five philosophers are sitting at a round table.
In front of each philosopher is a bowl of rice.
Between each pair of philosophers is one
chopstick. Before an individual philosopher can
take a bite of rice he must have two
chopsticks--one taken from the left, and one
taken from the right. The philosophers must find
some way to share chopsticks such that they all
get to eat.
The best choice is to prevent deadlock rather
than to try and detect it. Deadlock detection is
complicated. The simplest approach to preventing
deadlock is to impose ordering on the condition
variables.
10References
The Producer/Consumer Example is available in the
link http//java.sun.com/docs/books/tutorial/esse
ntial/threads/multithreaded.html
Book for examples on Multithreading
Java 2 The Complete Reference, Patrick Naughton,
Herbert Schildt