多线程编程题目:使用三个线程有序输出“1,2,3,1,2,3。。。”,其中线程1只输出1,线程2只输出2,线程3只输出3

这个问题用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
*///:~



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陆业聪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值