惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
S
Schneier on Security
人人都是产品经理
人人都是产品经理
博客园_首页
L
LangChain Blog
D
Docker
B
Blog
阮一峰的网络日志
阮一峰的网络日志
D
DataBreaches.Net
C
Check Point Blog
WordPress大学
WordPress大学
博客园 - 聂微东
P
Palo Alto Networks Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
腾讯CDC
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
Help Net Security
Help Net Security
The Last Watchdog
The Last Watchdog
有赞技术团队
有赞技术团队
美团技术团队
aimingoo的专栏
aimingoo的专栏
博客园 - 叶小钗
爱范儿
爱范儿
S
Security @ Cisco Blogs
MyScale Blog
MyScale Blog
C
Cisco Blogs
P
Proofpoint News Feed
I
Intezer
Last Week in AI
Last Week in AI
The Register - Security
The Register - Security
IT之家
IT之家
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Help Net Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Latest news
Latest news
M
MIT News - Artificial intelligence
N
News | PayPal Newsroom
G
Google Developers Blog
Cloudbric
Cloudbric
T
Troy Hunt's Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recorded Future
Recorded Future
Hugging Face - Blog
Hugging Face - Blog
AWS News Blog
AWS News Blog

博客园 - Shapley

数据库字段排序方法两则 Sql Server生成ULID例子 审批流进化记 winform+Task+async 献丑贴:Task.Run中foreach优化 Sql Server Begin TRY sample No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.Filters.ValidateAntiforgeryTokenAuthorizationFilter' has been registered. 某低代码平台自定义控件技术点 Element-plus的el-date-picker控件动态切换中英文方案 Chrome浏览器无法查看页面跳转前的请求日志及解决办法 element-plus上传视频并生成缩略图(封面)方案 JWT自动刷新草案 Ajax方法POST的两种提交方式 H5项目在微信浏览器运行实录 Sql Server变量声明及使用技巧 修改Dify的Nginx对外访问端口 Task with Console(with SemaphoreSlim) Task实战 asp.net core 9.0发布centos7.9 Barrier
CountdownEvent
Shapley · 2024-10-20 · via 博客园 - Shapley

CountdownEvent是一种同步结构,主要用来协调多种同步处理后的结果场景使用。其含义为:定义一个多信号的结构,然后在应用时判断信号是否全部到达,如果全部到达则继续往下运行,否则进行阻塞,有意思的是,如果阻塞已过,那么再次运行到wait()时,线程是不会再次阻塞的,除非调用reset()重新初始化后才可以。这点和其余的同步结构不一样。

示例如下:

internal class Program
{
    static CountdownEvent e = new CountdownEvent(3);
    static bool isOutput = true;

    static int i = 0;
    static void Main(string[] args)
    {
        Thread t = new Thread(ProcessData);
        t.Start();

        Console.WriteLine("启动");
        i++;

        Console.WriteLine("信号1已到");
        e.Signal();
        Console.WriteLine("信号2已到");
        e.Signal();
        Console.WriteLine("信号3已到");
        e.Signal();

        Console.ReadLine();
        i++;
        Console.WriteLine("信号1又到");
        e.Signal();
        Console.WriteLine("信号2又到");
        e.Signal();
        Console.WriteLine("信号3又到");
        e.Signal();

        Console.ReadKey();

    }
    static void ProcessData()
    {
        while (isOutput)
        {
            Console.WriteLine("阻塞等待信号");
            e.Wait();
            Console.WriteLine("阻塞已过,处理:Processed {0}", i);
            e.Reset();
            if (i > 1)
            {
                isOutput = false;
            }
        }
        if (!isOutput)
        {
            e.Dispose();
            Console.WriteLine("处理完成");
        }
    }

}

运行结果: