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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:19:42+00:00 2026-06-15T19:19:42+00:00

In the case of a thread throwing an exception, how can I wait until

  • 0

In the case of a thread throwing an exception, how can I wait until all threads that did not throw an exception have finished (so the user doesn’t launch again until everything has stopped)?

I use GPars in several different ways, so I need a strategy for each (parallel collections, async closures, and fork/join). The exceptions are not getting buried, they are nicely handled via promises, getChildrenResults, etc., so that’s not an issue (thanks to Vaclav Pech’s answers). I just need to make sure that the main thread waits until anything that was still running gets to complete or is otherwise stopped.

For instance, when using parallel collections, some threads continue to run, while some never launch after the exception. So it’s not easy to tell how many are out there to wait on, or to get a hold of them possibly.

My guess is maybe there’s a way to work with the Thread pool (GParsPool in this case). Any suggestions?

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-15T19:19:44+00:00Added an answer on June 15, 2026 at 7:19 pm

    I believe I have a solution for the problem, I implemented it in the application after thorough testing and it works.

    The withPool closure passes in the created pool (a jsr166y.ForkJoinPool) as the first argument. I can grab that and store it off in a variable (currentPool), to be used later by the main thread, like so:

        GParsPool.withPool { pool ->
            currentPool = pool
    

    When an exception is thrown, and goes back up to the main thread for handling, I can make it wait until everything is finished, something like this:

        } catch (Exception exc) {
            if (currentPool) {
                while (!currentPool.isQuiescent()) {
                    Thread.sleep(100)
                    println 'waiting for threads to finish'
                }
            }
    
            println 'all done'
        }
    

    The isQuiescent() seems to be a safe way to make sure there’s no more work to be done.

    Note that during testing, I also found that exceptions didn’t seem to terminate execution of the loop as I originally thought. If I had a list of 500 and did an eachParallel, they all ran regardless if the 1st one through had an error. So I had to terminate the loop by using currentPool.shutdownNow() inside the parallel loop’s exception handler. See also: GPars – proper way to terminate a parallel collection early

    Here is a complete simplified representation of the actual solution:

    void example() {
        jsr166y.ForkJoinPool currentPool
    
        AtomicInteger threadCounter = new AtomicInteger(0)
        AtomicInteger threadCounterEnd = new AtomicInteger(0)
    
        AtomicReference<Exception> realException = new AtomicReference<Exception>()
    
        try {
            GParsPool.withPool { pool ->
                currentPool = pool
    
                (1..500).eachParallel {
                    try {
                        if (threadCounter.incrementAndGet() == 1) {
                            throw new RuntimeException('planet blew up!')
                        }
    
                        if (realException.get() != null) {
                            // We had an exception already in this eachParallel - quit early
                            return
                        }
    
                        // Do some long work
                        Integer counter=0
                        (1..1000000).each() {counter++}
    
                        // Flag if we went all the way through
                        threadCounterEnd.incrementAndGet()
                    } catch (Exception exc) {
                        realException.compareAndSet(null, exc)
    
                        pool.shutdownNow()
                        throw realException
                    }
                }
            }
        } catch (Exception exc) {
            // If we used pool.shutdownNow(), we need to look at the real exception.
            // This is needed because pool.shutdownNow() sometimes generates a CancellationException
            // which can cover up the real exception that caused us to do a shutdownNow().
            if (realException.get()) {
                exc = realException.get()
            }
    
            if (currentPool) {
                while (!currentPool.isQuiescent()) {
                    Thread.sleep(100)
                    println 'waiting for threads to finish'
                }
            }
    
            // Do further exception handling here...
            exc.printStackTrace()
        }
    }
    

    Going back to my earlier example, if I threw an exception on the 1st time through on a 4-core machine, there were about 5 threads queued up. The shutdownNow() would cut things off after around 20 or so threads had gotten through, so having the ‘quit early’ check near the top helped those 20 or so quit as soon as possible.

    Just posting it here in case it helps somebody else, in return for all the help I’ve gotten here. Thanks!

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

Sidebar

Related Questions

As we all know in case of multiple threads each thread maintains it's separate
A thread can use Object.wait() to block until another thread calls notify() or notifyAll()
I want to throw an exception from a runnable thread but its not possible
I have the following use-case: I have code executing in Thread A (not EDT).
In my application, I'm having functionality like twitter that when you have not refreshed
I have problem understanding the meaning of join in the following case: Thread t1=new
You have two threads, a and b. Thread a is in a forever loop,
I have a multithreaded openCV program that uses 4 threads to do the following:
I have 2 concurrent threads that at the same time enter a (Spring) transaction
I have a thread that updates it's state from time to time and I

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.