• 2.1


    //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);
            }
        }
    }
  • 相关阅读:
    7.1 异常处理结构
    第 7 章 异常处理结构、代码测试与调试
    6.4.2 案例精选
    6.4.1 标准库 os、os.path 与 shutil 简介
    设计模式----装饰模式
    设计模式---单例模式
    设计模式--工厂方法模式
    设计模式-简单工厂模式
    设计模式基础知识
    更改Mysql数据库中的数据出现乱码问题
  • 原文地址:https://www.cnblogs.com/jiaoluo/p/3550677.html
Copyright © 2020-2023  润新知