• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us

How to Use the getCompletedTaskCount() Method of ThreadPoolExeceutor in Java

September 26, 2023 //  by Talha Malik

In Java, the execution of tasks is handled by the thread Pools. These pools manage the execution of tasks using threads that are either previously used or newly created. These pools are implemented using the ThreadPoolExecutor class of the “util.concurrent” package to execute the provided tasks. To keep track of the completed tasks within these thread pools, the getCompletedCount() method of Java’s ThreadPoolExecutor class is used.

In this article, the getCompletedTaskCount() method of ThreadPoolExector in Java will be explained in detail.

How to Get the Number of Completed Tasks in Java?

The ThreadPoolExecutor class uses a pool of threads to execute the tasks. There are various methods present in the thread pool executor class of Java. One of these methods is the getCompletedTaskCount() method which provides the number of tasks that have been completed. The thread state and the tasks change dynamically therefore, we can only make an approximation of the count.

How to Use the getCompletedTaskCount() Method of ThreadPoolExeceutor in Java?

To use the getCompletedTaskCount() method in Java, first, import the required packages and create an object of the ThreadPoolExecutor class. Afterward, you can use the getCompletedTaskCount() method on the created ThreadPoolExecutor object to retrieve the number of completed tasks.

The syntax of the getCompletedTaskCount() is represented below:

obj.getCompletedTaskCount()

Here, obj represents the object of Java’s ThreadPoolExecutor class.

Example: Implementation of the getCompletedTaskCount()

The code below depicts the implementation of the method.

//Import the required packages
import java.util.concurrent.*; 
import java.lang.Thread;
//Declare the class for the task count method.
class taskcount  { 
    public static void main(final String[] arguments) throws InterruptedException { 
                ThreadPoolExecutor etask = (ThreadPoolExecutor)Executors.newCachedThreadPool();
    //The submit() method is responsible for submitting the tasks.
            etask.submit(new getcount()); 
            etask.submit(new getcount()); 
            etask.submit(new getcount()); 
    // Print the completed tasks
            System.out.println("The task count that has been completed is : "+ etask.getCompletedTaskCount()); 
    // The shutdown() method runs the present tasks and new tasks are accepted.
            etask.shutdown(); 
          }   
    // The Runnable Interface implements the getcount class.
      static class getcount implements Runnable { 
    @Override 
    // The run() method is implemented using the Runnable Interface.
      public void run() { 
        try { 
            Long sleeptime = (long) (Math.random() * 6); 
    // The currently running threads have been displayed using the getName() method.
            System.out.println("Currently running thread: " + Thread.currentThread().getName());
            TimeUnit.SECONDS.sleep(sleeptime); 
    // The threads that have completed running.
            System.out.println("The thread that is completed: " +Thread.currentThread().getName()); 
    //The Interrupted exception for the threads.
          } catch (InterruptedException f) {  } 
      } 
  } 
}

In the above Java code:

  • A class for the method is declared.
  • The cached thread pool is created for the new threads or the previously created ones are used for execution of tasks. 
  • Three tasks are submitted for execution using the submit() method.
  • The number of completed tasks is counted using the getCompletedTaskCount() method.
  • The shutdown() method ends the tasks that are accepted for execution.
  • The threads that have started are shown by the run() method which is implemented by the Runnable interface.
  • The threads that have been running and the threads that have completed running are printed using the println() method.

Output

In the below output, the threads running currently are displayed. The total number of tasks along with the completed threads are printed as output. 

Conclusion

To use the getCompletedTaskCount() method in Java, first, import the required packages and create an object of the ThreadPoolExecutor class. Afterward, you can use the getCompletedTaskCount() method on the created ThreadPoolExecutor object to retrieve the number of completed tasks. In this write-up, we have elaborated through code examples the implementation of the getCompletedTaskCount() method in Java.

Category: Java

Previous Post: « How to Use Mouse Listeners in Java
Next Post: How to Implement shutdown() Method of ThreadPoolExeceutor in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Use Math.min() Method in Java?

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact