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

  • Home
  • SEARCH
  • 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 6885203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:36:19+00:00 2026-05-27T05:36:19+00:00

I have some experience with threading in java, but I am wondering… What’s the

  • 0

I have some experience with threading in java, but I am wondering…

What’s the best practise to store multiple threads where I can access them both individually and as a group?

My own solution is to create a threadArray class but naturally I would prefer a native class that is much more reliable.

Thanks in advance!


edit

Apparently the functionality is of great importance of the best method. Well, I’ll give an example:

I’ve got an application that basically searches through a lot of information at the same time hence I’m using threads. The threads however each need to perform just a part of the entire operation so I wish to add additional parameters to specify a range.

When a thread finishes it’s specific search it can just stop naturally. When a thread finds a result however, I wish to stop all threads and retrieve that result.

Does that help?

  • 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-05-27T05:36:19+00:00Added an answer on May 27, 2026 at 5:36 am

    I’d use an ExecutorService to manage my threads as a pool and put the Futures I am given when adding my Threads to the pool in some form of Collection.

    In this way you can manage all of the threads as one unit via the Executor and track individual threads through their Future.

    Edit in response to yours

    You can use the shutdownNow method of the ExecutorService to abort all running threads.

    Example (not a solution to your problem but covers most benefits of using Executors):

    // Thread pool for the collectors.
    ExecutorService threads = Executors.newFixedThreadPool(MAX_THREADS);
    ...
    // Futures of all collectors running in the pool.
    ConcurrentLinkedQueue<Future> collectors = new ConcurrentLinkedQueue<Future>();
    ...
    // Make my Callable.
    Callable<Void> c = new FileListCollector(path, recurse, filter);
    // Start it up and keep track of it so we can find out when it has finished.
    collectors.add(threads.submit(c));
    ...
    
    // Called when nothing in queue.
    private void checkForFinished() {
      // Count the running threads.
      int runningThreads = 0;
      try {
        // Track dead ones to remove.
        List<Future> deadThreads = new LinkedList<Future>();
        // Walk all collectors.
        for (Future f : collectors) {
          // I've seen f being null once. No idea how.
          if (f != null) {
            // If it's not done then it's running.
            if (!f.isDone()) {
              // Count it.
              runningThreads += 1;
            } else {
              // Mark for deletion.
              deadThreads.add(f);
            }
          }
        }
        // Clear dead threads - just to be tidy.
        collectors.removeAll(deadThreads);
      } catch (ConcurrentModificationException cme) {
        // Probably a new thread has been started while I was checking!
        // Therefore almost certainly NOT all finished.
        runningThreads += 1;
      }
      // If no threads are left, we're done.
      if (runningThreads == 0) {
        // Finished! Close everything down.
        close();
      }
    }
    
    // Close down the whole system.
    public void close() {
      // Use the fileQueue state to indicate closed.
      if (!fileQueue.isClosed()) {
        // Close the queue ... unblocking all collectors (I hope).
        fileQueue.close();
        // Shut them down agressively as this may be called by user prematurely as well as by me.
        threads.shutdownNow();
        // Wait until all is done.
        boolean terminated = false;
        do {
          try {
            // Wait up to 1 second for termination.
            terminated = threads.awaitTermination(1, TimeUnit.SECONDS);
          } catch (InterruptedException ex) {
            // Ignore the interrupt! If I exit here we will probably leak.
          }
        } while (!terminated);
        log("! All done");
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some experience in working with java, but many are telling MATLAB will
I have some experience in Python and Java, but I dont really know the
I'm pretty new to Android development, but I have some experience with Java and
I have some experience making multiplayer turn-based games using sockets, but I've never attempted
I have some experience using parallel extensions in .Net development, but I was looking
I have some experience in ASP.Net and can work my way around it without
I have some experience writing web applications in Java for class projects. My first
Hi I have some experience with programming but I'm not very good with pointers.
I'm new to NAnt but have some experience with Ant and CruiseControl. What I
I am working on a rails application (I have some experience with rails). But,

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.