作者:jiangzh | 来源:互联网 | 2023-10-17 18:34
Java编程思想一书中第21章并发中关于线程间协作的一节中有个关于汽车打蜡与抛光的小例子(原书的704页)。这个例子主要展示的是两个线程如何通过wait与notify/notifyAll方法waxOn的Flag让打蜡线程与抛光线程依次顺序反复运行。
class Car {private boolean waxOn = false;synchronized void waxed() {waxOn = true;System.out.println("waxOn = true");notify();}synchronized void buffed() {System.out.println("waxOn = false");waxOn = false;notify();}synchronized void waitForWaxing() {try {while (!waxOn) {wait();}} catch (InterruptedException e) {e.printStackTrace();}}synchronized void waitBuffing() {try {while (waxOn) {wait();}} catch (InterruptedException e) {e.printStackTrace();}}}
class WaxOn implements Runnable {private Car car;WaxOn(Car car) {this.car = car;}@Overridepublic void run() {while (!Thread.interrupted()) {car.waitBuffing();car.waxed();}}
}
class WaxOff implements Runnable {private Car car;WaxOff(Car car) {this.car = car;}@Overridepublic void run() {while (!Thread.interrupted()) {car.waitForWaxing();car.buffed();}}
}
public class Test {public static void main(String[] args) {Car car = new Car();ExecutorService executorService = Executors.newCachedThreadPool();executorService.execute(new WaxOn(car));executorService.execute(new WaxOff(car));try {TimeUnit.MILLISECONDS.sleep(30);} catch (InterruptedException e) {e.printStackTrace();}executorService.shutdownNow();}
}
waxOn = true
waxOn = false
waxOn = true
waxOn = false
waxOn = true
waxOn = false
新浪的一道笔试题
1、有一个数组存储了 0 ~ 19 这20个数字;
2、有三个线程的名字分别是Thread0,Thread1,Thread2;
3、启动这三个线程顺序的从数组中取出一个数字打印出来;
要求:
1、Thread0 必须第一个获取数据打印;
2、同一时刻只有一个线程在运行;
3、线程的执行是有顺序的 Thread0->Thread1->Thread2->Thread0->Thread1->Thread2
abstract class Step {int allSteps;int stepNow;abstract public void doStep(int threadIndex);abstract public void waitForPreStep(int stepNow) throws InterruptedException;
}
class WaitStep extends Step{private int totalNum &#61; 0;&#64;Overridepublic synchronized void doStep(int threadIndex) {if (stepNow &#61;&#61; allSteps) {stepNow &#61; 0;} else {&#43;&#43; stepNow;}if (totalNum <20) {System.out.println("Thread :" &#43; threadIndex &#43; " num:" &#43; totalNum &#43;&#43;);}notifyAll();}&#64;Overridepublic synchronized void waitForPreStep(int stepN) throws InterruptedException {while (stepN !&#61; stepNow) {wait();}}
}
class RunnableTest implements Runnable {private int threadIndex;private Step step;RunnableTest(int index, Step step) {this.threadIndex &#61; index;this.step &#61; step;}&#64;Overridepublic void run() {int runTime &#61; 20;while (runTime -- > 0) {try {step.waitForPreStep(this.threadIndex);} catch (InterruptedException e) {e.printStackTrace();}step.doStep(this.threadIndex);}}
}
public class Test {public static void main(String[] args) {WaitStep waitStep &#61; new WaitStep();waitStep.stepNow &#61; 0;waitStep.allSteps &#61; 2;ExecutorService executorService &#61; Executors.newCachedThreadPool();executorService.execute(new RunnableTest(0, waitStep));executorService.execute(new RunnableTest(1, waitStep));executorService.execute(new RunnableTest(2, waitStep));executorService.shutdown();}
}
运行结果&#xff1a;
Thread :0 num:0
Thread :1 num:1
Thread :2 num:2
Thread :0 num:3
Thread :1 num:4
Thread :2 num:5
Thread :0 num:6
Thread :1 num:7
Thread :2 num:8
Thread :0 num:9
Thread :1 num:10
Thread :2 num:11
Thread :0 num:12
Thread :1 num:13
Thread :2 num:14
Thread :0 num:15
Thread :1 num:16
Thread :2 num:17
Thread :0 num:18
Thread :1 num:19