Java: return results from Runnable

南楼画角 提交于 2019-12-08 20:05:31

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)

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.

You can try doing something like this:

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);}
   }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!