同步示例

来源:互联网 发布:织梦cms邀请码 编辑:程序博客网 时间:2024/05/22 11:41
package mypack;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;/* * 一个是将蜡涂到car上,一个是将抛光它 * 抛光任务在涂蜡任务完成之前,是不能执行其他工作的 * 涂蜡任务在涂另一层蜡之前,必须等待抛光任务完成*/class Car{private boolean waxed=false;public synchronized void waxOn() throws InterruptedException{while(waxed)wait();TimeUnit.MILLISECONDS.sleep(200);waxed=true;System.out.println("Wax on");notifyAll();}public synchronized void waxOff() throws InterruptedException{while(!waxed)wait();TimeUnit.MILLISECONDS.sleep(200);waxed=false;System.out.println("wax off");notifyAll();}}class WaxOn implements Runnable{private Car car;    public WaxOn(Car car){this.car=car;}@Overridepublic void run() {// TODO Auto-generated method stubtry {while(!Thread.interrupted()){    car.waxOn();}} catch (InterruptedException e) {// TODO Auto-generated catch blockthrow new RuntimeException("Interrupt Wax on");}}}class WaxOff implements Runnable{private Car car;public WaxOff(Car car){this.car=car;}@Overridepublic void run() {// TODO Auto-generated method stubtry {while(!Thread.interrupted())car.waxOff();} catch (InterruptedException e) {// TODO Auto-generated catch blockthrow new RuntimeException("Interrupt Wax Off");}}}public class WaxOMatic {public static void main(String[] args) throws InterruptedException {// TODO Auto-generated method stub    ExecutorService exec = Executors.newCachedThreadPool();    Car car = new Car();    WaxOn waxOn = new WaxOn(car);    WaxOff waxOff = new WaxOff(car);    exec.execute(waxOn);    exec.execute(waxOff);    TimeUnit.SECONDS.sleep(5);    exec.shutdownNow();}}


package mypack;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;import javax.swing.plaf.synth.SynthSpinnerUI;/* * 一个是将蜡涂到car上,一个是将抛光它 * 抛光任务在涂蜡任务完成之前,是不能执行其他工作的 * 涂蜡任务在涂另一层蜡之前,必须等待抛光任务完成*/class Car{private ReentrantLock lock=new ReentrantLock();private Condition condition=lock.newCondition();private boolean waxed=false;public  void waxOn() throws InterruptedException{try{lock.lock();while(waxed)   condition.await();TimeUnit.MILLISECONDS.sleep(200);System.out.println("WaxOn");waxed=true;condition.signalAll();} finally{lock.unlock();}}public  void waxOff() throws InterruptedException{try{lock.lock();while(!waxed)condition.await();TimeUnit.MILLISECONDS.sleep(200);System.out.println("WaxOff");waxed=false;condition.signalAll();} finally{lock.unlock();}}}class WaxOn implements Runnable{private Car car;public WaxOn(Car car){this.car=car;}@Overridepublic void run() {// TODO Auto-generated method stubtry{while(!Thread.interrupted())car.waxOn();}catch(InterruptedException e){System.out.println("Wax on Interrupt");}System.out.println("Exit Wax on");}}class WaxOff implements Runnable{private Car car;public WaxOff(Car car){this.car=car;}@Overridepublic void run() {// TODO Auto-generated method stubtry{while(!Thread.interrupted())car.waxOff();}catch(InterruptedException e){System.out.println("Wax off interrupt");}System.out.println("Exit Wax Off");}}public class WaxOMatic {public static void main(String[] args) throws InterruptedException {// TODO Auto-generated method stub    ExecutorService exec = Executors.newCachedThreadPool();    Car car = new Car();    WaxOn waxOn = new WaxOn(car);    WaxOff waxOff = new WaxOff(car);    exec.execute(waxOn);    exec.execute(waxOff);    TimeUnit.SECONDS.sleep(5);    exec.shutdownNow();}}


0 0