Java:从Runnable返回结果 [英] Java: return results from Runnable

查看:1087
本文介绍了Java:从Runnable返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设以下简化示例。设B表示处理某些栅格数据的类:

Suppose the following simplified example. Let B represent a class processing some raster data:

import java.awt.image.BufferedImage;

public class B implements Runnable{
    private boolean c;
    private Runnable f;

    public B (boolean c_, Runnable f_) { c = c_; f = f_;}

    public BufferedImage process() {
            //Some operations
            BufferedImage output = null;
            if (c) output = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
            return output;
    }

    public void run() { process();}
}

process()方法可能但不能创建输出评估者。由于计算成本,该过程在一个单独的线程中运行。

The process() method may but may not create an output rater. Due to the computational cost, the procedure runs in a separate thread.

设A表示将在其中运行过程的类。它还包含一些等待线程完成的后期处理步骤:

Let A represent a class inside which the procedure will be run. It also contains some post process steps waiting until the thread is finished:

import java.awt.image.BufferedImage;

public class A {
    public A(){}
    public void compute() {
            boolean c = true;
            B b = new B( c, new Runnable() {
                    public void run() {
                            //Post process, after the thread has been finished
                            BufferedImage img = ??? //Get resulting raster, how?
                            if (img != null) {
                                    //Perform some steps
                            }
                    }
            });

            Thread t = new Thread(b);
            t.start ();  //Run  procedure
    }
}

但是,如何得到结果光栅使用A的 process()方法在A中的 run()方法中创建?

However, how to get resulting raster created using the process() method of B "inside" the run() method in A?

当输出图像表示B的数据成员时,避免使用模型

Avoid the model, when the output image represents a data member of B together with

b.getImage();

我读了一篇关于回调的帖子

I read a post about callbacks

Runnable的返回值

但是如何在这里实现?感谢您的帮助和一个简短的例子。

but how to implement it here? Thanks for your help and a short example.

推荐答案

使用 ExecutorService ,特别是 提交(可调用) 方法,该方法返回 Future get() isDone()可以调用 方法来检索结果:

Use an ExecutorService, specifically it submit(Callable) method which returns a Future which get() or isDone() methods can be called to retrieve the result:

public class B implements Callable<BufferedImage> {
    private boolean c;

    public B (boolean c) { this.c = c; }

    public BufferedImage call() {
        //Some operations
        if (!c)
            return null;
        return new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
    }
}

// Somewhere, e.g. in your A.compute() method

ExecutorService exe = Executors.newFixedThreadPool(2); // Two threads in parallel
B b = new B(true);
Future<BufferedImage> res = exe.submit(b); // Will return immediately. Processing will run in a thread of 'exe' executor
// ... do things
System.out.println("Finished: "+res.isDone());
BufferedImage img = res.get(); // Will block until image is available (i.e. b.call() returns)

你可以使用不同的风格 ExecutorService ,您可以在其中对可能(提交(可调用))的处理进行排队,或者可以不对其进行排队( 执行(Runnable) )返回结果。您要使用的执行者类型取决于您需要的处理类型和订单。

You can use different flavors of ExecutorService in which you can queue processings which may (submit(Callable)) or may not (execute(Runnable)) return a result. The type of Executor you want to use depends on the type of processing and order you need.

这篇关于Java:从Runnable返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆