开发者社区> 问答> 正文

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

如图下代码所示,在使用 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();
    }
}

 

展开
收起
爱吃鱼的程序员 2020-06-05 13:45:36 550 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
                        <pre>
    

    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

    2020-06-05 13:45:50
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
多IO线程优化版 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多