zoukankan      html  css  js  c++  java
  • 线程池示例(摘抄)

    using System.Threading;
    
    namespace System
    {
        public static class CompileCSCAtRuntime
        {
            private delegate string RunOnThreadPool(out int threadId);
    
            private static void Callback(IAsyncResult ar)
            {
                Console.WriteLine("Starting a callback...");
                Console.WriteLine("State passed to a callback: {0}", ar.AsyncState);
                Console.WriteLine("Is Thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread);
                Console.WriteLine("Thread pool worker thread id: {0}", Thread.CurrentThread.ManagedThreadId);
            }
    
            private static string Test(out int threadId)
            {
                Console.WriteLine("Starting...");
                Console.WriteLine("Is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread);
                Thread.Sleep(TimeSpan.FromSeconds(2));
                threadId = Thread.CurrentThread.ManagedThreadId;
                return string.Format("Thread pool worker thread id was: {0}", threadId);
            }
    
            static void Main(string[] args)
            {
                int threadId = 0;
    
                RunOnThreadPool poolDelegate = Test;
    
                var t = new Thread(() => Test(out threadId));
                t.Start();
                t.Join();
    
                Console.WriteLine("Thread id: {0}", threadId);
    
                IAsyncResult r = poolDelegate.BeginInvoke(out threadId, Callback, "a delegate asynchronous call");
                r.AsyncWaitHandle.WaitOne();
    
                string result = poolDelegate.EndInvoke(out threadId, r);
    
                Console.WriteLine("Thread pool worker thread id: {0}", threadId);
                Console.WriteLine(result);
    
                Console.WriteLine("main threadid was: {0}", Thread.CurrentThread.ManagedThreadId);
    
                Thread.Sleep(TimeSpan.FromSeconds(2));
            }
    
        }
    }

    示例来源:《C#多线程编程实战》

  • 相关阅读:
    webStorage和cookie相比存在的优势
    session、localStorage和cookie之间的区别
    了解常见的状态码
    什么情况下会碰到跨域问题?有哪些方法可以解决
    什么是跨域?跨域请求资源的方式有哪些?
    垃圾回收机制
    400,500错误
    Junit添加完maven依赖无法使用@Test
    Maven资源过滤问题处理
    javascript的一些注意点
  • 原文地址:https://www.cnblogs.com/hellowzl/p/8966338.html
Copyright © 2011-2022 走看看