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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:00:06+00:00 2026-05-23T09:00:06+00:00

I need some tasks to be done multi threaded. I know in advance that

  • 0

I need some tasks to be done multi threaded.
I know in advance that i will continue with my program when all tasks are completed.
Is the following code right for this purpose?

  public void test() {
    Callable<String> myCall = new Callable() {

        @Override
        public String call() throws Exception {
            return doDomething();
        }
    };


    Callable<String> myCall2 = new Callable() {

        @Override
        public String call() throws Exception {
            return doDomething2();
        }
    };

    ExecutorService executor = Executors.newFixedThreadPool(2);
    List<Callable<String>> list = Arrays.asList(myCall,myCall2);
    List<Future<String>> futuresList = executor.invokeAll(list);

    String result1 = futuresList.get(0).get();

    String result2 = futuresList.get(0).get();

    //...
}

I’m trying to change this to work with generics:

public void test() {
Callable<?> myCall = new Callable() {

    @Override
    public String call() throws Exception {
        return doDomething();
    }
};


Callable<?> myCall2 = new Callable() {

    @Override
    public String call() throws Exception {
        return doDomething2();
    }
};

ExecutorService executor = Executors.newFixedThreadPool(2);
List<Callable<?>> list = Arrays.asList(myCall,myCall2);
List<Future<?>> futuresList = executor.invokeAll((Collection<? extends Callable<?>>)list);

String result1 = futuresList.get(0).get();

String result2 = futuresList.get(0).get();

// ...
}

I get the following compilation error:

The method invokeAll(Collection<? extends Callable<T>>) in the type ExecutorService is not applicable for the arguments (Collection<capture#2-of ? extends Callable<?>>).

  • 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-23T09:00:06+00:00Added an answer on May 23, 2026 at 9:00 am

    Aside from you accessing the zeroth index twice I dont see anything wrong with it.

    This edit is in regards to your question on how to implement it without the ExecutorService. As ColinD notes, you really shouldn’t I will show why

    To get the same set of functionality you would need two Objects and use two threads as a latch

    String result1 = null;
    
    String result2 = null;
    
    public void test() {
        Thread thread1 = new Thread(new Runnable(){
            public void run(){
                result1 = doSomething();
            }
        });
        Thread thread2 = new Thread(new Runnable(){
            public void run(){
                result2 = doSomething2();
            }
        });
        thread1.start(); 
        thread2.start();
        thread1.join(); 
        thread2.join();
        ...
        ...
    
    }
    

    Now what if you want to add another thread/unit-of-work then you need

    Thread thread3 = new Thread(new Runnable(){
                public void run(){
                    result3 = doSomething3();
                }
            });
    thread3.start();
    thread3.join();
    

    And so forth. Hence your solution is the better way to do this.

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

Sidebar

Related Questions

I need to automate some tasks on a website that does not have an
I'm writing some db utility scripts, and one of the tasks I need to
I need to implement something. Something that could do some certain task in my
I'm struggling for a pattern here. I have quite a few tasks that need
I need to do some tasks before any Archive launched in Xcode. Is there
I need to execute some (let's say 10) tasks with less (i.e.) 3 actors.
My ruby application will be running on Windows XP. I need some kind of
Need help with the following. I am creating two different combinations of view/controllers that
I have some tasks in an application that are CPU bound and I want
Need some help about with Memcache. I have created a class and want to

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.