我的java程序中运行了多少个线程?

时间:2021-12-22 06:59:01

I am running simple program and here is log

我正在运行简单的程序,这是日志

Total Thread : 6  // using Thread.activeCount()
pool-1-thread-143

Here is class

这是上课

public class Test implements Runnable{

    String              ul;
    ExecutorService     threadPool;
    public Test(String s, ExecutorService executor)
    {
        this.ul = s;
        threadPool  =   executor;
    }

    public void run() {
        try {
            Fun(ul);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void Fun(String ss) throws IOException
    {

    // .... 
        System.out.println("Total Thread : "+Thread.activeCount());
       Iterator iterator = links.iterator();
       while(iterator.hasNext())
       {
          Element ele = iterator.next();
          String s = ele.getprop("....");
          if(!Model.condition(s))
          {
              System.out.println(Thread.currentThread().getName());
              threadPool.execute(new Test(s, threadPool));
          }
       }        
    }
}

Here is Main.java

这是Main.java

   ExecutorService executor = Executors.newFixedThreadPool(5);        
    executor.execute(new Test("this is something", executor));          
    while (!executor.isTerminated()) {   }         

So how many actual thread is running by my program ? Have I implemented executor.execute() correct in this program?

那么我的程序运行了多少实际线程?我在这个程序中实现了executor.execute()吗?

3 个解决方案

#1


The Executors call creates a fixed size thread pool of 5. The application starts on a main thread...thus 5+1 = 6

Executors调用创建一个固定大小的线程池5.应用程序在主线程上启动...因此5 + 1 = 6

#2


One thread for the main program and five threads in the pool for a total of 6 as you see. And it's a perfectly good use of a Threadpool, although it seems a bit convoluted that the Test executes a new Test?!

主程序的一个线程和池中的五个线程,总共6个,如您所见。而且这是一个非常好用的Threadpool,虽然看起来有点费解,Test执行一个新的测试?!

Cheers,

#3


Implementation of code is a bit clumsy. This portion looks a bit ugly threadPool.execute(new Test(s, threadPool)); as you are passing reference of variable as parameter which itself is making call to method.

代码的实现有点笨拙。这部分看起来有点难看threadPool.execute(new Test(s,threadPool));因为你传递变量的引用作为参数,它本身正在调用方法。

I would rather keep Thread pool executor outside Test class and pass multiple instance of test class to it.

我宁愿将线程池执行器保留在Test类之外,并将多个测试类实例传递给它。

Count as explained in answers above is 5 + 1 main thread.

上面的答案中解释的计数是5 + 1主线程。

This is how i will do it. Main method can be improved further by adding more modularity but aim is ThreadPool should exist outside Runnable implementation.

这是我将如何做到这一点。可以通过添加更多模块化来进一步改进主要方法,但目标是ThreadPool应该存在于Runnable实现之外。

public class Test implements Runnable{

    String              ul;    
    public Test(String s)
    {
        this.ul = s;        
    }

    public void run() {
        try {
            Fun(ul);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void Fun(String ss) throws IOException
    {

    // .... 
        System.out.println("Total Thread : "+Thread.activeCount());

        System.out.println(Thread.currentThread().getName());

       }        
    }
}

public class MyRunnerClass {

   public static void main(String args[]){
     ExecutorService executor = Executors.newFixedThreadPool(5);
     String s = "this is something";

      Iterator iterator = links.iterator();
      while(iterator.hasNext())
       {
          Element ele = iterator.next();
          String s = ele.getprop("....");
          if(!Model.condition(s))
          {              
              executor.execute(new Test(s, threadPool));
          }
       } 

   }
}

#1


The Executors call creates a fixed size thread pool of 5. The application starts on a main thread...thus 5+1 = 6

Executors调用创建一个固定大小的线程池5.应用程序在主线程上启动...因此5 + 1 = 6

#2


One thread for the main program and five threads in the pool for a total of 6 as you see. And it's a perfectly good use of a Threadpool, although it seems a bit convoluted that the Test executes a new Test?!

主程序的一个线程和池中的五个线程,总共6个,如您所见。而且这是一个非常好用的Threadpool,虽然看起来有点费解,Test执行一个新的测试?!

Cheers,

#3


Implementation of code is a bit clumsy. This portion looks a bit ugly threadPool.execute(new Test(s, threadPool)); as you are passing reference of variable as parameter which itself is making call to method.

代码的实现有点笨拙。这部分看起来有点难看threadPool.execute(new Test(s,threadPool));因为你传递变量的引用作为参数,它本身正在调用方法。

I would rather keep Thread pool executor outside Test class and pass multiple instance of test class to it.

我宁愿将线程池执行器保留在Test类之外,并将多个测试类实例传递给它。

Count as explained in answers above is 5 + 1 main thread.

上面的答案中解释的计数是5 + 1主线程。

This is how i will do it. Main method can be improved further by adding more modularity but aim is ThreadPool should exist outside Runnable implementation.

这是我将如何做到这一点。可以通过添加更多模块化来进一步改进主要方法,但目标是ThreadPool应该存在于Runnable实现之外。

public class Test implements Runnable{

    String              ul;    
    public Test(String s)
    {
        this.ul = s;        
    }

    public void run() {
        try {
            Fun(ul);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void Fun(String ss) throws IOException
    {

    // .... 
        System.out.println("Total Thread : "+Thread.activeCount());

        System.out.println(Thread.currentThread().getName());

       }        
    }
}

public class MyRunnerClass {

   public static void main(String args[]){
     ExecutorService executor = Executors.newFixedThreadPool(5);
     String s = "this is something";

      Iterator iterator = links.iterator();
      while(iterator.hasNext())
       {
          Element ele = iterator.next();
          String s = ele.getprop("....");
          if(!Model.condition(s))
          {              
              executor.execute(new Test(s, threadPool));
          }
       } 

   }
}