zoukankan      html  css  js  c++  java
  • 乱七八糟 多线程

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

    namespace ParallelComputing
    {
        public class MoreThread
        {
            private class ManualResetEventObj
            {
                public ManualResetEvent resetEvent
                {
                    get;
                    set;
                }

                public Object obj
                {
                    get;
                    set;
                }
            }

            private List<int> list;
            ManualResetEvent auto;
            long doneCount = 0;

            public MoreThread()
            {
                list = new List<int>();
            }

            public void Run()
            {
                Stack<int> stack = new Stack<int>();
                for (int i = 0; i < 60; i++)
                {
                    stack.Push(i);
                }
                int temp = stack.Count;
                Console.WriteLine(temp);
                ManualResetEvent[] _manualResetEvents = new ManualResetEvent[temp];
                for (int i = 0; i < temp; i++)
                {
                    Console.WriteLine("The Current Namber is {0}" + i);
                    _manualResetEvents[i] = new ManualResetEvent(false);
                    ThreadPool.QueueUserWorkItem(new WaitCallback(Star), (Object)new ManualResetEventObj()
                    {
                        obj = i,
                        resetEvent = _manualResetEvents[i]
                    });
                }
                WaitHandle.WaitAll(_manualResetEvents);
                Console.WriteLine("执行完成!");
            }

            private void Star(Object obj)
            {
                ManualResetEventObj e = obj as ManualResetEventObj;
                int i = Convert.ToInt32(e.obj);
                Monitor.Enter(obj);
                Thread thread = Thread.CurrentThread;
                System.Threading.Thread.Sleep(10);
                Console.WriteLine(i);
                list.Add(Convert.ToInt32(i));
                if (!thread.Equals(Thread.CurrentThread))
                    throw new Exception("线程异常");
                Monitor.Exit(obj);
                e.resetEvent.Set();
            }
        }
    }
  • 相关阅读:
    一个例子帮助理解正则表达式
    requestAnimationFrame兼容性扩展
    检测访问网页的浏览器呈现引擎、平台、Windows操作系统、移动设备和游戏系统
    手把手教你如何安装和使用Karma-Jasmine
    cesium随笔 — 隐藏三维场景下方版权信息
    cesium随笔 — 获取当前鼠标的经度、纬度、高度
    Cesium Language (CZML) 入门2 — CZML Content(CZML的内容)
    html初识
    MySQL终章
    MySQL表与表之间的关系详解
  • 原文地址:https://www.cnblogs.com/rhythmK/p/2530447.html
Copyright © 2011-2022 走看看