http://cse.unl.edu/~ylu/csce351/notes/Two-Alternative-Solutions-for-Building-H2O.pdf
http://www.cs.berkeley.edu/~kubitron/cs162/hand-outs/synch-solutions.html
Building H2O
https://paulamarcu.wordpress.com/2014/08/03/program-h2o/
http://www.mitbbs.com/article_t/JobHunting/32348515.html
http://www.mitbbs.com/article_t/JobHunting/32331973.html
http://shanhe.me/2015/03/13/implement-an-h-method-and-an-o-method-to-produce-a-water-molecular
实现两个函数: H() and O(), 这两个函数会被多线程调用。当一个线程调用H或O时
,如果当前已经有至少两个线程call H和一个线程call O。那么让两个call H和一个
call O的线程返回(产生一个水分子),其他的都block。
对Lock不是很熟,请教一下,一个thread call H2O.h(),LOCK就被take了,那其他的thread就没办法call h()了吧?那H就一直为1?
Read full article from LinkedIn: H2O
http://www.cs.berkeley.edu/~kubitron/cs162/hand-outs/synch-solutions.html
Building H2O
- Correctness constraints
- Each hydrogen thread waits to be grouped with one other hydrogen and oxygen before returning
- Each oxygen thread waits for two other hydrogens before returning
- Only one thread access shared state at a time
- There is only one condition any thread will wait for, i.e. a water molecule being formed. However, it will be necessary to signal hydrogen and oxygen threads independently, so we will use two condition variables, waitingH and waitingO.
- It will be necessary to know the number of hydrogen and oxygen threads in the monitor. But it would be more useful to know how many hydrogen and oxygen threads have been assigned and have not been assigned to water molecules; let these be int wH (number of waiting hydrogens), wO (number of waiting oxygens),aH (number of assigned hydrogens), and aO (number of assigned oxygens). These are all initialized to 0.
https://paulamarcu.wordpress.com/2014/08/03/program-h2o/
http://www.mitbbs.com/article_t/JobHunting/32348515.html
http://www.mitbbs.com/article_t/JobHunting/32331973.html
http://shanhe.me/2015/03/13/implement-an-h-method-and-an-o-method-to-produce-a-water-molecular
Both methods are to be called by multiple threads. Each thread calling either method becomes blocking and when one thread finds that there are already at least two threads calling H() and at least one thread calling O(), let two threads calling H() and one thread calling O() resume from blocking in order to produce a water molecular.
LinkedIn: H2O实现两个函数: H() and O(), 这两个函数会被多线程调用。当一个线程调用H或O时
,如果当前已经有至少两个线程call H和一个线程call O。那么让两个call H和一个
call O的线程返回(产生一个水分子),其他的都block。
对Lock不是很熟,请教一下,一个thread call H2O.h(),LOCK就被take了,那其他的thread就没办法call h()了吧?那H就一直为1?
public
class
H2O {
static
final
Lock LOCK =
new
ReentrantLock();
static
final
Condition ENOUGH_H = LOCK.newCondition();
static
final
Condition ENOUGH_O = LOCK.newCondition();
static
int
H =
0
;
static
int
O =
0
;
static
void
check() {
if
(H >=
2
&& O >=
1
) {
ENOUGH_H.signal();
ENOUGH_H.signal();
ENOUGH_O.signal();
H -=
2
;
O -=
1
;
}
}
public
static
void
h() {
LOCK.lock();
try
{
check();
++H;
ENOUGH_H.awaitUninterruptibly();
}
finally
{
LOCK.unlock();
}
}
public
static
void
o() {
LOCK.lock();
try
{
check();
++O;
ENOUGH_O.awaitUninterruptibly();
}
finally
{
LOCK.unlock();
}
}
}`
public class H2O {
static final int H = 0;
static final int O = 1;
static final Lock LOCK = new ReentrantLock();
static final Condition ENOUGH[] = {LOCK.newCondition(), LOCK.newCondition()};
static final int NEEDED[] = {2, 1};
static final int COUNT[] = {0, 0};
static void run(int index) {
LOCK.lock();
try {
if(COUNT[H] >= NEEDED[H] && COUNT[O] >= NEEDED[O]) {
for(int i = 0, n = NEEDED[H]; i < n; ++i) {
ENOUGH[H].signal();
}
COUNT[H] -= NEEDED[H];
for(int i = 0, n = NEEDED[O]; i < n; ++i) {
ENOUGH[O].signal();
}
COUNT[O] -= NEEDED[O];
}
++COUNT[index];
ENOUGH[index].awaitUninterruptibly();
} finally {
LOCK.unlock();
}
}
public static void h() {
run(H);
}
public static void o() {
run(O);
}
}
Read full article from LinkedIn: H2O