Java: return results from Runnable

二次信任 提交于 2019-12-10 11:40:00

问题


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();}
}

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

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
    }
}

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

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

b.getImage();

I read a post about callbacks

Return value from Runnable

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


回答1:


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.




回答2:


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);}
   }
}


来源:https://stackoverflow.com/questions/45000623/java-return-results-from-runnable

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