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();
            }
        }
    }
  • 相关阅读:
    ping和telnet
    nginx下No input file specified错误的解决
    【Git】删除某个全局配置项
    windows7使用Sphinx+PHP+MySQL详细介绍
    TortoiseGit需要重复填写用户名和密码的问题
    【算法】字符串数组的排序时间复杂度问题
    java随机生成6位随机数 5位随机数 4位随机数
    Linux下MySQL报Table 'xxx' doesn't exist错误解决方法,表名存在大小写区分
    Cannot find ./catalina.sh The file is absent or does not have execute permission This file is nee
    Linux 服务器安装jdk,mysql,tomcat简要教程
  • 原文地址:https://www.cnblogs.com/rhythmK/p/2530447.html
Copyright © 2011-2022 走看看