zoukankan      html  css  js  c++  java
  • c#之线程池优先级

    using System;
    using System.Threading;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                ManualResetEvent rem = new ManualResetEvent(false);  //设置信号灯为无状态
                ThreadPool.SetMaxThreads(3, 3);
                thr t = new thr(15, rem);
                for (int i = 0; i < 15; i++) 
                {
             
                  ThreadPool.QueueUserWorkItem(new WaitCallback(t.met),i);
                }
                rem.WaitOne(Timeout.Infinite, true);
                Console.WriteLine("断点测试");
                Thread.Sleep(10000);
                Console.WriteLine("运行结束");
                Console.ReadKey();
            }
    
            
        }
    
        public class thr 
        {
            public thr(int count,ManualResetEvent res) 
            {
                iMaxCount1 = count;
                evens = res;
    
            }
    
            public static int icount = 0;
            public static int iMaxCount1 = 0;
            public ManualResetEvent evens;
            public void met(object i)
            {
                Console.WriteLine("thgead[" + i.ToString() + "]");
                Thread.Sleep(2000);
                //Interlocked.Increment()操作是一个原子操作,作用是:iCount++ 具体请看下面说明 
                //原子操作,就是不能被更高等级中断抢夺优先的操作。你既然提这个问题,我就说深一点。
                //由于操作系统大部分时间处于开中断状态,
                //所以,一个程序在执行的时候可能被优先级更高的线程中断。
                //而有些操作是不能被中断的,不然会出现无法还原的后果,这时候,这些操作就需要原子操作。
                //就是不能被中断的操作。
                Interlocked.Increment(ref icount);
                if (icount == iMaxCount1)
                {
                    Console.WriteLine("发出结束信号!");
                    //将事件状态设置为终止状态,允许一个或多个等待线程继续。
                    evens.Set();
                }
            }
    
           
    
           
        }
    }
    

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Diagnostics;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                ManualResetEvent rem = new ManualResetEvent(false);  //设置信号灯为无状态
                ThreadPool.SetMaxThreads(1, 3);
                for (int i = 0; i < 5; i++)
                {
                    thr t = new thr(5, rem);
                    ThreadPool.QueueUserWorkItem(new WaitCallback(t.met), i);
                }
                Console.WriteLine("断点测试");
                Thread.Sleep(200000);
                Console.ReadKey();
            }
        }
        public class thr
        {
            public thr(int count, ManualResetEvent res)
            {
                iMaxCount1 = count;
                evens = res;
    
            }
    
            public static int icount = 0;
            public static int iMaxCount1 = 0;
            public ManualResetEvent evens;
            public void met(object ss)
            {
                lock (this)
                {
                    Thread.Sleep(30);
                    Random r = new Random();
    
    
    
    
                    List<int> l = new List<int>();
                    for (int j = 0; j < 6; j++)
                    {
                        int lable = r.Next(1, 34);
                        if (l.Contains(lable))
                        {
                            j--;
                        }
                        else
                        {
                            l.Add(lable);
                        }
                    }
    
                    l.Sort();
                    int lan = r.Next(1, 17);
                    string zu = l[0].ToString() + " " + l[1].ToString() + " " + l[2].ToString() + " " + l[3].ToString() + " " + l[4].ToString() + " " + l[5].ToString() + "-" + lan;
                    Console.WriteLine(zu);
    
    
                    //Interlocked.Increment()操作是一个原子操作,作用是:iCount++ 具体请看下面说明
                    //原子操作,就是不能被更高等级中断抢夺优先的操作。你既然提这个问题,我就说深一点。
                    //由于操作系统大部分时间处于开中断状态,
                    //所以,一个程序在执行的时候可能被优先级更高的线程中断。
                    //而有些操作是不能被中断的,不然会出现无法还原的后果,这时候,这些操作就需要原子操作。
                    //就是不能被中断的操作。
                    Interlocked.Increment(ref icount);
                    if (icount == iMaxCount1)
                    {
                        Console.WriteLine("发出结束信号!");
                        //将事件状态设置为终止状态,允许一个或多个等待线程继续。
                        evens.Set();
                    }
                }
            }
    
            public void PlayGame(object ss)
            {
    
    
            }
    
    
    
    
    
    
        }
    }
    

      

    
    
  • 相关阅读:
    HTML5学习记录
    CSS学习记录
    HTML扩展(thead,tbody,tfoot标签的使用)
    测删除功能
    jmeter-连接数据库
    jmeter-正则表达式提取器
    jmeter常用函数
    java基础(二)
    git基本使用
    波特的钻石模型
  • 原文地址:https://www.cnblogs.com/mengluo/p/5592918.html
Copyright © 2011-2022 走看看