zoukankan      html  css  js  c++  java
  • 使用线程池

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Threading;
    using UnityEngine;
    using Random = System.Random;
    
    public class MyThreadManger : MonoBehaviour {
        Hashtable hashtable = new Hashtable();
        private int m_iParam=0;//随便一个类型的参数
        private static string name = "1";//随便一个类型的参数
        void Start()
        {
    
                ThreadPool.QueueUserWorkItem(new WaitCallback(MyMethod), name);//将方法添加进线程池,并传入参数
                ThreadPool.QueueUserWorkItem(new WaitCallback(MyMethod2), name);//将方法添加进线程池,并传入参数
    
        }
        private void MyMethod(object param)
        {
            Random ran = new Random(100);
          
            for (int i = 0; i < 100; i++)
            {
                try
                {
                    hashtable[i] = param;
                    param = Convert.ToString(ran.Next());
                    Debug.Log("开了一个线程:" + hashtable[i + "11~"].ToString());
                    Thread.Sleep(100);
                }
                catch (Exception ex)
                {
    
                    Debug.LogWarning(ex);
                }   
            }        
        }
    
        private void MyMethod2(object param)
        {
            Random ran = new Random(200);
            for (int i = 0; i < 100; i++)
            {
                try
                {
                    hashtable[i + "11~"] = param;
                    param = Convert.ToString(ran.Next(2));
                    Debug.Log("开了第二个线程:" + hashtable[i + "11~"].ToString());
                    Thread.Sleep(100);
                }
                catch (Exception ex)
                {
    
                    Debug.LogWarning(ex);
                }
              
            }
        }
    }
    

      

  • 相关阅读:
    VS2019正式版 密钥 Visual Studio 2019 破解 激活码 Key
    关于随机数的前世今生
    木兮的纪中集训感想
    浅谈欧洲算法——模拟退火
    你没听过的梅森旋转算法
    二分贪心杂题
    DP专项训练
    实验八 进程间通信
    实验七 信号
    实验六 进程基础
  • 原文地址:https://www.cnblogs.com/yangxiaohang/p/8617694.html
Copyright © 2011-2022 走看看