zoukankan      html  css  js  c++  java
  • 多线程简单应用示例

    程序主题部分摘自msdn中Thread 类的示例说明,地址:http://msdn.microsoft.com/zh-cn/library/System.Threading.Thread(v=vs.110).aspx

    附改动后的代码:

    主体代码:

    static void Main(string[] args)
            {
                Console.WriteLine("Main thread: Start a second thread.");
    
                List<Thread> listT = new List<Thread>();
    
                for (int i = 0; i < 5; i++)
                {
                    Thread t = new Thread(new ThreadStart(ThreadProc));
    
                    listT.Add(t);
    
                    t.Start();
                    //Thread.Sleep(0);
                }
    
                for (int i = 0; i < 4; i++)
                {
                    Console.WriteLine("Main thread: Do some work.");
                    Thread.Sleep(0);
                }
    
                Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
                foreach (Thread t in listT)
                {
                    t.Join();
                }
                Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
                Console.ReadLine();
            }
    
            public static void ThreadProc()
            {
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine("ThreadProc: {0}", i);
                    // Yield the rest of the time slice.
                    Thread.Sleep(0);
                }
            }

    需要单独控制线程停止,可给线程设置Name属性来实现。

  • 相关阅读:
    JavaScript运行机制 Event Loop
    async 函数
    JavaScript Promise 对象
    pc端rest.css
    微信小程序公用样式类
    移动端base.css
    RegExp正则对象匹配规则
    RegExp正则相关方法
    mysql(五)事务
    mysql(四)海量数据优化
  • 原文地址:https://www.cnblogs.com/kinolee/p/3499503.html
Copyright © 2011-2022 走看看