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;
    //        }
    //    }
    //}

  • 相关阅读:
    java_oop_方法2
    POJ 3276 Face The Right Way(反转)
    POJ 3276 Face The Right Way(反转)
    POJ 2566 Bound Found(尺取法,前缀和)
    POJ 2566 Bound Found(尺取法,前缀和)
    POJ 3320 Jessica's Reading Problem(尺取法)
    POJ 3320 Jessica's Reading Problem(尺取法)
    POJ 3061 Subsequence(尺取法)
    POJ 3061 Subsequence(尺取法)
    HDU 1222 Wolf and Rabbit(欧几里得)
  • 原文地址:https://www.cnblogs.com/MayGarden/p/1494388.html
Copyright © 2011-2022 走看看