根据java doc,Runnable
方法void run()
无法返回值。我不知道是否有任何解决方法。
其实我有一个调用的方法:
public class Endpoint{
public method_(){
RunnableClass runcls = new RunnableClass();
runcls.run()
}
}
方法run()
是:
public class RunnableClass implements Runnable{
public jaxbResponse response;
public void run() {
int id;
id =inputProxy.input(chain);
response = outputProxy.input();
}
}
我想在qazxsw poi中访问qazxsw poi变量这可能吗?
使用response
而不是使用method_()
接口。
例:
Callable<V>
在此示例中,您还需要实现WordLengthCallable类,该类实现Callable接口。
Runnable
看看qazxsw poi课程。这通常是通过qazxsw poi提交的
它可以返回线程完成时返回的future对象
如果你向public static void main(String args[]) throws Exception {
ExecutorService pool = Executors.newFixedThreadPool(3);
Set<Future<Integer>> set = new HashSet<Future<Integer>>();
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);
}
添加一个字段,你可以在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
}
}
中设置它并在Callable中读取它。但是,executor service是一个穷人(Java关键字)RunnableClass
因为它没有告诉你任何关于(概念)接口(只有API文档的有用行:“方法run
的一般契约是它可能采取任何行动。 “)。使用更有意义的界面(可能会返回一些东西)要好得多。
是的,有解决方法。只需使用队列并输入您想要返回的值。并从另一个线程中获取此值。
method_
一种方法是,我们必须使用Runnable
方法。
另一种方法是,您可以保留对象,而不是返回值
例:
interface
因为对象范围在同一范围内,所以您可以将对象传递给线程并可以在主范围内检索。但是,最重要的是,我们必须使用join()方法。因为主要范围应该等待线程完成其任务。
对于多线程情况,您可以使用run
/ 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
}
}
来保存线程中的值。
看看Future - Callable
,也许这可以满足您的需求。您还可以通过调用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);
}
}
方法中的setter方法来尝试获取响应字段的值
List
请尝试以下方法
Map