69

The run method of Runnable has return type void and cannot return a value. I wonder however if there is any workaround of this.

I have a method like this:

public class Endpoint {
    public method() {
       Runnable runcls = new RunnableClass();
       runcls.run()
    }
}

The method run is like this:

public class RunnableClass implements Runnable {
    
    public JaxbResponse response;

    public void run() {
        int id = inputProxy.input(chain);
        response = outputProxy.input();
    }
}

I want to have access to response variable in method. Is this possible?

Lii
  • 11,553
  • 8
  • 64
  • 88
Grzzzzzzzzzzzzz
  • 1,301
  • 4
  • 20
  • 33

8 Answers8

88

Use Callable<V> instead of using Runnable interface.

Example:

public static void main(String args[]) throws Exception {
    ExecutorService pool = Executors.newFixedThreadPool(3);
    Set<Future<Integer>> set = new HashSet<>();

    for (String word : args) {
      Callable<Integer> callable = new WordLengthCallable(word);
      Future<Integer> future = pool.submit(callable);
      set.add(future);
    }

    int sum = 0;
    for (Future<Integer> future : set) {
      sum += future.get();
    }

    System.out.printf("The sum of lengths is %s%n", sum);
    System.exit(sum);
}

In this example, you will also need to implement the class WordLengthCallable, which implements the Callable interface.

Lii
  • 11,553
  • 8
  • 64
  • 88
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
  • `Exception in thread "main" java.lang.Error: Unresolved compilation problem: WordLengthCallable cannot be resolved to a type`. I'm not smart enough to fix it but it doesn't work for me. – localhost Jul 07 '13 at 12:57
  • Also I think `new HashSet>()` should be `new HashSet>()` – localhost Jul 07 '13 at 13:01
  • 2
    @localhost it is just a concrete implementation of Callable Interface. For more on Callable: https://blogs.oracle.com/CoreJavaTechTips/entry/get_netbeans_6 – Narendra Pathai Jul 08 '13 at 10:43
  • If your delegate doesn't throw any exceptions, it's better to use `Supplier`. – RubberDuck May 24 '17 at 12:09
  • Thanks, Narendra Pathai. However, I have a problem. The .join is blocking my UI, negating all benefits of the separate thread. How could I prevent this? – Felipe La Rotta Jun 02 '22 at 18:40
18
public void check() {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<Integer> result = executor.submit(new Callable<Integer>() {
        public Integer call() throws Exception {
            return 10;
        }
    });

    try {
        int returnValue = result.get();
    } catch (Exception exception) {
       //handle exception
    }
}
Marcel
  • 1,509
  • 1
  • 17
  • 39
vishal_aim
  • 7,636
  • 1
  • 20
  • 23
8

Have a look at the Callable class. This is usually submited via an executor service

It can return a future object which is returned when the thread completes

RNJ
  • 15,272
  • 18
  • 86
  • 131
7

Yes, there are workaround. Just use queue and put into it value which you want to return. And take this value from another thread.

public class RunnableClass implements Runnable{

        private final BlockingQueue<jaxbResponse> queue;


        public RunnableClass(BlockingQueue<jaxbResponse> queue) {
            this.queue = queue;
        }

        public void run() {
            int id;
            id =inputProxy.input(chain);
            queue.put(outputProxy.input());
        }
    }


    public class Endpoint{
        public method_(){
            BlockingQueue<jaxbResponse> queue = new LinkedBlockingQueue<>();

            RunnableClass runcls = new RunnableClass(queue);
            runcls.run()

            jaxbResponse response = queue.take(); // waits until takes value from queue
        }
    }
Eldar Agalarov
  • 4,849
  • 4
  • 30
  • 38
3

If you add a field to RunnableClass you can set it in run and read it in method_. However, Runnable is a poor (the Java keyword) interface as it tells you nothing about the (the concept) interface (only useful line of the API docs: "The general contract of the method run is that it may take any action whatsoever."). Much better to use a more meaningful interface (that may return something).

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
3

One way is, we have to use Future - Callable approach.

Another way is, Instead of returning value, you can hold in object

Example:

class MainThread {
    public void startMyThread() {
        Object requiredObject = new Object(); //Map/List/OwnClass
        Thread myThread = new Thread(new RunnableObject(requiredObject)).start();
        myThread.join();

        System.out.println(requiredObject.getRequiredValue());    
    }
}



class RunnableObject implements Runnable {
    private Object requiredObject;

    public RunnableObject(Object requiredObject) {
        this.requiredObject = requiredObject;
    }

    public void run() {
        requiredObject.setRequiredValue(xxxxx);
    }
}

Because object scope is in the same scope so that you can pass object to thread and can retrieve in the main scope. But, most important thing is, we have to use join() method. Because main scope should be waiting for thread completion of its task.

For multiple thread case, you can use List/Map to hold the values from threads.

ramakrishna
  • 827
  • 1
  • 6
  • 5
  • 1
    `requiredObject.setRequiredValue()` is not a method in `Object`. A more sophisticated approach would be needed to do what are trying to do: `requiredObject = result;` & `Object getResult() { return requiredObject; }`. Also starting a new Thread has nothing to do with the original question, nor is it necessary for the answer. BTW: Using `Callable` as answered by Narendra Pathai is definitely a better way. – Frederic Leitenberger Jul 19 '16 at 08:03
  • here, Object is **Map/List/OwnClass** which was already mentioned in comment. And also I clearly mention using the member names. ** required object / required value.** If it is custom class, we can write setter and getter methods. – ramakrishna Dec 26 '17 at 12:45
1

Try the following

public abstract class ReturnRunnable<T> implements Runnable {

    public abstract T runForResult();

    @Override
    public void run() {
        runForResult();
    }
}
Sankumarsingh
  • 9,889
  • 11
  • 50
  • 74
Martin
  • 652
  • 9
  • 27
0

Take a look at the callable interface, perhaps this suites your needs. You can also try to get the value of the response field by calling a setter-method inside of your run() method

public void run() {
    int id;
    id =inputProxy.input(chain);
    response = outputProxy.input();
    OuterClass.setResponseData(response);

}
htz
  • 1,037
  • 1
  • 13
  • 37