返回值的Java Runnable run()方法
根据Java文档,response方法method_()无法返回值。 但是,我确实想知道是否有任何解决方法。
其实我有一个方法,它调用:
public class Endpoint{
public method_(){
RunnableClass runcls = new RunnableClass();
runcls.run()
}
}
其中方法response为:
public class RunnableClass implements Runnable{
public jaxbResponse response;
public void run() {
int id;
id =inputProxy.input(chain);
response = outputProxy.input();
}
}
我想访问method_()中的response变量,这可能吗?
8个解决方案
56 votes
使用Callable而不是使用Runnable接口。
例:
public static void main(String args[]) throws Exception {
ExecutorService pool = Executors.newFixedThreadPool(3);
Set> set = new HashSet>();
for (String word: args) {
Callable callable = new WordLengthCallable(word);
Future future = pool.submit(callable);
set.add(future);
}
int sum = 0;
for (Future future : set) {
sum += future.get();
}
System.out.printf("The sum of lengths is %s%n", sum);
System.exit(sum);
}
在此示例中,您还将需要实现WordLengthCallable类,该类实现Callable接口。
Narendra Pathai answered 2020-08-11T18:57:00Z
14 votes
public void check() {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future result = executor.submit(new Callable() {
public Integer call() throws Exception {
return 10;
}
});
try {
int returnValue = result.get();
} catch (Exception exception) {
//handle exception
}
}
vishal_aim answered 2020-08-11T18:57:15Z
7 votes
看一下Callable类。 这通常是通过执行服务提交的
它可以返回一个将来的对象,该对象在线程完成时返回
RNJ answered 2020-08-11T18:57:40Z
4 votes
是的,有解决方法。 只需使用queue,然后将要返回的值放入其中即可。 并从另一个线程获取此值。
public class RunnableClass implements Runnable{
private final BlockingQueue queue;
public RunnableClass(BlockingQueue queue) {
this.queue = queue;
}
public void run() {
int id;
id =inputProxy.input(chain);
queue.put(outputProxy.input());
}
}
public class Endpoint{
public method_(){
BlockingQueue queue = new LinkedBlockingQueue<>();
RunnableClass runcls = new RunnableClass(queue);
runcls.run()
jaxbResponse response = queue.take(); // waits until takes value from queue
}
}
Eldar Agalarov answered 2020-08-11T18:58:00Z
3 votes
如果将字段添加到RunnableClass,则可以在run中进行设置,并在method_中进行读取。但是,Runnable的效果较差(Java关键字)interface,因为它没有告诉您有关(概念)接口的任何信息(API的唯一有用的内容) docs:“方法run的一般约定是,它可能会采取任何措施。”)。 使用一个更有意义的接口(可能会返回一些东西)会更好。
Tom Hawtin - tackline answered 2020-08-11T18:58:21Z
2 votes
一种方法是,我们必须使用List方法。
另一种方法是,您可以保留对象而不是返回值
例:
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);
}
}
因为对象作用域在同一作用域中,所以您可以将对象传递给线程并可以在主作用域中进行检索。 但是,最重要的是,我们必须使用join()方法。 因为主作用域应等待线程完成其任务。
对于多线程情况,可以使用List/Map来保存线程中的值。
ramakrishna answered 2020-08-11T18:58:59Z
1 votes
尝试以下
public abstract class ReturnRunnable implements Runnable {
public abstract T runForResult();
@Override
public void run() {
runForResult();
}
}
Martin answered 2020-08-11T18:59:19Z
0 votes
看一下可调用的界面,也许这适合您的需求。 您还可以尝试通过调用run()方法内部的设置方法来获取响应字段的值
public void run() {
int id;
id =inputProxy.input(chain);
response = outputProxy.input();
OuterClass.setResponseData(response);
}
htz answered 2020-08-11T18:59:39Z