Lombda 表达式,使用多线程,如何使用 wait 释放线程资源。

这是一只小小鸟 发布于 06/03 11:43
阅读 430
收藏 2

华为云11月刊推送:DIY微信问答机器人,高性能计算代码的20个技巧!>>>

如图下代码所示,在使用 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
宗离
宗离
基础了,Lock使用lock.await。如果是synchronized则用object.wait。
0
f
freezingsky

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

0
dongjunv5
dongjunv5

wait不是这么用的 要配合 synchronized

返回顶部
顶部