[CareerCup] 16.5 Semphore 信号旗
?
16.5 Suppose we have the following code:
public class Foo {
public Foo() { . . . }
public void first() { ... }
public void second() { ... }
public void thirdQ { ... }
}
The same instance of Foo will be passed to three different threads. ThreadA will call first, threads will call second, and threadC will call third. Design a mechanism to ensure that first is called before second and second is called before third.
?
按照題目中要求的順序即需滿足在執(zhí)行second()前檢測first()是否完成了,在執(zhí)行third()前檢測second()是否完成了,由于我們需要考慮線程安全,所以布爾型的標記不行,那么使用鎖呢,看如下代碼:
?
import java.util.concurrent.locks.ReentrantLock;public class FooBad {public int pauseTime = 10000;public ReentrantLock lock1;public ReentrantLock lock2;public FooBad() {try {lock1 = new ReentrantLock();lock2 = new ReentrantLock();lock1.lock();lock2.lock();} catch (Exception ex) {ex.printStackTrace();}}public void first() {try {System.out.println("Started Executing 1");Thread.sleep(pauseTime);System.out.println("Finished Executing 1");lock1.unlock();} catch (Exception ex) {ex.printStackTrace();}}public void second () {try {lock1.lock();lock1.unlock();System.out.println("Started Executing 2");Thread.sleep(pauseTime);System.out.println("Finished Executing 2");lock2.unlock();} catch (Exception ex) {ex.printStackTrace();}}public void third() {try {lock2.lock();lock2.unlock();System.out.println("Started Executing 3");Thread.sleep(pauseTime);System.out.println("Finished Executing 3");} catch (Exception ex) {ex.printStackTrace();}} }public class MyThread extends Thread {private String method;private FooBad foo;public MyThread(FooBad foo, String method) {this.method = method;this.foo = foo;}public void run() {if (method == "first") {foo.first();} else if (method == "second") {foo.second();} else if (method == "third") {foo.third();}} }public class j {public static void main(String[] args) {FooBad foo = new FooBad();MyThread thread1 = new MyThread(foo, "first");MyThread thread2 = new MyThread(foo, "second");MyThread thread3 = new MyThread(foo, "third");thread3.start();thread2.start();thread1.start();} }?
上述代碼并不能很好的完成題目中要求的順序,因為鎖的所有權的問題。一個線程操作一個鎖,當別的線程是無法解這個線程的鎖的,所以用鎖是不行的。我們可以使用信號旗Semaphores,參見代碼如下:
?
import java.util.concurrent.Semaphore; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;public class Foo {public int pauseTime = 1000;public Semaphore sem1;public Semaphore sem2;public Foo() {try {sem1 = new Semaphore(1);sem2 = new Semaphore(1);sem1.acquire();sem2.acquire();} catch (InterruptedException e) {e.printStackTrace();}}public void first() {try {System.out.println("Started Executing 1");Thread.sleep(pauseTime);System.out.println("Finished Executing");sem1.release();} catch (Exception ex) {ex.printStackTrace();}}public void second() {try {sem1.acquire();sem1.release();System.out.println("Started Executing 2");Thread.sleep(pauseTime);System.out.println("Finished Executing 2");sem2.release();} catch (Exception ex) {ex.printStackTrace();}}public void third() {try {sem2.acquire();sem2.release();System.out.println("Started Executing 3");Thread.sleep(pauseTime);System.out.println("Finished Executing 3");} catch (Exception ex) {ex.printStackTrace();}} }public class MyThread extends Thread {private String method;private Foo foo;public MyThread(Foo foo, String method) {this.method = method;this.foo = foo;}public void run() {if (method == "first") {foo.first();} else if (method == "second") {foo.second();} else if (method == "third") {foo.third();}} }public class j {public static void main(String[] args) {Foo foo = new Foo();MyThread thread1 = new MyThread(foo, "first");MyThread thread2 = new MyThread(foo, "second");MyThread thread3 = new MyThread(foo, "third");thread3.start();thread2.start();thread1.start();} }?
CareerCup All in One 題目匯總
?
轉載于:https://www.cnblogs.com/grandyang/p/5400632.html
《新程序員》:云原生和全面數(shù)字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的[CareerCup] 16.5 Semphore 信号旗的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人工神经网络基本特点
- 下一篇: (转)搜索Maven仓库 获取 grou