当前位置:编程学习 > C#/ASP.NET >>

.net 中线程数有限制吗,求助

代码 
using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Collections; 

using System.Threading; 
namespace test 

    class Program 
    { 

        static void Main(string[] args) 
        { 

        

            for (int i = 0; ; i++) 
            { 

                try 
                { 
                    ceshi ch = new ceshi(); 
                    //ch.dispose(); 
                
                  //  th.Abort(); 
                  //  Thread.Sleep(1000); 
                    Console.WriteLine(">>"+i); 
                } 
                catch(Exception ex) 
                { 
                  
                    Console.WriteLine(ex.ToString()); 
                    break; 
                } 

            } 
            Console.Read(); 
        } 
    } 
    class ceshi 
    { 
        public Thread th; 
        public ceshi() 
        { 
            th = new Thread(f); 
            th.Start(); 
        } 
        void f() 
        { 
            int k = 0; 

            while (true) 
            { 
                k++; 
                Thread.Sleep(1000); 
                //if (k == 10) 
                //{ 
                //    break; 
                //} 
            } 
        } 
        public void dispose() 
        { 
            th.Abort(); 
        } 
        ~ceshi() 
        { 
            th.Abort(); 
        } 
    } 

    
    



当跑到第1862(不同机器不一样) 时候,就报System.OutOfMemoryException  错误 
根本不是电脑内存不够,程序才吃到50M 左右  我电脑4G内存 

这到底怎么回事啊  --------------------编程问答--------------------


sing System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace WZDM.Test
{
    /// <summary>
    /// 限制并发线程数例程
    /// </summary>
    public class TestThread 
    {
        int _currentThreads = 0;
        int _maxThreads = 10;

        public void Test()
        {
            while (true)
            {
                //_maxThreads = new Random().Next(1, 10);
                for (int i=0; i<100; i++)
                {
                    // 在此进行数量限制
                    if (_currentThreads >= _maxThreads)
                        break;

                    // 开启线程
                    lock (typeof(TestThread))
                    {
                        _currentThreads++;
                        if (_currentThreads > _maxThreads)
                            _currentThreads = _maxThreads;

                        string currentInfo = string.Format("thread {0}/{1}", _currentThreads, _maxThreads);
                        Console.WriteLine(currentInfo + " start");
                        Thread thread = new Thread(new ParameterizedThreadStart(Run));
                        thread.Start(currentInfo);
                    }
                }

                System.Threading.Thread.Sleep(2000);
            }
        }

        // 线程任务
        void Run(object obj)
        {
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine("{0}:{1}", obj, i);
                Thread.Sleep(100);
            }
            Console.WriteLine("{0} finished", obj);


            lock (typeof(TestThread))
            {
                _currentThreads--;
                if (_currentThreads < 0)
                    _currentThreads = 0;
            }
        }
    }
}
--------------------编程问答--------------------  for (int i = 0; ; i++)  你牛啊 --------------------编程问答-------------------- CLR对线程池有限制

CLR1.x 30左右
CLR2.0 具体不记得了,多了很多
CLR4.0 不知道

自建线程没限制 --------------------编程问答-------------------- 才用线程池可以设置最大线程数。
其他没有 --------------------编程问答-------------------- 一个线程默认要保留1m的堆栈空间(用于局部变量,参数传递等)。
32位操作系统中一个程序默认可分配最多2G的内存地址。不断的开线程,最终会导致内存地址用完并抛出OutOfMemoryException异常。

你看到的“程序才吃到50M 左右”是该程序实际用到的内存,并不矛盾。 --------------------编程问答-------------------- 楼上的有点道理,学习了 --------------------编程问答-------------------- Win32单程序最大管理到2G内存
线程2000个左右

不虽然你看到占用只有50M,其实他占了虚拟内存2G了已经
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,