多线程间的协作

目录

一、java.long.object中的方法

二、java.util.concurrent.locks中Condition接口中的方法

一、java.long.object中的方法

wait(),使当前线程等待

notify(),唤醒一个等待线程

notifyAll(),唤醒所有等待线程

以上方法只能在同步方法或同步代码块中使用且需要抛出异常,通过同步监视器调用。

生产者消费者代码

public class Test {
    public static void main(String[] args) {
        Middle m = new Middle();
        Producter productT = new Producter(m);
        productT.setName("生产线程1");
        Customer customT = new Customer(m);
        customT.setName("消费线程1");
        customT.start();
        productT.start();
    }
}

class Producter extends Thread {
    Middle middle;

    public Producter(Middle m) {
        middle = m;
    }

    @Override
    public void run() {
        while (true) {
            try {
                sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            middle.product();
        }
    }
}

class Customer extends Thread {
    Middle middle;

    public Customer(Middle m) {
        middle = m;
    }

    @Override
    public void run() {
        while (true) {
            try {
                sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            middle.custom();
        }
    }
}

class Middle {
    private int nums = 0;

    public synchronized void product()  {
        if (nums < 100) {
            nums++;
            System.out.println(Thread.currentThread().getName() + " 生产 " + nums);
            notify();
        } else {
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public synchronized void custom() {
        if (nums > 0) {
            System.out.println(Thread.currentThread().getName() + " 消费 " + nums);
            nums--;
            notify();
        } else {
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

二、java.util.concurrent.locks中Condition接口中的方法

await(),使当前线程等待

signal(),唤醒一个等待线程

signalAll(),唤醒所有等待线程

以上方法只能在Lock对象中使用。通过调用Lock对象中的newCondition()方法创建一个Condition对象,通过该Condition对象调用以上方法。

生产者消费者代码

public class Test {
    public static void main(String[] args) {
        Middle m = new Middle();
        Producter productT = new Producter(m);
        productT.setName("生产线程1");
        Customer customT = new Customer(m);
        customT.setName("消费线程1");
        customT.start();
        productT.start();
    }
}

class Producter extends Thread {
    Middle middle;

    public Producter(Middle m) {
        middle = m;
    }

    @Override
    public void run() {
        while (true) {
            try {
                sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            middle.product();
        }
    }
}

class Customer extends Thread {
    Middle middle;

    public Customer(Middle m) {
        middle = m;
    }

    @Override
    public void run() {
        while (true) {
            try {
                sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            middle.custom();
        }
    }
}

class Middle {
    private int nums = 0;
    private Lock lock = new ReentrantLock();
    private Condition empCondition = lock.newCondition();
    private Condition fulCondition = lock.newCondition();
    public void product()  {
        lock.lock();
        if (nums < 100) {
            nums++;
            System.out.println(Thread.currentThread().getName() + " 生产 " + nums);
            fulCondition.signal();
        } else {
            try {
                empCondition.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        lock.unlock();
    }

    public void custom() {
        lock.lock();
        if (nums > 0) {
            System.out.println(Thread.currentThread().getName() + " 消费 " + nums);
            nums--;
            empCondition.signal();
        } else {
            try {
                fulCondition.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        lock.unlock();
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值