Returning a value from Runnable

JavaRunnable

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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
    }
}

Solution 3 - Java

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

Solution 4 - Java

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
        }
    }

Solution 5 - Java

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).

Solution 6 - Java

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.

Solution 7 - Java

Try the following

public abstract class ReturnRunnable<T> implements Runnable {

    public abstract T runForResult();

    @Override
    public void run() {
        runForResult();
    }
}

Solution 8 - Java

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);

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGrzzzzzzzzzzzzzView Question on Stackoverflow
Solution 1 - JavaNarendra PathaiView Answer on Stackoverflow
Solution 2 - Javavishal_aimView Answer on Stackoverflow
Solution 3 - JavaRNJView Answer on Stackoverflow
Solution 4 - JavaEldar AgalarovView Answer on Stackoverflow
Solution 5 - JavaTom Hawtin - tacklineView Answer on Stackoverflow
Solution 6 - JavaramakrishnaView Answer on Stackoverflow
Solution 7 - JavaMartinView Answer on Stackoverflow
Solution 8 - JavahtzView Answer on Stackoverflow