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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:28:01+00:00 2026-05-24T06:28:01+00:00

I wrote some code in java to push exceptions from a thread to the

  • 0

I wrote some code in java to push exceptions from a thread to the main thread, by queueing them in a wrapper (seperate class). Now, if there is any exception present in the wrapper, the thread dies. Then the exceptions have to be thrown on the main thread. There doesn’t seem to be a standard java event that is called on the main thread, so I had to use the join function on the thread somewhere. I can’t use join in the main thread, because then the main thread blocks. I can’t use it in another thread, then I couldn’t throw exceptions any more. I can’t think of any other way to throw exceptions on the main thread. Anyone here has an idea?

  • 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-24T06:28:01+00:00Added an answer on May 24, 2026 at 6:28 am

    As mentioned by @Ryan Stewart you can use Executros framework. In this case you do not create thread yourself: the big brother helps you to do that. You can use Callable instead of Runnable to implement your logic. In this case you can get Future and ask it what’s the thread status.

    Unfortunately I am afraid this helps you only partially because you have to call Future actively.

    If I understood you correctly you want your main thread to be notified when exception is thrown by one of wrorker threads. I do not think you should get exception from worker thread and re-throw it in main thread.

    I’d recommend you to use one of the following strategies.

    A. Use listener pattern. Create

    interface ExceptionListener {
        public void exceptionThrown(Exception e);
    }
    

    Create abstract class ExceptionNotifierThread extends Thread.
    This class will have collection of ExceptionListener and method addExceptionListener(ExceptionListener l).

    Method run() of the thread will catch all Exceptions and then call all exceptiton listeners in loop.

    Now you crete your thread and before calling its start() method you can register as an excption listener.

    That’s it. When thread throws exception your listener will be called back.

    B. It is a version of the same solution. Just instead of notification of listeners synchronously the thread my push excption to queue and everyone may read it from queue. User executors and Blocking queues for this.

    Good luck and welcome to stackoverflow!

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

Sidebar

Related Questions

I wrote some code to read a file in my Java Servlet class. (I'm
I'm trying to instantiate a class from variable, and wrote some test code. But,
I have wrote some code to compile a Java source code. It then produces
I have this Java code some other person wrote, specifically JSPs. I am trying
I wrote some Java code to generate pseudo-random numbers but I'm running into what
I wrote some code in Java using the pdfbox API that splits a pdf
I'd like to write some (java) code that takes a PDF document, and creates
I wrote some code with a lot of recursion, that takes quite a bit
I wrote some code recently (ISO/ANSI C), and was surprised at the poor performance
I just wrote some code to test the behavior of std::equal, and came away

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.