Manish Kumar
Manish Kumar

Reputation: 10482

How many threads are running in my java program?

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

   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?

Upvotes: 1

Views: 132

Answers (3)

Lokesh
Lokesh

Reputation: 7940

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.

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

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

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.

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

   }
}

Upvotes: 0

Anders R. Bystrup
Anders R. Bystrup

Reputation: 16050

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?!

Cheers,

Upvotes: 1

copeg
copeg

Reputation: 8348

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

Upvotes: 1

Related Questions