java Runnable run()方法返回一个值

问题描述 投票:39回答:8

根据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变量这可能吗?

java runnable
8个回答
54
投票

使用response而不是使用method_()接口。

例:

Callable<V>

在此示例中,您还需要实现WordLengthCallable类,该类实现Callable接口。


14
投票
Runnable

7
投票

看看qazxsw poi课程。这通常是通过qazxsw poi提交的

它可以返回线程完成时返回的future对象


3
投票

如果你向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的一般契约是它可能采取任何行动。 “)。使用更有意义的界面(可能会返回一些东西)要好得多。


2
投票

是的,有解决方法。只需使用队列并输入您想要返回的值。并从另一个线程中获取此值。

method_

1
投票

一种方法是,我们必须使用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 } } 来保存线程中的值。


0
投票

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

0
投票

请尝试以下方法

Map
© www.soinside.com 2019 - 2024. All rights reserved.