Sei sulla pagina 1di 4

Monitors in Java

First alternative:
To implement a monitor in Java, you must create a new class and use Lock and Condition classes.
Lock is the interface is ReentrantLock is the main used implementation. A ReentrantLock has two
constructors, a default constructor and a constructor with a boolean argument indicating if the lock is
fair or not. A fair lock indicates that the threads will acquire the locks in the order they ask for. airness
is a little heavier than default locking strategies, so use it only if you need it. To acquire the lock, you
!ust have to use the method lock and unlock to release it.
The e"plicit locks have the same memory semantics than the synchroni#ed blocks. $o the visibility of
the changes is guarantee when you use lock%&'unlock%& blocks.
To implement a monitor, you !ust need to create a class and use the lock to make the mutual e"clusion (
public class Monitor {
private data;
private final Lock lock = new ReentrantLock();
public Monitor(){} // constructor, init private data
public void A() {
locklock();
tr! {
// "et#od code
} finall! {
lockunlock();
}
}
public void $() {
locklock();
tr! {
// "et#od code
} finall! {
lockunlock();
}
}
}
Adding Conditional Variables:
%ou can create conditions usin& t#e newCondition "et#od on t#e lock A condition
is a variable of t!pe Condition %ou can "ake t#e current t#read wait on t#e
condition usin& t#e await "et#od (and its variant wit# ti"eout) and !ou can si&nal
t#reads usin& signal and signalAll "et#ods '#e si&nalAll "et#ods wakes up all t#e
t#reads waitin& on t#e condition variable
public class Monitor {
private data;
private final Lock lock = new ReentrantLock();
private final (ondition c) = locknew(ondition();
private final (ondition c* = locknew(ondition();
public Monitor(){} // constructor, init private data
public void A() t#rows +nterrupted,-ception {
locklock();
tr! {
//(ode involvin& c) or c*, includin& waitin& for a condition or
si&nalin& anot#er t#read of a condition t#at anot#er t#read is waitin& for
} finall! {
lockunlock();
}
}
public void $() t#rows +nterrupted,-ception {
locklock();
tr! {
// (ode involvin& c) or c*, includin& waitin& for a condition or
si&nalin& anot#er t#read of a condition t#at anot#er t#read is waitin& for
} finall! {
lockunlock();
}
}
}
Second Alternative:
)se the synchroni#ed keyword on the two methods. *ut with synchroni#ed, we will not have the
condition variables. +f you don,t need condition variables but only locking, it will be easier to use the
synchroni#ed blocks instead of Locks.
public class Monitor {
private data;
public Monitor(){}
public s!nc#roni.ed void A() {
// "et#od code

}
public s!nc#roni.ed void $() {
// "et#od code
}
}
/sin& s!nc#roni.ed "et#ods onl! an anon!"ous condition variable is available and
it is used as follows0
public class Monitor {
private data;
public Monitor(){}
public s!nc#roni.ed void A() t#rows +nterrupted,-ception {
tr! {
//1o"e code includin& wait() or notif!All()
} catc#(+nterrupted,-ception e){
t#row new ,rror (2,rror en wait 3ueueMonitor de4ueue 0 25 e);
}
}

public s!nc#roni.ed void $() t#rows +nterrupted,-ception {
tr! {
//1o"e code includin& wait() or notif!All()
} catc#(+nterrupted,-ception e){
t#row new ,rror (2,rror en wait 3ueueMonitor de4ueue 0 25 e);
}
}
Using threads with a Monitor
public class 6ebra e-tends '#read {
private Monitor ";
public 6ebra(Monitor "7){
t#is" = "7;
}
public void run(){
// call t#e Monitor "et#ods to access s#ared resource
}
}
Example defining the main testing the use of threads using the monitor
public class 'est {
public static void "ain(1trin&89 ar&s){
Monitor " = new Monitor();
6ebra # = new 6ebra(");
// add "ore t#reads instances and/ot#er t!pes of t#reads t#at access
t#e resource usin& Monitor
#start(); // call run() "et#od in 6ebra class
}
}
Ejercicios en aboratorio
) +"ple"ente el proble"a de lectores/escritores creando un Monitor en :ava 4ue
priori.a los lectores ! usa ";todos sincroni.ados ! variable de condici<n an<ni"a
* Lo "is"o 4ue en ), pero use Lock reentrante ! variables de condici<n
= A#ora "e>ore el al&orit"o en * para priori.ar los escritores
? +"ple"ente el proble"a de productor/consu"idor en :ava con lock reentrante !
variables de condici<n
@ +"ple"ente un se"Aforo contador creando un Monitor en :ava Analice su
i"ple"entaci<n

Potrebbero piacerti anche