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)
            {
    
    
            }
    
    
    
    
    
    
        }
    }
    

      

    
    
  • 相关阅读:
    服务器实现跨域
    quartz定时任务cron表达式详解
    mysql分区/分片
    微信小程序 之三元运算符代替wx:if 来解决背景图片显示隐藏
    微信小程序 本地缓存保持登录状态之wx.setStorageSync()使用技巧
    微信小程序 赋值问题
    微信小程序 wx.navigateTo()传参及多个参数方法
    js 循环遍历数组
    VUE之Router命令行警告:Named Route 'Home' has a default child route. 解决办法
    VUE之命令行报错:Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead 解决办法
  • 原文地址:https://www.cnblogs.com/mengluo/p/5592918.html
Copyright © 2011-2022 走看看