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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:41:56+00:00 2026-06-06T12:41:56+00:00

I have to call more than one webservice in one method each webservice is

  • 0

I have to call more than one webservice in one method each webservice is executed by separate thread in concurrent/parellel. Every web service will return one ArrayList. Note: may chance some webservices will fail or take more time process response in this case i have to skip these failure result. How can I achieve this? I tried this sample code.

public class MultiThreadsEx{     

    public class Task implements Runnable {  
    private Object result;          
    private String id;      
    int maxRowCount = 0;    
    public Task(String id) {              
        this.id = id;          
    }            
    public Object getResult() {
        return result;          
    }            
    public void run() {              

        try {  
            System.out.println("Running id=" + id+" at "+Utilities.getCurrentJavaDate("DD/MM/YYYY HH:MM:SS"));  
            if(id.equalsIgnoreCase("1")){
                /**Getting Details from Amazon WS*/
                maxRowCount = AmazonUtils.getweather(cityname);
            }else if(id.equalsIgnoreCase("2")){
                /**Getting Details from Google WS* /
                maxRowCount = GoogleUtils.getWeather(cityName);
            }
            // call web service                  
            //Thread.sleep(1000);                  
            //result = id + " more";      
            result = maxRowCount;
        } catch (InterruptedException e) {                  
            // TODO do something with the error                 
            throw new RuntimeException("caught InterruptedException", e);              
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }         
    }      
    }       
    public  static void runInParallel(Runnable runnable1, Runnable runnable2) {          
        try {              
            Thread t1 = new Thread(runnable1);  
            Thread t2 = new Thread(runnable2);                
            t1.start();              
            t2.start();                

        } catch (Exception e) {             
            // TODO do something nice with exception              
            throw new RuntimeException("caught InterruptedException", e);          
        }      
    }        
    public void foo() {  
        try {
            Task task1 = new Task("1");   
            Task task2 = new Task("2");            
            runInParallel(task1, task2);            
            System.out.println("task1 = " + task1.getResult()+" at "+Utilities.getCurrentJavaDate("DD/MM/YYYY HH:MM:SS"));          
            System.out.println("task2 = " + task2.getResult()+" at "+Utilities.getCurrentJavaDate("DD/MM/YYYY HH:MM:SS"));
        } catch (Exception e) {
            //TODO Auto-generated catch block
            e.printStackTrace();
        }       
    } 

But run() return type is void so how can return result? Examples are highly appreciated. I am new to multithread/concurrent threads concept so if I’m doing anything wrong, please point me in the right direction.

  • 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-06T12:41:56+00:00Added an answer on June 6, 2026 at 12:41 pm

    Consider replacing Runnable - run with Callable - call. This will allow you to return a result from your thread task:

    public class Task implements Callable<Object> { 
    
         private Object result;
    
         public Object call() {
              // compute result
              return result;
         } 
    }
    

    Now use an ExecutorService:

    public static void runInParallel(Callable<Object> c1, Callable<Object> c2) {                       
            ExecutorService exec = Executors.newFixedThreadPool(2);
            Future<Object> f1 = exec.submit(c1);
            Future<Object> f2 = exec.submit(c2);
    }
    

    Later in the code you can use f1.get() and f2.get() to wait for the results of the tasks.

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

Sidebar

Related Questions

how to call more than one method at same time in my page load
I have more than one user control on my aspx page, and each user
I have a jar file of my application which has more than one class.
Is there a way in Python, to have more than one constructor or more
I have a program which I need to call more than 100 times with
I have been asked in an interview how one can return more than one
I have an error page which, I would like to call from more than
I know for more than one result, you don't have much choice for using
Is it possible to query more than one insights metric in one API call?
I have to call an existing AMF Method in a PHP (or Perl) Script

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.