这个问题用java中的管程的语言概念比较容易实现,类似的实现可以参考《java编程思想》中多线程协作的WaxOMatic例子。
public class Test {
public static void main(String args[]) {
Monitor car = new Monitor();
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(new Print3Task(car));
exec.execute(new Print2Task(car));
exec.execute(new Print1Task(car));
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // Run for a while...
exec.shutdownNow(); // Interrupt all tasks
}
}
class Monitor {
private boolean hasPrint1,hasPrint2,hasPrint3;
public synchronized void wairForPrint1() throws InterruptedException{
while(hasPrint1 == false)
wait();
}
public synchronized void wairForPrint2() throws InterruptedException{
while(hasPrint2 == false)
wait();
}
public synchronized void wairForPrint3() throws InterruptedException{
while(hasPrint3 == false)
wait();
}
public synchronized void print1(){
System.out.print("1,");
hasPrint1 = true;
hasPrint2 = false;
hasPrint3 = false;
notifyAll();
}
public synchronized void print2(){
System.out.print("2,");
hasPrint1 = false;
hasPrint2 = true;
hasPrint3 = false;
notifyAll();
}
public synchronized void print3(){
System.out.println("3,");
hasPrint1 = false;
hasPrint2 = false;
hasPrint3 = true;
notifyAll();
}
}
class Print1Task implements Runnable {
private Monitor monitor;
public Print1Task(Monitor monitor){
this.monitor = monitor;
}
public void run() {
while(!Thread.interrupted()) {
monitor.print1();
try {
monitor.wairForPrint3();
} catch (InterruptedException e) {}
}
}
}
class Print2Task implements Runnable {
private Monitor monitor;
public Print2Task(Monitor monitor){
this.monitor = monitor;
}
public void run() {
while(!Thread.interrupted()) {
try {
monitor.wairForPrint1();
} catch (InterruptedException e) {}
monitor.print2();
}
}
}
class Print3Task implements Runnable {
private Monitor monitor;
public Print3Task(Monitor monitor){
this.monitor = monitor;
}
public void run() {
while(!Thread.interrupted()) {
try {
monitor.wairForPrint2();
} catch (InterruptedException e) {}
monitor.print3();
}
}
}
管程类Monitor通过synchronized关键字同步。
话说这是快的打车今年的笔试题,我做出来还被刷了,真是不知道怎么评判的。
顺便附上java编程思想中的例子,print之类的函数是作者的函数库,自己改一下也可以运行。这是两个线程协作的例子。
class Car {
private boolean waxOn = false;
public synchronized void waxed() {
waxOn = true; // Ready to buff
notifyAll();
}
public synchronized void buffed() {
waxOn = false; // Ready for another coat of wax
notifyAll();
}
public synchronized void waitForWaxing()
throws InterruptedException {
while(waxOn == false)
wait();
}
public synchronized void waitForBuffing()
throws InterruptedException {
while(waxOn == true)
wait();
}
}
class WaxOn implements Runnable {
private Car car;
public WaxOn(Car c) { car = c; }
public void run() {
try {
while(!Thread.interrupted()) {
printnb("Wax On! ");
TimeUnit.MILLISECONDS.sleep(200);
car.waxed();
car.waitForBuffing();
}
} catch(InterruptedException e) {
print("Exiting via interrupt");
}
print("Ending Wax On task");
}
}
class WaxOff implements Runnable {
private Car car;
public WaxOff(Car c) { car = c; }
public void run() {
try {
while(!Thread.interrupted()) {
car.waitForWaxing();
printnb("Wax Off! ");
TimeUnit.MILLISECONDS.sleep(200);
car.buffed();
}
} catch(InterruptedException e) {
print("Exiting via interrupt");
}
print("Ending Wax Off task");
}
}
public class WaxOMatic {
public static void main(String[] args) throws Exception {
Car car = new Car();
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(new WaxOff(car));
exec.execute(new WaxOn(car));
TimeUnit.SECONDS.sleep(5); // Run for a while...
exec.shutdownNow(); // Interrupt all tasks
}
} /* Output: (95% match)
Wax On! Wax Off! Wax On! Wax Off! Wax On! Wax Off! Wax On! Wax Off! Wax On! Wax Off! Wax On! Wax Off! Wax On! Wax Off! Wax On! Wax Off! Wax On! Wax Off! Wax On! Wax Off! Wax On! Wax Off! Wax On! Wax Off! Wax On! Exiting via interrupt
Ending Wax On task
Exiting via interrupt
Ending Wax Off task
*///:~