Lombda 表达式,应用多线程,若何应用 wait 释放线程资本。

这是一只小小鸟 发布于 06/03 11:43
浏览 411
收藏 2

鲲鹏开辟者生长练习营,学练结合,技能一站式进阶,赢华为手机等好礼!>>>

如图下代码所示,在应用 lombda 为 Runnable 供给完成办法时,想要用 wait 释放线程资本,成果报错了,lombda中有办法应用 wait 这类办法吗。不可的话 在lombda 中又若何释放线程资本呢。

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class DemoMain {

    public static void main(String[] args) {
        DemoMain demo = new DemoMain();
        demo.market();
    }

    public void market(){
        AtomicInteger bread = new AtomicInteger(0);
        Lock lock = new ReentrantLock();

        Runnable product = () -> {
            while (true) {
                try {
                    lock.lock();
                    if (bread.get() < 99) {
                        bread.getAndIncrement();
                    }else{
                        this.wait();
                    }
                    System.out.println("临盆者临盆后,还有 " + bread.get() + " 个面包");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        };

        Runnable consumer = () -> {
            while (true) {
                try {
                    lock.lock();
                    if (bread.get() >= 1) {
                        bread.addAndGet(-1);
                    }else{

                    }
                    System.out.println("花费者花费后,还有 " + bread.get() + " 个面包");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        };

        new Thread(product).start();
        new Thread(consumer).start();
    }
}

 

加载中
0
小222
小222
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class DemoMain {

    public static void main(String[] args) {
        DemoMain demo = new DemoMain();
        demo.market();
    }

    public void market(){
        AtomicInteger bread = new AtomicInteger(0);
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        Runnable product = () -> {
            while (true) {
                try {
                    lock.lock();
                    if (bread.get() < 99) {
                        bread.getAndIncrement();
                    }else{
                        condition.await();
                    }
                    System.out.println("临盆者临盆后,还有 " + bread.get() + " 个面包");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    condition.signal();
                    lock.unlock();
                }
            }
        };

        Runnable consumer = () -> {
            while (true) {
                try {
                    lock.lock();
                    if (bread.get() >= 1) {
                        bread.addAndGet(-1);
                    }else{
                        condition.await();
                    }
                    System.out.println("花费者花费后,还有 " + bread.get() + " 个面包");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    condition.signal();
                    lock.unlock();
                }
            }
        };

        new Thread(product).start();
        new Thread(consumer).start();
    }
}

 

https://blog.csdn.net/wd_888/article/details/105154184

这是一只小小鸟
这是一只小小鸟
感激 ,感激,涨知识了。
0
M
Mzoro

仿佛不是lombda 的锅

0
SvenAugustus
SvenAugustus
基本了,Lock应用lock.await。假设是synchronized则用object.wait。
0
f
freezingsky

lambda  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

0
dongjunv5
dongjunv5

wait不是这么用的 要合营 synchronized

前往顶部
顶部