【发布时间】:2016-07-15 16:02:41
【问题描述】:
我有这样的服务,有很多方法:
public class MyService {
public void method1(String arg0, int arg1) {...}
public MyObject method2(Object arg0, String arg1, int arg2) {...}
//...
}
到目前为止,MyService 的方法是从各种线程(Eclipse RCP 上下文)调用的。 我需要从同一个唯一线程调用该服务的所有方法。
我见过 SingleThreadExecutor,但我是否必须将每个方法定义为 Callable 并为每个方法创建一个类?另外,我不知道如何将各种参数传递给我的方法? 当然,对这些方法的所有调用都应该得到返回值(如果有的话)和现在的异常。
是否有一个简单的解决方案可以像这样转换所有调用:
myService.method1(arg0, arg1);
到这样的事情:
executor.execute(myService.method1(arg0, arg1))
我会很感激一些例子。
【问题讨论】:
标签: java multithreading methods parameters callable