java 线程协作

package thead;

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

public class ThreadWaitAndNotifyTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Car car=new Car();
        ExecutorService es=Executors.newCachedThreadPool();
        es.execute(new WaxOff(car));
        es.execute(new WaxOn(car));
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        es.shutdownNow();

    }

}

class Car {
    private boolean waxOn = false;

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

    public synchronized void buffed() {
        waxOn = false;
        notify();
    }

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

    public synchronized void waitForBuffing() throws InterruptedException {
        while (waxOn) {
            wait();
        }
    }
}

class WaxOn implements Runnable {

    private Car car;

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

    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
                System.out.println("wax on");

                //TimeUnit.MILLISECONDS.sleep(200);
                car.waxed();
                car.waitForBuffing();
            }
        } catch (InterruptedException e) {
            System.out.println("结束");
            //e.printStackTrace();
        }
        System.out.println("ending wax task");
    }

}

class WaxOff implements Runnable {

    private Car car;

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

    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {

                car.waitForWaxing();
                System.out.println("wax off");
                //TimeUnit.MILLISECONDS.sleep(200);
                car.buffed();
            }
        } catch (InterruptedException e) {
            System.out.println("结束");
            //e.printStackTrace();
        }
        System.out.println("ending wax task");

    }

}

 

 

package thead;

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.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadWaitAndNotifyTest1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ExecutorService es = Executors.newCachedThreadPool();
        //Resource r = new Resource();
        resourceLock r1 = new resourceLock();
        es.execute(new Task2(r1));
        es.execute(new Task1(r1));
        try {
            TimeUnit.MILLISECONDS.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(es.shutdownNow());
        // es.shutdown();
        System.out.println("任务圆满结束");

    }

}

class Task1 implements Runnable {
    private resourceLock r;

    public Task1(resourceLock r) {
        this.r = r;
    }

    @Override
    public void run() {

        while (!Thread.interrupted()) {
            r.baoJiaozi();// 执行包饺子
            // TimeUnit.MILLISECONDS.sleep(2);
            try {
                r.waitZhuJiaozi();// 等待煮饺子
            } catch (InterruptedException e) {
                System.out.println("包饺子任务被关闭了");
                break;
            }
        }

    }

}

class Task2 implements Runnable {

    private resourceLock r;

    public Task2(resourceLock r) {
        this.r = r;
    }

    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {

                r.zhuJiaozi();// 执行煮饺子
                // TimeUnit.MILLISECONDS.sleep(2);
                r.waitBaoJiaozi();// 等待包饺子
            }
        } catch (InterruptedException e) {
            System.out.println("煮饺子任务被关闭了");
        }

    }

}

class Resource {
    private boolean status = false;// false包饺子可以

    public synchronized void baoJiaozi() {
        status = true;
        System.out.println("包饺子");
        notifyAll();
    }

    public synchronized void zhuJiaozi() {
        status = false;
        System.out.println("煮饺子");
        notifyAll();
    }

    public synchronized void waitBaoJiaozi() throws InterruptedException {

        while (!status) {
            wait();
        }

    }

    public synchronized void waitZhuJiaozi() throws InterruptedException {

        while (status) {
            wait();
        }

    }
}

class resourceLock {
    private boolean status = false;// false包饺子可以
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void baoJiaozi() {
        lock.lock();
        try {
            status = true;
            System.out.println("包饺子");
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }

    public void zhuJiaozi() {
        lock.lock();
        try {
            status = false;
            System.out.println("煮饺子");
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }

    public void waitBaoJiaozi() throws InterruptedException {
        lock.lock();
        try {
            while (!status) {
                condition.await();
            }
        } finally {
            lock.unlock();
        }

    }

    public void waitZhuJiaozi() throws InterruptedException {
        lock.lock();
        try {
            while (status) {
                condition.await();
            }
        } finally {
            lock.unlock();
        }
    }
}

 

 

 

转载于:https://my.oschina.net/jiangtao1314/blog/40270

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值