Java:return 来自 Runnable 的结果
Java: return results from Runnable
假设以下简化示例。令 B 代表 class 处理一些光栅数据:
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() 方法可能会但可能不会创建输出评分器。由于计算成本,过程 运行s 在一个单独的线程中。
让 A 代表一个 class,其中的过程将是 运行。它还包含一些 post 等待线程完成的处理步骤:
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
}
}
但是,如何获得使用 B process() 方法创建的结果栅格 "inside" 运行()A中的方法?
避免模型,当输出图像与
一起表示 B 的数据成员时
b.getImage();
我阅读了一篇关于回调的 post
但是这里怎么实现呢?感谢您的帮助和一个简短的例子。
使用ExecutorService
, specifically it submit(Callable)
method which returns a Future
which get()
or isDone()
方法可以调用检索结果:
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
,您可以在其中对可能 (submit(Callable)
) 或可能 (execute(Runnable)
) return 结果的处理进行排队。您要使用的执行器类型取决于您需要的处理类型和订单。
您可以尝试这样做:
public class B{
private boolean c;
volatile boolean finished = false; // it can be shared among threads
BufferedImage output;
public B (boolean c_) { c = c_;}
public void process() {
Thread t = new Thread(new Runnable(){
@Override
public void run() {
if (c) output = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
finished = true;
}
});
t.start();
}
}
public class A {
public void compute() {
B b = new B(true);
b.process();
while(!b.finished){System.out.println("Processing");}
// when finished check if output is not null
// and then do some stuff
if(b.output!=null){System.out.println(b.output);}
}
}
假设以下简化示例。令 B 代表 class 处理一些光栅数据:
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() 方法可能会但可能不会创建输出评分器。由于计算成本,过程 运行s 在一个单独的线程中。
让 A 代表一个 class,其中的过程将是 运行。它还包含一些 post 等待线程完成的处理步骤:
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
}
}
但是,如何获得使用 B process() 方法创建的结果栅格 "inside" 运行()A中的方法?
避免模型,当输出图像与
一起表示 B 的数据成员时b.getImage();
我阅读了一篇关于回调的 post
但是这里怎么实现呢?感谢您的帮助和一个简短的例子。
使用ExecutorService
, specifically it submit(Callable)
method which returns a Future
which get()
or isDone()
方法可以调用检索结果:
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
,您可以在其中对可能 (submit(Callable)
) 或可能 (execute(Runnable)
) return 结果的处理进行排队。您要使用的执行器类型取决于您需要的处理类型和订单。
您可以尝试这样做:
public class B{
private boolean c;
volatile boolean finished = false; // it can be shared among threads
BufferedImage output;
public B (boolean c_) { c = c_;}
public void process() {
Thread t = new Thread(new Runnable(){
@Override
public void run() {
if (c) output = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
finished = true;
}
});
t.start();
}
}
public class A {
public void compute() {
B b = new B(true);
b.process();
while(!b.finished){System.out.println("Processing");}
// when finished check if output is not null
// and then do some stuff
if(b.output!=null){System.out.println(b.output);}
}
}