Java Semaphore

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.


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();
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值