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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:02:43+00:00 2026-05-23T07:02:43+00:00

i write the following code: public void test() { Callable<?> myCall = new Callable()

  • 0

i write the following code:

 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<Future<?>> futuresList = executor.invokeAll((Collection<? extends Callable<?>>) getList());

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

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

...
...

}

private List<Callable<?>> getList() {
    .. create callables with wildcard and return them
}

i get the following compilation error: The method invokeAll(Collection>) in the type ExecutorService is not applicable for the arguments (Collection>).

EDIT
i added a method getList because i want this to use generics and not String.
I want to understand why it doesnt compile. and in my real program it is a method.

  • 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-23T07:02:44+00:00Added an answer on May 23, 2026 at 7:02 am

    You have to understand when you need wildcards in generics. In your example, you don’t need them at all. You only need them when you don’t know the type of some generic object. In your example, you want the Callables to return Strings so you should use as your generic type, like this:

    public void test() throws InterruptedException, ExecutionException {
    
      Callable<String> myCall = new Callable<String>(){
        public String call() throws Exception{
          return doDomething();
        }
      };
    
    
      Callable<String> myCall2 = new Callable<String>(){
        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(1).get();
    
      executor.shutdown();
    }
    

    See Sun’s / Oracle’s tutorial on generics and wildcards

    EDIT:

    • changed futuresList.get(0) to futuresList.get(1) to get the second result
    • added executor.shutdown() because people tend to forget this…

    EDIT2:

    Responding to your comment, here is another example. That’s one way you could do stuff like this. I want to show how you have to generify all involved method from the caller all the way down inside your Callables or better your doDomething methods.

    public void test() throws Exception {
    
      ExecutorService executor = Executors.newFixedThreadPool(2);
      List<Callable<String>> list = getList();
      List<Future<String>> futuresList = executor.invokeAll(list);
    
      String result1 = futuresList.get(0).get();
      String result2 = futuresList.get(1).get();
    
      System.out.println(result1);
      System.out.println(result2);
    
      executor.shutdown();
    }
    
    private <T> List<Callable<T>> getList() {
    
      Callable<T> myCall = new Callable<T>(){
        public T call() throws Exception{
          return (T) "a"; //doDomething();
        }
      };
    
      Callable<T> myCall2 = new Callable<T>(){
        public T call() throws Exception{
          return (T) "b"; //doDomething2();
        }
      };
    
      return Arrays.asList(myCall, myCall2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: MemoryStream foo(){ MemoryStream ms = new MemoryStream(); // write
Suppose I write the following code: public ref class Data { public: Data() {
How can I write the following code quickly in emacs? \newcommand{\cA}{\mathcal A} \newcommand{\cB}{\mathcal B}
I am a AS3 novice learning PureMVC and want to write code following best
I am trying to use the following code to write data into an excel
I'm trying to write a piece of code that will do the following: Take
I needed some simple string encryption, so I wrote the following code (with a
In C# I could easily write the following: string stringValue = string.IsNullOrEmpty( otherString )
Suppose someone (other than me) writes the following code and compiles it into an
In C++Builder, I wrote the following code (in Button1Click handler), When I run in

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.