zoukankan      html  css  js  c++  java
  • C# 任务、线程、同步(三)

    线程池使用, 线程池中线程均为后台线程

     1 static void Main()
     2         {
     3             int nWorkerThreads;
     4             int nCompletionPortThreads;
     5             ThreadPool.GetMaxThreads(out nWorkerThreads, out nCompletionPortThreads);
     6             Console.WriteLine("Max worker threads: {0}, I/O completion threads: {1}", nWorkerThreads, nCompletionPortThreads);
     7 
     8             for (int i = 0; i < 5; i++)
     9             {
    10                 ThreadPool.QueueUserWorkItem(JobForAThread);
    11 
    12             }
    13 
    14             Thread.Sleep(3000);
    15         }
    16 
    17 
    18         static void JobForAThread(object state)
    19         {
    20             for (int i = 0; i < 3; i++)
    21             {
    22                 Console.WriteLine("loop {0}, running inside pooled thread {1}", i,
    23                    Thread.CurrentThread.ManagedThreadId);
    24                 Thread.Sleep(50);
    25             }
    26 
    27         }

    Thread类使用

     1  static void Main(string[] args)
     2         {
     3             //定义、启动线程
     4             var t1 = new Thread(ThreadMain1);
     5             t1.Start();
     6             Console.WriteLine("This is the main thread.{0}",Thread.CurrentThread.ManagedThreadId);
     7 
     8             var t2 = new Thread(() => Console.WriteLine(" running is a thread, id {0}", Thread.CurrentThread.ManagedThreadId));
     9             t2.Start();
    10             Console.WriteLine("This is the main thread , id {0}", Thread.CurrentThread.ManagedThreadId);
    11 
    12             //线程传参
    13             var d = new Data { Message = "Info" };
    14             var t3 = new Thread(ThreadMainWithParameters);
    15             t3.Start(d);
    16 
    17             //后台线程
    18             var t4 = new Thread(ThreadMain2) { Name = "MyNewThread1", IsBackground = true, Priority = ThreadPriority.Highest };
    19             t4.Start();
    20             Console.WriteLine("Main thread ending now.");
    21 
    22    
    23             Console.Read();
    24         }
    25 
    26         static void ThreadMain1()
    27         {
    28             Console.WriteLine("Running in a thread.{0}", Thread.CurrentThread.ManagedThreadId);
    29         }
    30         static void ThreadMainWithParameters(object o)
    31         {
    32             Data d = (Data)o;
    33             Console.WriteLine("Running in a thread ,received {0}", d.Message);
    34         }
    35         static void ThreadMain2()
    36         {
    37             Console.WriteLine("Thread {0} started", Thread.CurrentThread.Name);
    38             Thread.Sleep(3000);
    39             // Console.WriteLine("Running in the thread {0}, id: {1}.", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId);
    40             Console.WriteLine("Thread {0} completed", Thread.CurrentThread.Name);
    41         }
    42 
    43 
    44         public struct Data
    45         {
    46             public string Message;
    47         }
  • 相关阅读:
    JavaScript变量的传递方式
    三分钟读懂BGP带宽
    Docker安装及基础知识
    CentOS搭建“加速器”
    SSH 不分配远程主机tty
    2020年智慧电力解决方案
    docker安装2020
    如何让电力巡检机器人项目落地
    清淤机器人项目落地重庆市政工程
    智慧电网_电力巡检机器人解决方案
  • 原文地址:https://www.cnblogs.com/farmer-y/p/6085788.html
Copyright © 2011-2022 走看看