Lua 协程 + 过滤器实现生产者和消费者

在这里插入图片描述

文章目录

  • 调用 p r o d u c e r ( ) producer() producer() 返回一个 c o r o u t i n e coroutine coroutine

  • 调用 f i l t e r ( p r o d ) filter(prod) filter(prod) 返回一个 c o r o u t i n e coroutine coroutine

  • 执行 c o n s u m e r ( p r o d ) consumer(prod) consumer(prod) 函数

    • c o n s u m e r consumer consumer 调用 r e c e i v e receive receive 函数
    • f i l t e r filter filter 协程开始执行
    • p r o d u c e r producer producer 协程开始执行
    • s e n d send send p r o d u c e r producer producer 协程挂起
    • f i l t e r filter filter 协程挂起
    • 返回到 c o n s u m e r consumer consumer r e c e i v e receive receive 函数

实现

#!/usr/local/bin/lua

function send(x) 
    coroutine.yield(x)
end

--[[
-- 生产者
--      创建一个coroutine,生产、停止生产、发送商品
--]]
function producer() 
    return coroutine.create(function() 
        while true do 
            local x = io.read() -- 生产商品
            send(x) -- 停止生产、返回商品
        end 
    end) 
end

function receive(prod) 
    local status, value = coroutine.resume(prod)
    return value
end

function filter(prod) 
    return coroutine.create(function() 
        for line = 1, math.huge do 
            local x = receive(prod) 
            x = string.format("%5d %s", line, x)
            send(x)
        end
    end)
end

--[[
-- 消费者一直循环,当需要消费就唤醒生产者
--]]
function consumer(prod) 
    while true do 
        local x = receive(prod) -- filter coroutine
        io.write(x, "\n")
    end
end 

consumer(filter(producer()))

输出

lua 
	1	lua
c++
	2	c++
py
	3	py

结语

在这里插入图片描述

Lua还在精进中,期待您的关注~~

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
生产者消费者模式是一种常见的多线程编程模式,可以用于解决生产者消费者之间的数据交换问题。在Java中,可以使用多种方式实现生产者消费者模式,比如使用wait()和notify()方法、使用BlockingQueue等。以下是一种使用wait()和notify()方法实现生产者消费者模式的示例代码: ``` public class ProducerConsumerExample { private static final int BUFFER_SIZE = 10; private static List<Integer> buffer = new ArrayList<>(BUFFER_SIZE); public static void main(String[] args) { Thread producerThread = new Thread(new Producer()); Thread consumerThread = new Thread(new Consumer()); producerThread.start(); consumerThread.start(); } static class Producer implements Runnable { @Override public void run() { while (true) { synchronized (buffer) { while (buffer.size() == BUFFER_SIZE) { try { buffer.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } int item = (int) (Math.random() * 100); buffer.add(item); System.out.println("Produced item: " + item); buffer.notifyAll(); } } } } static class Consumer implements Runnable { @Override public void run() { while (true) { synchronized (buffer) { while (buffer.isEmpty()) { try { buffer.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } int item = buffer.remove(0); System.out.println("Consumed item: " + item); buffer.notifyAll(); } } } } } ``` 在这个示例代码中,Producer和Consumer都是实现了Runnable接口的线程类。Producer线程不断地生成随机数,并将其添加到buffer中,如果buffer已满,则等待。Consumer线程不断地从buffer中取出数据,并打印出来,如果buffer为空,则等待。在每次修改buffer的时候,都需要获取buffer的锁,并在修改完成后调用notifyAll()方法来唤醒其他等待的线程。 至于"lua closure factory 完整代码"和"中文加密"的问题,我可以回答。但是请注意,这些问题的答案并不代表我是一个真正的AI,而只是一个由CSDN开发的模拟AI。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ღCauchyོꦿ࿐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值