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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:11:46+00:00 2026-06-07T06:11:46+00:00

I am using java.util.concurrent package to create a parallel program. I have 2 threads:

  • 0

I am using java.util.concurrent package to create a parallel program. I have 2 threads:

  • thread-1 which invokes webservice method-1, and
  • thread-2 which invokes webservice method-2.

I am specifying a thread execution timeout – suppose if thread-1 does not complete execution within the specified timeout then I have to intercept thread-1, continue the execution with thread-2 and display thread-2 results in jsp (note: if both threads take too much time to process the requests then I don’t want the UI to wait until they complete).

I tried with the code below but it throws an InterruptedException. How can I proceed with other tasks when one task takes more mire?

ExecutorService executor = Executors.newFixedThreadPool(2);
CompletionService<ArrayList<AvailableWeatherDetailVO>> compService =   new ExecutorCompletionService<ArrayList<AvailableWeatherDetailVO>>(executor);
// Start amazonTask using thread-1
try{
   compService.submit(amazonTask).get(20, TimeUnit.MILLISECONDS);
   amazonFuture = compService.take();
  amazonFinalList =(ArrayList<AvailableWeatherDetailVO>)amazonFuture .get()                 }
catch (TimeoutException e) {
  compService.submit(amazonTask).cancel(true);
  //throw new TimeoutException("Thread not executed with in speifed time");
 }
// Start googleTask using thread-2
try{
   compService.submit(googleTask).get(100, TimeUnit.MILLISECONDS);
   googleFuture = compService.take();
  googleFinalList =(ArrayList<AvailableWeatherDetailVO>)googleFuture .get()                 }
catch (TimeoutException e) {
  compService.submit(googleTask).cancel(true);
  //throw new TimeoutException("Thread not executed with in speifed time");
 }
  • 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-07T06:11:48+00:00Added an answer on June 7, 2026 at 6:11 am

    It is not clear if you mean thread2 only begins execution if thread1 fails.

    Or, are they both executing, but you want to use the result of thread1 if it completes, else you want to use the result of thread2

    It sounds to me like you should attach thread 1 to a Future. Then get the result (from your main thread) with a timeout. If the Future.get() times out, then get the result from the second thread.

    Not sure if I got your meaning. If this is not what you are looking for, please clarify.

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

Sidebar

Related Questions

I have a problem with Observer-Pattern and deadlock using threads. package observerDeadLock; import java.util.Observable;
I'm experimenting Java Multi-Threading using synchronization on method comparing with Atomic variables (java.util.concurrent.atomic package).
I have a thread pool created using java.util.concurrent.ThreadPoolExecutor Is there anyway I can wait
I create a java.util.concurrent.locks.ReentrantReadWriteLock using new java.util.concurrent.locks.ReentrantReadWriteLock().readLock() and then I pass to a method
I have realized some logic using integers import java.util.concurrent.locks.*; public class MassageSalon implements Salon{
I'm new to the package java.util.concurrent . I've created the following program testing if
Using the java.util.concurrent.locks.ReentrantLock library as follows: Two threads generate a random number and use
Exception in thread Thread-8 java.lang.StackOverflowError at sun.misc.Unsafe.compareAndSwapLong(Native Method) at java.util.concurrent.atomic.AtomicLong.compareAndSet(Unknown Source) at java.util.Random.next(Unknown Source)
I'm using java.util.concurrent.ExecutorService with fixed thread pool to execute list of tasks. My list
I am using java.util.Timer class and I am using its schedule method to perform

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.