学完操作系统,习惯用信号量了
class Foo {
Semaphore s1, s2;
public Foo() {
s1 = new Semaphore(0);
s2 = new Semaphore(0);
}
public void first(Runnable printFirst) throws InterruptedException {
printFirst.run();
s1.release();
}
public void second(Runnable printSecond) throws InterruptedException {
s1.acquire();
printSecond.run();
s2.release();
}
public void third(Runnable printThird) throws InterruptedException {
s2.acquire();
printThird.run();
}
}

本文探讨了如何在操作系统中使用信号量(Semaphore)进行并发控制,通过Foo类实例展示如何在first、second和third方法间协调资源访问。理解并应用信号量有助于提升程序的同步性和效率。
783

被折叠的 条评论
为什么被折叠?



