How to pass a parameter to a thread and get a return value?

pmichna
public class CalculationThread implements Runnable {

    int input;
    int output;

    public CalculationThread(int input)
    {
        this.input = input;
    }

    public void run() {
        output = input + 1;
    }

    public int getResult() {
        return output;
    }
}

Somewhere else:

Thread thread = new Thread(new CalculationThread(1));
thread.start();
int result = thread.getResult();

Of course, thread.getResult() doesn't work (it tries to invoke this method from the Thread class).

You get what I want. How can I achieve this in Java?

Andrey Chaschev

This a job for thread pools. You need to create a Callable<R> which is Runnable returning a value and send it to a thread pool.

The result of this operation is a Future<R> which is a pointer to this job which will contain a value of the computation, or will not if the job fails.

public static class CalculationJob implements Callable<Integer> {
    int input;

    public CalculationJob(int input) {
        this.input = input;
    }

    @Override
    public Integer call() throws Exception {
        return input + 1;
    }
}

public static void main(String[] args) throws InterruptedException {
    ExecutorService executorService = Executors.newFixedThreadPool(4);

    Future<Integer> result = executorService.submit(new CalculationJob(3));

    try {
        Integer integer = result.get(10, TimeUnit.MILLISECONDS);
        System.out.println("result: " + integer);
    } catch (Exception e) {
        // interrupts if there is any possible error
        result.cancel(true);
    }

    executorService.shutdown();
    executorService.awaitTermination(1, TimeUnit.SECONDS);
}

Prints:

result: 4

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pass a parameter to a thread and get a return value?

From Java

How to get the return value from a thread in python?

From Dev

how to get return value when a thread is done

From Dev

how to get value of parameter and pass it to a stored procedure?

From Dev

How to pass a parameter in an expectation to the return value in Rhino Mocks?

From Dev

how to pass askopenfilename() return value as a parameter to the builtin python function open

From Dev

How to get "pass parameter"

From Dev

Pass parameter to a method and read the return value?

From Dev

Pass parameter to a method and read the return value?

From Dev

How to get return value of a function using context as parameter?

From Dev

How to pass DateTime value parameter?

From Dev

How to pass a value to a parameter in a script

From Dev

How to get the value of parameter

From Dev

How to return value as a parameter in function

From Dev

How to return the parameter value in Racket?

From Dev

How to pass return type as parameter (no reflection?)

From Dev

How to get id from model which return array and pass to the controller function as parameter

From Dev

Get the return value of a parameter of async function

From Dev

How to open new form, pass parameter and return parameter back

From Dev

Pass function return value to reference parameter cause compilation error in GCC

From Dev

How to pass same parameter with different value

From Dev

How to pass parameter value when request to server

From Dev

Javascript : how to pass value into function parameter?

From Dev

MSBuild how to pass a parameter to set a property value?

From Dev

how to pass value to where parameter by variable?

From Dev

How to pass value to pointer parameter C++

From Dev

Javascript : how to pass value into function parameter?

From Dev

how to pass multiple integer parameter value in SSRS

From Dev

How to get a href parameter value

Related Related

  1. 1

    How to pass a parameter to a thread and get a return value?

  2. 2

    How to get the return value from a thread in python?

  3. 3

    how to get return value when a thread is done

  4. 4

    how to get value of parameter and pass it to a stored procedure?

  5. 5

    How to pass a parameter in an expectation to the return value in Rhino Mocks?

  6. 6

    how to pass askopenfilename() return value as a parameter to the builtin python function open

  7. 7

    How to get "pass parameter"

  8. 8

    Pass parameter to a method and read the return value?

  9. 9

    Pass parameter to a method and read the return value?

  10. 10

    How to get return value of a function using context as parameter?

  11. 11

    How to pass DateTime value parameter?

  12. 12

    How to pass a value to a parameter in a script

  13. 13

    How to get the value of parameter

  14. 14

    How to return value as a parameter in function

  15. 15

    How to return the parameter value in Racket?

  16. 16

    How to pass return type as parameter (no reflection?)

  17. 17

    How to get id from model which return array and pass to the controller function as parameter

  18. 18

    Get the return value of a parameter of async function

  19. 19

    How to open new form, pass parameter and return parameter back

  20. 20

    Pass function return value to reference parameter cause compilation error in GCC

  21. 21

    How to pass same parameter with different value

  22. 22

    How to pass parameter value when request to server

  23. 23

    Javascript : how to pass value into function parameter?

  24. 24

    MSBuild how to pass a parameter to set a property value?

  25. 25

    how to pass value to where parameter by variable?

  26. 26

    How to pass value to pointer parameter C++

  27. 27

    Javascript : how to pass value into function parameter?

  28. 28

    how to pass multiple integer parameter value in SSRS

  29. 29

    How to get a href parameter value

HotTag

Archive