zoukankan      html  css  js  c++  java
  • 多线程信号源_红绿灯

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    using System.Threading;

    namespace ConsoleApplication1
    {
        class Program
        {
            static AutoResetEvent greenLight = new AutoResetEvent(false);
            static AutoResetEvent redLight = new AutoResetEvent(false);
            static void Main(string[] args)
            {
                Thread t1 = new Thread(new ThreadStart(M1));
                Thread t2 = new Thread(new ThreadStart(M2));

                t1.Start();
                t2.Start();

                //这个是主线程语句,相当于说现在是有3个线程。
                greenLight.Set();
            }

            static void M1()
            {
                while (true)
                {
                    if (greenLight.WaitOne())
                    {
                        Console.WriteLine("绿灯了。行人过马路!");
                        Thread.Sleep(3000);
                        redLight.Set();//红灯了
                    }
                }
            }
            static void M2()
            {
                while (true)
                {
                    if (redLight.WaitOne())
                    {
                        Console.WriteLine("红灯了。行人等待!");
                        Thread.Sleep(3000);
                        greenLight.Set();
                    }
                }
            }
        }
    }
  • 相关阅读:
    pyqt-QGrapicsView类
    pyqt5.0 GraphicsView框架
    STM32(三)- GPIO输入输出之按键检测点亮LED
    STM32(二)- 位带操作、启动文件讲解
    STM32(一)- 基于固件库的工程模板
    C语言(四)- C预处理和C库
    C语言(三)- 结构体、结构体指针、位运算
    C语言(二)- 函数、指针、数组
    C语言(一)- 基础知识
    [caffe笔记]:杀死caffe多个进程中的某个(发生 leveldb lock 解决方法)
  • 原文地址:https://www.cnblogs.com/pnljs/p/3551785.html
Copyright © 2011-2022 走看看