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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:31:45+00:00 2026-06-08T10:31:45+00:00

I have a question with my code. I have two threads running from the

  • 0

I have a question with my code.

I have two threads running from the main method & I want to catch the exception that can occur in any of the two threads in the main method.

Future<Object> incoming=Executors.newSingleThreadExecutor().submit(new Task1(param1));
Future<Object> outgoing=Executors.newSingleThreadExecutor().submit(new Task2(param2));

Problem is that if i use Future Object & call get() method for the exception, it will block my code & i would not know if say thread 2 has finished/throws exception before thread 1.

How can i handle this elegantly instead of this?

while(!(incoming.isDone() || outgoing.isDone())){}
  • 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-08T10:31:48+00:00Added an answer on June 8, 2026 at 10:31 am

    How about this:

    A queue shared between all threads (make sure to be thread safe!),

    Queue<Throwable> exceptionsToProcess;
    

    Then, lock up your main method with a while loop:

    //start threads, pass them the queue
    
    
    while(true)
    {
        Throwable t;
        while((t = exceptionsToProcess.poll()) == null);
        //process t
    }
    

    The exceptions will be processed in the correct order, although your run the risk of a ConcurrentModificationException if you’re not careful about thread safety.

    Edit: This might be a useful queue class for this purpose: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/LinkedBlockingQueue.html

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

Sidebar

Related Questions

I have two threads that want to synchonize on the same object. Thead A
Newbie question. I have two c# classes - a code class (say, CodeClass) and
Ok so here is a question. I have two projects in code igniter, say
I have question i.e how can i select a previous div CODE:- if($i >
I have a question about a programming problem from the book Cracking The Code
So I just have posted a question about this code (which was answered): $(document).ready(Main);
Possible Duplicate: Locking main() thread Below you'll find my code, Main calls two threads,
I have a class that has two method in it, one calls a class
Okay, i have this question in one regarding threads. there are two unsynchronized threads
I have std::list<Info> infoList in my application that is shared between two threads. These

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.