博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2.1
阅读量:6236 次
发布时间:2019-06-22

本文共 3027 字,大约阅读时间需要 10 分钟。

//2.1.4 //Sleep IsAlive using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Reflection;using System.Runtime.Remoting;public class Threading{    static void WorkerFunction()    {        string ThreadState;        for (int i = 1; i < 5000; ++i)        {            if (i % 50 == 0)            {                ThreadState = Thread.CurrentThread.ThreadState.ToString();                Console.WriteLine("Worker;"+ThreadState);            }        }        Console.WriteLine("WorkerFunction Complete");    }    static void Main()    {        string ThreadState;        Thread t = new Thread(new ThreadStart(WorkerFunction));        t.Start();        while (t.IsAlive)        {            Console.WriteLine("Still waiting .I'm going back to sleep.");            Thread.Sleep(20);        }        ThreadState = t.ThreadState.ToString();        Console.WriteLine("He's finally done! Thread state is :"            + ThreadState);        Console.ReadLine();    }}

  

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Reflection;using System.Runtime.Remoting;public class Threading{    public static Thread worker;    public static Thread worker2;    static void Main()    {        Console.WriteLine("Entering void Main()");        worker = new Thread(new ThreadStart(FindPriority));        worker2 = new Thread(new ThreadStart(FindPriority));        worker.Name = "FindPriority()Thread";        worker2.Name = "FindPriority()Thread2";        worker2.Priority = System.Threading.ThreadPriority.Highest;        worker.Start();        worker2.Start();        Console.WriteLine("Exitting void Main()");        Console.ReadLine();    }    static public void FindPriority()    {        //Thread tempThread;        //System.Threading.Thread.CurrentThread();        Console.WriteLine("Name ;" + System.Threading.Thread.CurrentThread.Name);        Console.WriteLine("State :" + System.Threading.Thread.CurrentThread.ThreadState.ToString());        Console.WriteLine("Priority:" +System.Threading.Thread.CurrentThread.Priority.ToString());    }}

  

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Reflection;using System.Runtime.Remoting;public class Threading{    static void Main(string[] args)    {        Thread.CurrentThread.Name = "主线程";        Thread thread1 = new Thread(new ThreadStart(Threading.Output));        thread1.Name = "子线程1";        thread1.Priority = ThreadPriority.Lowest;        Thread thread2 = new Thread(new ThreadStart(Threading.Output));        thread2.Name = "子线程2";        thread2.Priority = ThreadPriority.Highest;        thread1.Start();        thread2.Start();        Console.ReadLine();    }    static void Output()    {        for (int i = 0; i < 10; i++)        {            Console.WriteLine("线程:{0},i的值:{1}", Thread.CurrentThread.Name, i);        }    }}

 

转载于:https://www.cnblogs.com/jiaoluo/p/3550677.html

你可能感兴趣的文章