Java introduced Semaphore since 1.5, let's see how we can use it to maintain an order for thread access.
Suppose we have the following code:
class Foo {
public:
A(.....);
B(.....);
}
Foo f;
f.A(.....);
f.B(.....);
f.C(.....);
Suppose we have the following code to use class Foo. We do not know how the threads
will be scheduled in the OS.
Foo f;
f.A(.....);
f.B(.....);
f.C(.....);
f.A(.....);
f.B(.....);
f.C(.....);
i) Can you design a mechanism to make sure that all the methods will be executed in
sequence?
--------------------------------------
we can use Semaphore sem = new Semaphore(1) to init a mutex, at this time, available permit for this semaphore is 1.
we can use sem.acquire() for P operation, so the permit will be reduced to 0, any other places uses sem.acquire() will be blocked until sem.release() is called and permit is increased to 1 again. So to maintain an access order, we wanna acquire semaphore first and use predecessor to release this semaphore.
Suppose we have the following code:
class Foo {
public:
A(.....);
B(.....);
}
Foo f;
f.A(.....);
f.B(.....);
f.C(.....);
Suppose we have the following code to use class Foo. We do not know how the threads
will be scheduled in the OS.
Foo f;
f.A(.....);
f.B(.....);
f.C(.....);
f.A(.....);
f.B(.....);
f.C(.....);
i) Can you design a mechanism to make sure that all the methods will be executed in
sequence?
--------------------------------------
we can use Semaphore sem = new Semaphore(1) to init a mutex, at this time, available permit for this semaphore is 1.
we can use sem.acquire() for P operation, so the permit will be reduced to 0, any other places uses sem.acquire() will be blocked until sem.release() is called and permit is increased to 1 again. So to maintain an access order, we wanna acquire semaphore first and use predecessor to release this semaphore.
public class SemaphoreTest {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
final Semaphore sem_c = new Semaphore(1);
final Semaphore sem_b = new Semaphore(1); //permit is 1
final Semaphore sem_all = new Semaphore(1);
System.out.println("first, sem_b permit: " + sem_b.availablePermits());
System.out.flush();
sem_b.acquire(); //permit from 1 to 0
System.out.println("after acquire, sem_b permit: " + sem_b.availablePermits());
System.out.flush();
sem_c.acquire();
sem_all.acquire();
A(sem_b);
B(sem_c, sem_b);
C(sem_c, sem_all, sem_b);
// System.out.println("second round");
System.out.flush();
System.out.println("sem_all permit: " + sem_all.availablePermits());
sem_all.acquire();
System.out.println("sem_b permit: " + sem_b.availablePermits());
sem_b.acquire();
sem_c.acquire();
//
A(sem_b);
B(sem_c, sem_b);
C(sem_c, sem_all, sem_b);
}
private static void C(final Semaphore sem_c, final Semaphore sem_all, final Semaphore sem_b) {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
sem_c.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("c");
System.out.flush();
sem_c.release();
// System.out.println("sem_all permit: " + sem_all.availablePermits());
sem_all.release();
}}).start();
}
private static void B(final Semaphore sem_c, final Semaphore sem_b) {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(1000);
sem_b.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sem_b.release();
System.out.println("b");
System.out.flush();
sem_c.release();
}}).start();
}
private static void A(final Semaphore sem_b) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("a");
System.out.flush();
// System.out.println("sem_b permit: " + sem_b.availablePermits());
sem_b.release();
// System.out.println("sem_b permit after: " + sem_b.availablePermits());
System.out.flush();
}}).start();
}
}