• C#多线程和线程池问题


    static void Main(string[] args)
            {                
                Thread threadA = new Thread(ThreadMethod); //执行的必须是无返回值的方法 
                threadA.Name = "A";
                Thread threadB = new Thread(ThreadMethod); //执行的必须是无返回值的方法 
                threadB.Name = "B";
                threadA.Priority = ThreadPriority.Highest;
                threadB.Priority = ThreadPriority.BelowNormal;
                threadB.Start();
                threadA.Start();
                Thread.CurrentThread.Name = "C";
                ThreadMethod(new object());
                Console.ReadKey();
            }
            public static void ThreadMethod(object parameter)
            {
                for (int i = 0; i < 500; i++)
                { 
                    Console.Write(Thread.CurrentThread.Name); 
                }
            }
    View Code
    static void Main(string[] args)
            {
                Program pro = new Program();
                Thread threadA = new Thread(pro.ThreadMethod); //执行的必须是无返回值的方法 
                threadA.Name = "王文建";
                Thread threadB = new Thread(pro.ThreadMethod); //执行的必须是无返回值的方法 
                threadB.Name = "生旭鹏";
                threadA.Start();
                threadB.Start();
                Console.ReadKey();
            }
            public void ThreadMethod(object parameter)
            {
                lock (this)             //添加lock关键字
                {
                    for (int i = 0; i < 10; i++)
                    {
                        Console.WriteLine("我是:{0},我循环{1}次", Thread.CurrentThread.Name, i);
                        Thread.Sleep(300);
                    }
                } 
            }
    View Code
    static void Main(string[] args)
            {
                Program pro1 = new Program();                    
                Program pro2 = new Program();                   
                Thread threadA = new Thread(pro1.ThreadMethod); //执行的必须是无返回值的方法 
                threadA.Name = "王文建";
                Thread threadB = new Thread(pro2.ThreadMethod); //执行的必须是无返回值的方法 
                threadB.Name = "生旭鹏";
                threadA.Start();
                threadB.Start();
                Console.ReadKey();
            }
            public void ThreadMethod(object parameter)
            {
                lock (this)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        Console.WriteLine("我是:{0},我循环{1}次", Thread.CurrentThread.Name, i);
                        Thread.Sleep(300);
                    }
                }
            }
    View Code
    View Code

    原文地址------------------------------------>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    多线程和线程池的一个全部解析——————————————————>>>>>>飞机票

  • 相关阅读:
    [论文复现笔记]Im2Struct
    深度学习踩坑
    Matlab问题汇总
    Linux网络服务
    探索Blender
    [每日挖坑]20200728
    Ubuntu重启之后显卡挂了
    3D视觉知识点
    [每日挖坑]20200727
    遥感影像相关知识
  • 原文地址:https://www.cnblogs.com/liuqifeng/p/9150567.html
Copyright © 2020-2023  润新知