zoukankan      html  css  js  c++  java
  • ThreadPool学习草稿1

    原文发布时间为:2010-10-27 —— 来源于本人的百度文章 [由搬家工具导入]

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

    namespace TestThreadPool
    {
        class Program
        {
            static object locker = new object();
            static int runningThreads = 0;

            static void Main(string[] args)
            {
                try
                {
                    ThreadPool.SetMaxThreads(4, 4); //设置最大线程数 using System.Threading;

                    runningThreads = 10;
                    for (int i = 0; i < runningThreads; i++)
                    {

                        ThreadPool.QueueUserWorkItem(new WaitCallback(Auto), i);//线程池指定线程执行Auto方法
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                int iWhileNumber = 0;

                lock (locker)
                {
                    while (runningThreads > 0)
                    {
                        Monitor.Wait(locker);
                        iWhileNumber++;
                    }
                }
                Console.WriteLine("结束了,共循环了" + iWhileNumber + "次");
                //Environment.Exit(0);
                Console.ReadLine();
            }

            public static void Auto(object i)//多线程执行的方法
            {
                try
                {
                    int d = 4 / (int)i;
                    if (string.Equals(i, 2))
                    {
                        Thread.Sleep(1000);
                    }
                    Console.WriteLine(i.ToString());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    lock (locker)
                    {
                        runningThreads--;
                        Monitor.Pulse(locker);
                    }
                }
            }
        }
    }

  • 相关阅读:
    数学图形(1.25)cassini曲线
    数学图形(1.24)巴斯加线与蚶线
    数学图形(1.23)太极线
    webpack打包多个入口文件
    cnpm与npm的区别
    入门 Webpack,看这篇就够了
    protocol error, got 'n' as reply type byte + redis如何后台启动
    PHP执行系统外部命令函数:exec()、passthru()、system()、shell_exec()
    CentOS7.0+Zend Guard Loader for PHP 5.6环境搭建
    通过shell脚本进行数据库操作
  • 原文地址:https://www.cnblogs.com/handboy/p/7163961.html
Copyright © 2011-2022 走看看