zoukankan      html  css  js  c++  java
  • 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;
    //        }
    //    }
    //}

  • 相关阅读:
    微信公众号内调用微信支付
    transform-translate3d
    Ubuntu16.04 install apache-flume-1.7.0-bin.tar.gz
    Ubuntu16.04 install apache-hive-2.X.X-bin.tar.gz
    Ubuntu16.04 install mysql5.X
    Ubuntu16.04 install hadoop-2.8.1.tar.gz伪分布式配置
    Ubuntu16.04 install jdk-8u144-linux-x64.tar.gz
    入门VMware Workstation下的Debian学习之Vim简单使用(三)
    入门VMware Workstation下的Debian学习之基本命令(二)
    Ubuntu16.04 install android-studio-ide-162.4069837-linux
  • 原文地址:https://www.cnblogs.com/MayGarden/p/1494388.html
Copyright © 2011-2022 走看看