如何使java中的线程按顺序1运行 - > 2 - > 3 - > 1 - > 2 - > 3等

时间:2016-04-07 19:26:42

标签: java multithreading

我有一个任务,我有3个线程:Wax,Polish和Wash。 我必须让程序按顺序运行 - >抛光 - >洗过 - >打蜡 - >抛光 - >洗了等。

这是我的代码

package konsumencitest;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

class Car {
    public boolean waxOn = false;

    public synchronized void waxed() {
        waxOn = true;
        notifyAll();
    }

    public synchronized void polished() {
        waxOn = false;
        notifyAll();
    }

        public synchronized void washed() {
        waxOn = false;
        notifyAll();
    }

    public synchronized void waitForWaxing() throws InterruptedException {
        while (waxOn = false) {
            wait();
        }
    }

    public synchronized void waitForPolishing() throws InterruptedException {
        while (waxOn = true) {
            wait();
        }
    }

        public synchronized void waitForWashing() throws InterruptedException {
        while (waxOn = true) {
            wait();
        }
    }
}

class Wax implements Runnable {
    private Car car;

    public Wax(Car car) {
        this.car = car;
    }

    public void run() {
        try {
            while (!Thread.interrupted()) {
                System.out.print("Waxed!");
                TimeUnit.MILLISECONDS.sleep(200);
                car.waxed();
                car.waitForPolishing();
            }
        } catch (InterruptedException e) {
            System.out.println("Existing via interrupt");
        }
        System.out.println("Ending Wax on task");
    }
}

class Polish implements Runnable {
    private Car car;

    public Polish(Car car) {
        this.car = car;
    }

    public void run() {
        try {
            while (!Thread.interrupted()) {
                car.waitForWaxing();
                System.out.print("Polished!");
                TimeUnit.MILLISECONDS.sleep(200);
                car.polished();
                                car.waitForWashing();
            }
        } catch (InterruptedException e) {
            System.out.println("Exiting via interrupt");
        }
        System.out.println("Ending Wax off task");
    }
}

class Wash implements Runnable {
    private Car car;

    public Wash(Car car) {
        this.car = car;
    }

    public void run() {
        try {
            while (!Thread.interrupted()) {
                car.waitForWaxing();
                System.out.print("Washed!");
                TimeUnit.MILLISECONDS.sleep(200);
                car.washed();
                                car.waitForWaxing();


            }
        } catch (InterruptedException e) {
            System.out.println("Exiting via interrupt");
        }
        System.out.println("Ending Wax off task");
    }
}

public class WaxOMatic {
    public static void main(String[] args) throws InterruptedException {
        Car car = new Car();
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(new Wax(car));
        service.execute(new Polish(car));
                service.execute(new Wash(car));
        TimeUnit.SECONDS.sleep(5);
        service.shutdownNow();
    }
}

但是输出是“打蜡!擦亮!洗了!洗了!洗了!洗了!洗了!洗了!洗了!洗了!洗了!洗了!洗了!洗了!洗了!洗了!洗了!洗了!洗了!洗了!等等。”。

如何解决?

0 个答案:

没有答案