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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:08:51+00:00 2026-06-09T12:08:51+00:00

I am trying to understand how a Callable is able to return a value

  • 0

I am trying to understand how a Callable is able to return a value when it is run on a different thread.

I am looking in the classes Executors, AbstractExecutorService, ThreadPoolExecutor and FutureTask, all available in java.util.concurrent package.

You create an ExecutorService object by calling a method in Executors (e.g. newSingleThreadExecutor()). Then you can pass a Callable object with ExecutorService.submit(Callable c).

Since the call() method is run by a thread provided by the ExecutorService, where does the returned object “jump” back to the calling thread?

Look at this simple example:

1    ExecutorService executor = Executors.newSingleThreadExecutor();
2    public static void main(String[] args) {
3       Integer i = executor.submit(new Callable<Integer>(){
4           public Integer call() throws Exception {
5              return 10;
6           }
7       }).get();
8       System.out.print("Returns: " + i + " Thread: " + Thread.currentThread.getName());
9       // prints "10 main"
10    }

How is it possible that the integer in the call method, which is run by a separate thread, is returned to the Integer object (row 3) so it can be printed by the System.out statement in the main thread (row 7)?

Isn´t it possible for the main thread to be run before the ExecutorService has run its thread, so that the System.out statement prints null?

  • 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-09T12:08:53+00:00Added an answer on June 9, 2026 at 12:08 pm

    How is it possible that the integer in the call method, which is run by a separate thread, is returned to the Integer object

    ExecutorService.submit(...) does not return the object from call() but it does return a Future<Integer> and you can use the Future.get() method to get that object. See the example code below.

    Isn´t it possible for the main thread to be run before the ExecutorService has run its thread, so that the System.out statement prints null?

    No, the get() method on the future waits until the job finishes. If call() returned null then get() will otherwise it will return (and print) 10 guaranteed.

    Future<Integer> future = executor.submit(new Callable<Integer>(){
        public Integer call() throws Exception {
           return 10;
        }
    });
    try {
       // get() waits for the job to finish before returning the value
       // it also might throw an exception if your call() threw
       Integer i = future.get();
       ...
    } catch (ExecutionException e) {
       // this cause exception is the one thrown by the call() method
       Exception cause = e.getCause();
       ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to understand why you can't use sql_variant for the value type when using
I am trying understand ViewModels deeper and I have read many articles and blogs
Trying to understand what's the correct way of implementing OpenID authentication with Spring Security.
Trying to understand the Deezer API. When I visit: http://connect.deezer.com/oauth/auth.php?app_id=MY_APP_ID&redirect_uri=http://mydomain.me&perms=basic_access I end up at
Trying to understand PNG format. Consider this PNG Image: The Image is taken from
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand something. I created a d:\svn\repository on my server. I committed folders
Trying to understand the math of this code snippet. A token is provided which
Im trying to understand how class generics work and this bit just doesnt make

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.