Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9148441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:16:29+00:00 2026-06-17T11:16:29+00:00

I have a multithreaded program where I want the one of the thread to

  • 0

I have a multithreaded program where I want the one of the thread to print the statement after all the thread finished.How can I do that? and How do I know that all the threads finished?

ExecutorService pool = Executors.newCachedThreadPool();
    for(int i = 0; i < myList.size(); ++i) {
            pool.execute (new ThreadProcessRunnable (args));
    }


    public class ThreadProcessRunnable implements Runnable {
           public void run() {


           System.out.println("last thread should execute this");
    }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:16:30+00:00Added an answer on June 17, 2026 at 11:16 am

    This is a REALLY basic example/concept of using a CountDownLatch

    public class TestCountDownLatch {
    
      private static CountDownLatch latch;
    
      public static void main(String[] args) {
    
        latch = new CountDownLatch(10);
        ExecutorService pool = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; ++i) {
          pool.execute(new Worker(i));
        }
      }
    
      public static class Worker implements Runnable {
    
        private int number;
    
        public Worker(int number) {
          this.number = number;
        }
    
        @Override
        public void run() {
    
          try {
            System.out.println(number + " is sleeping...");
            Thread.sleep((long) (Math.round(Math.random() * 1000)));
          } catch (InterruptedException ex) {
          }
    
          System.out.println(number + " is Completed...");
          latch.countDown();
    
          if (latch.getCount() == 0) {
    
            System.out.println(number + " was last...");
    
          }
    
        }
      }
    }
    

    Simple Single Thread Test Case

    public class TestCountDownLatch {
    
      private static CountDownLatch latch;
    
      public static void main(String[] args) {
    
        latch = new CountDownLatch(1);
        ExecutorService pool = Executors.newCachedThreadPool();
        for (int i = 0; i < 1; ++i) {
          pool.execute(new Worker(i));
        }
      }
    
      public static class Worker implements Runnable {
    
        private int number;
    
        public Worker(int number) {
          this.number = number;
        }
    
        @Override
        public void run() {
    
          try {
            System.out.println(number + " is sleeping...");
            Thread.sleep((long) (Math.round(Math.random() * 1000)));
          } catch (InterruptedException ex) {
          }
    
          System.out.println(number + " is Completed...");
          latch.countDown();
    
          if (latch.getCount() == 0) {
    
            System.out.println(number + " was last...");
    
          }
    
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multithreaded c program written with pthreads. I want to know if
I'm working on multithread program and I want to check that all threads all
Situation: I have a multithreaded program written in C. If one of the threads
Possible Duplicate: Is stl vector concurrent read thread-safe? I have a multi-threaded program that
I have a multithreaded program running which crashes after a day or two. Moreover
I have a multithreaded application that runs using a custom thread pool class. The
I have written a program in C#. Now I finished all the functionality and
Let's say I have a multithreaded C++ program that handles requests in the form
Can setting the cpu affinity in linux for a multithreaded program where each thread
I have a multithreaded program which uses pthread mutexes for synchronization on few integers.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.