简体   繁体   English

我需要从线程返回一些东西

[英]I need to return something from a thread

I need to create more threads so one of my friends gave me some code for an "ThreadBuilder" but i can't return from a thread.我需要创建更多线程,所以我的一个朋友给了我一些“ThreadBuilder”的代码,但我无法从线程返回。 I need now some help to do this.我现在需要一些帮助来做到这一点。 Some piece of code would also be great.一些代码也会很棒。

I have tried tried it with an one element final array and with Atomic Reference but it didnt work as expected.我试过用一个元素的最终数组和原子引用尝试它,但它没有按预期工作。

package net.test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class ThreadBuilder {

    private static String API_QUEUE = "API QUEUE";
    private static int threads = 0;

    private static final ExecutorService queue;

    static {
        ThreadFactory threadBuilder = r -> {
            Thread thread = new Thread(r, ThreadBuilder.API_QUEUE + ++ThreadBuilder.threads);
            thread.setDaemon(true);
            return thread;
        };
        queue = Executors.newCachedThreadPool(threadBuilder);
    }

    public static void run(Runnable runnable) {
        queue.execute(runnable);
    }

    public static ExecutorService getQueue() {
        return queue;
    }

}

Use Callable , and return the result of the queue.submit :使用Callable ,并返回queue.submit的结果:

public static <T> Future<T> run(Callable<T> callable) {
    return queue.submit(callable);
}

When the Future has completed, you can get the result using Future.get .Future完成后,您可以使用Future.get获得结果。


I would also point out that your ThreadBuilder class is worse than just using an ExecutorService directly.我还要指出,您的ThreadBuilder class 比直接使用ExecutorService更糟糕。 For example, you can't shut your executor down, await termination etc. Plus, you are introducing a static binding to that class, which makes things harder to isolate, test etc.例如,您不能关闭执行程序、等待终止等。另外,您正在引入一个 static 绑定到该 class,这使得事情更难隔离、测试等。

It would be better simply to have a static factory method to create the ExecutorService , and then interact with the ExecutorService directly.最好有一个 static 工厂方法来创建ExecutorService ,然后直接与ExecutorService交互。

Another solution that I can think of is creating a class to hold the result of the computation or whatever you are performing on a different thread, then pass an instance of that class as a parameter to the Runnable itself (kind of like a has-a relation).我能想到的另一个解决方案是创建一个 class 来保存计算结果或您在不同线程上执行的任何操作,然后将该 class 的实例作为参数传递给Runnable本身(有点像 has-a关系)。 Then inside the run() method of the Runnable , when the result is obtained, then pass the result on to the result storing object.然后在Runnablerun()方法中,当得到结果时,将结果传递给存储 object 的结果。

Basically, it's the Observer Design Pattern .基本上,它是观察者设计模式

This is a actually a bad solution as you have to keep checking whether the result storing object has been updated or not and this is fundamentally bad for design.这实际上是一个糟糕的解决方案,因为您必须不断检查存储 object 的结果是否已更新,这从根本上不利于设计。 The only reason why I am suggesting this answer is that you do not know about Callable and I suggest you learn and then refer to @AndyTurner's answer我建议这个答案的唯一原因是您不了解Callable ,我建议您学习然后参考@AndyTurner 的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM