代码之家  ›  专栏  ›  技术社区  ›  justik

Java:从Runnable返回结果

  •  1
  • justik  · 技术社区  · 7 年前

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

    这个

    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“内部”方法

    当输出图像表示B的一个数据成员以及

    b.getImage();
    

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

    Return value from Runnable

    但是如何在这里实施呢?谢谢你的帮助和一个简短的例子。

    2 回复  |  直到 7 年前
        1
  •  6
  •   Matthieu    7 年前

    使用 ExecutorService ,具体来说 submit(Callable) 返回一个 Future get() 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)
    

    你可以使用不同口味的 您可以在其中对处理进行排队( 提交(可调用) execute(Runnable) )返回结果。您想要使用的执行器类型取决于您需要的处理类型和订单。

        2
  •  1
  •   Yahya    7 年前

    您可以尝试这样做:

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