• 多线程信号源_红绿灯


    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();
                    }
                }
            }
        }
    }
  • 相关阅读:
    Mysql update case
    phpexcel导出excel等比例缩放图片
    phpexcel错误 You tried to set a sheet active by the out of bounds index: 1解决办法
    phpexcel操作
    Java io基础
    java线程基础
    java 集合基础(适用单线程)
    java 泛型深入
    Java反射基础
    Java泛型基础
  • 原文地址:https://www.cnblogs.com/pnljs/p/3551785.html
Copyright © 2020-2023  润新知