Java:从Runnable返回结果

编程入门 行业动态 更新时间:2024-10-26 18:17:00
本文介绍了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 Apute() 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返回结果

本文发布于:2023-11-23 18:21:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622433.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java   Runnable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!