想测试一下平台支持的最大的线程数,想知道有没有这样的示例程序。
谢谢!

解决方案 »

  1.   

    public class SimpleThread extends Thread 
    {
      private static int MAX_THREAD = 10000;
      private static int threadCount = 0;
      public SimpleThread() {
        ++threadCount;
        System.out.println("Making " + threadCount);
      }
      public void run() {
        while(true) {
         try {
           sleep(1000);
           }catch(InterruptedException e)
           {
           e.printStackTrace();
           }
        }
      }
      public static void main(String[] args) throws Exception{
        for(int i = 0; i < MAX_THREAD; i++)
          new SimpleThread().start();
      }
    } 我用10000 但是运行到7000多时候就内存溢出了. 你可以测试一下你的机器.适当增加线程数.
      

  2.   

    十分谢谢!可为什莫我捕获不到java.lang.OutOfMemoryError异常。
      

  3.   

    public class SimpleThread extends Thread 
    {
      public static boolean flag = false; 
      private static int MAX_THREAD = 10000;
      private static int  threadCount = 0;
      public SimpleThread() {
        ++threadCount;
        System.out.println("Making " + threadCount);
      }
      public void run() {
        while(true) {
         try {
           sleep(1000);
           if (flag)
           {
           //System.out.println("Thread Destroy:" + (--threadCount));
          
             break;
           }
           }catch(InterruptedException e)
           {
           e.printStackTrace();
           }
        }
      }
      public static void main(String[] args)
      {
       int i = 0;
          try {
         for(; i < MAX_THREAD; i++)
           new SimpleThread().start();
           }catch (Throwable e)
           {
           System.out.println("最大线程是:"+ (i+1));
           //e.printStackTrace();
           flag = true;
           }
      }
    } ...  ...Making 7229
    Making 7230
    Making 7231
    Making 7232
    Making 7233
    最大线程是:7233F:\>
      

  4.   

    用 Throwable 捕获.OutOfMemoryError 不属于Exception 范围
      

  5.   

    Thanks, I will test it later.I sent a PM to you, please check!