zoukankan      html  css  js  c++  java
  • 多线程加深理解_进攻五个城

       1:  using System;
       2:  using System.Collections.Generic;
       3:  using System.Linq;
       4:  using System.Text;
       5:  using System.Threading;
       6:   
       7:  namespace CatVSMice_ForManyWars
       8:  {
       9:      class Program
      10:      {
      11:          /// <summary>
      12:          /// 军队的状态:set()时,表明已集结完毕,WaitOne时,表明在等待军队集结完毕。
      13:          /// </summary>
      14:          static EventWaitHandle armyState =new  AutoResetEvent(false);
      15:   
      16:          /// <summary>
      17:          /// 进攻状态:set()时,表明可以攻击了,或是下达了进攻命令,waitOne时,则在等待下达进攻命令。
      18:          /// </summary>
      19:          static EventWaitHandle attackState = new AutoResetEvent(false);
      20:   
      21:          /// <summary>
      22:          /// 进攻的第几个城
      23:          /// </summary>
      24:          static int info = -1;
      25:   
      26:          /// <summary>
      27:          /// 攻城的数量。
      28:          /// </summary>
      29:          static int maxTower = 5;
      30:   
      31:          static void Main(string[] args)
      32:          {
      33:              Thread th = new Thread(Attack);
      34:              th.Start();
      35:   
      36:              for (int i = 1; i <= maxTower; i++)
      37:              {
      38:                  //要攻下5个城。但每次只能攻一个城。
      39:   
      40:                  //指挥部(主程序)等待部队集结完毕。
      41:                  armyState.WaitOne();//直到部队集结完毕后才下达进攻命令
      42:   
      43:                  //当队伍集结完毕后,可以进攻了。
      44:                  attackState.Set();
      45:   
      46:                  Console.WriteLine("进攻的第" + i + "城");
      47:                  info = i;
      48:              }           
      49:              
      50:              Console.ReadLine();
      51:          }
      52:   
      53:          static void Attack()
      54:          {
      55:              while (true)
      56:              {
      57:                  if (info == maxTower)
      58:                  {
      59:                      Console.WriteLine("共攻下" + info.ToString() + "个城");
      60:                      break;
      61:                  }
      62:   
      63:                  Console.WriteLine("部队集结中。。。");
      64:                  Thread.Sleep(2000);
      65:   
      66:                  Console.WriteLine("部队集结完毕");
      67:                  armyState.Set();//准备好了,就发个信号通知主程序,部队已集结完毕。
      68:   
      69:                  //部队集结完毕后,等待进攻命令。
      70:                  Console.WriteLine("等待进攻命令。");
      71:                  attackState.WaitOne();
      72:   
      73:                  //当收到进攻命令时,开始冲杀。
      74:                  Console.WriteLine("进攻呀,冲啊,杀啊。。。。");
      75:   
      76:                  Thread.Sleep(3000);//进攻中。
      77:                  
      78:                  Console.WriteLine("进攻第" + info + "个城获得胜利。");                
      79:              }
      80:          }
      81:      }
      82:  }

    自己对EventWaitHandle的Set及WaitOne理解。
    Set:将事件状态设置为终止状态,从而允许继续执行一个或多个等待线程。 (MSDN)
    自己理解为在当前线程中给别的线程一个信号源。
    WaitOne:阻止当前线程,直到当前 WaitHandle 收到信号。(MSDN)
    自己理解为在阻止当前线程,等待别人给自己一个信号源。

  • 相关阅读:
    hibernate入门
    struts文件上传
    Struts的增删改查
    struts入门
    Maven配置以及环境搭配
    layui增删改查
    easyui三
    A
    C. Permutation Cycle
    E
  • 原文地址:https://www.cnblogs.com/pnljs/p/3024054.html
Copyright © 2011-2022 走看看