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 8481331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:32:08+00:00 2026-06-10T19:32:08+00:00

I have a multi-threaded batch processing app that runs anywhere between 5-10 concurrent threads

  • 0

I have a multi-threaded batch processing app that runs anywhere between 5-10 concurrent threads of execution. Their data segments are carefully sliced to be as evenly distributed as possible but, of course, the execution times always vary. What I wanna do is to invoke one last kind of onFinalize method when the last thread finishes, which will do some stat computation.

I was wondering if the best way to know for a thread that it is the last one (rather than querying the DB, which seems kind of pedestrian) is to have a static var which would be incremented in a synchronized block when a new thread is added and decremented when each thread finishes. So when a thread finishes and does its decrementing, I can have an if to see if the outstanding # of threads is 0 and then invoke the final stats.

That is what I was thinking. I was wondering if there is a better, more elegant, or bulletproof way of accomplishing this.

Using Java 7

Thanks

  • 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-10T19:32:09+00:00Added an answer on June 10, 2026 at 7:32 pm

    So what else you can do is that you can create a ThreadManager class. This class should have a List<Thread>. Whenever you create a new Thread to execute your task you should register it in ThreadManager. Then create a method in ThreadManager which returns the total number of live Threads in the List.

    In this way you can at any point see how many threads are running and also you can perform any other operation on the running threads if you want. Also one more Thread should run periodically which should clear all dead Thread references from the List.

    Sample ThreadManager can look like this:

    public class ThreadManager {
        private static ThreadManager tm;
        private List<Thread> threads;
    
        public static ThreadManager get() {
            if(null == tm) {
                tm = new ThreadManager();
            }
    
            return tm;
        }
    
        private ThreadManager() {
            threads = new ArrayList<Thread>();
        }
    
        public int getCountOfAliveThreads() {
            int count = 0;
            for(Thread t : threads) {
                if(t.isAlive()) {
                    count++;
                }
            }
    
            return count;
        }
    }
    

    Hope this helps. You can perform other functions aswell on the Thread List.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi-threaded app that uses Core Data. I've been seeing a lot
I have a multi threaded .NET app that uses async I/O and AsyncCallbacks to
Possible Duplicate: Is stl vector concurrent read thread-safe? I have a multi-threaded program that
I have written a multi threaded java code, which when runs creates 8 threads
I have multi-threaded section where threads need to allocate several large segments of data,
I have a multi threaded app that writes log to file. occasionaly, saving fails
If you have a multi-threaded application with a word variable shared between threads (ie
I have a multi threaded JAVA app that uses a shared c lib. What
I have a multi-threaded Cocoa app that processes images. The program has a progress
Are multi-threaded CLR stored procs possible? I have a data-intensive task with lots of

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.