• ThreadPool 示例


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

    namespace ThreadIsAll
    {
        class Program
        {
            public class PrintNumbers
            {
                private object threadLock = new object();
                public void PrintNumber()
                {
                    lock(threadLock)
                    {
                        Console.WriteLine("-> {0} is exucuting PrintNumbers()", Thread.CurrentThread.ManagedThreadId);
                        Console.WriteLine("Your numbers:");
                        for (int i = 0; i < 10; i++)
                        {
                            Console.Write("{0}", i);
    //                        Thread.Sleep(1000);
                        }
                        Console.WriteLine();
                    }
                }
            }
            static void PrintTheNumbers(object state)
            {
                PrintNumbers p = (PrintNumbers)state;
                p.PrintNumber();
            }
            static void Main()
            {
                Console.WriteLine("Main thread started. threadID is {0}", Thread.CurrentThread.ManagedThreadId);

                //for free thread numbers. begin
                //int wt;
                //int ct;
                //ThreadPool.GetAvailableThreads(out wt, out ct);
                //for free thread numbers. end
                PrintNumbers p = new PrintNumbers();
                WaitCallback workItem = new WaitCallback(PrintTheNumbers);

                for(int i = 0; i<10; i++)
                {
                    ThreadPool.QueueUserWorkItem(workItem, p);
                }
                Console.WriteLine("All task queued");
                Console.ReadLine();
            }
        }
    }


    //namespace ThreadIsAll
    //{
    //    class Program
    //    {
    //        public delegate int BinaryOp(int x, int y);
    //        static void Main()
    //        {
    //            Console.WriteLine("Main() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId);
    //            BinaryOp b = new BinaryOp(Add);

    //            IAsyncResult iftAR = b.BeginInvoke(10, 9, new AsyncCallback(AddComplete), null);

    //            int ret = b.EndInvoke(iftAR);

    //            Console.WriteLine("result is {0}",ret);

    //            Console.ReadLine();
    //        }
    //        static int Add(int x, int y)
    //        {
    //            Console.WriteLine("Add() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId);
    //            Thread.Sleep(5000);
    //            return x + y;
    //        }
    //        static void AddComplete(IAsyncResult itfAR)
    //        {
    //            Console.WriteLine("AddComplete() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId);
    //            Console.WriteLine("Your Addtion is complete.");
    //        }
    //    }
    //}


    //namespace ThreadIsAll
    //{
    //    class Program
    //    {
    //        public delegate int BinaryOp(int x, int y);
    //        static void Main(string[] args)
    //        {
    //            Console.WriteLine("Main() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId);

    //            BinaryOp b = new BinaryOp(Add);
    //            IAsyncResult iftAR = b.BeginInvoke(10, 9, null, null);

    ////            Console.WriteLine("Doing more work in Main()");

    //            while (!iftAR.IsCompleted)
    //            {
    //                Console.WriteLine("Doing more work in Main()");
    //                Thread.Sleep(1000);
    //            }

    //            int answer = b.EndInvoke(iftAR);
    //            Console.WriteLine("10 + 9 is {0}", answer);
    //            Console.ReadLine();
    //        }
    //        static int Add(int x, int y)
    //        {
    //            Console.WriteLine("Add() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId);
    //            Thread.Sleep(5000);
    //            return x + y;
    //        }
    //    }
    //}

  • 相关阅读:
    【Dubbo 源码解析】08_Dubbo与Spring结合
    【Dubbo 源码解析】07_Dubbo 重试机制
    【Dubbo 源码解析】06_Dubbo 服务调用
    【Dubbo 源码解析】05_Dubbo 服务发现&引用
    【Dubbo 源码解析】04_Dubbo 服务注册&暴露
    【Dubbo 源码解析】03_Dubbo Protocol&Filter
    【Dubbo 源码解析】02_Dubbo SPI
    Hadoop(十五)MapReduce程序实例
    Hadoop(十四)MapReduce原理分析
    Hadoop(十三)分析MapReduce程序
  • 原文地址:https://www.cnblogs.com/MayGarden/p/1494388.html
Copyright © 2020-2023  润新知