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

      

  • 相关阅读:
    怎么看到数据库以前做过的日志?
    感觉很好的网站
    uview 滑动切换
    Flyweight享元(结构型模式)
    悟空,真的是空?
    Interpreter解释器(行为型模式)
    Proxy代理(结构型模式)
    [转]有一种爱叫索取
    Command命令(行为型模式)
    Composite组合(结构型模式)
  • 原文地址:https://www.cnblogs.com/yangxiaohang/p/8617694.html
Copyright © 2011-2022 走看看