java-使用多线程完成费时的GUI任务

1、使用Future表现一个费时的任务,可以极大地简化取消的实现。类似地,FutureTask中也有一个done钩子函数,可以方便任务完成后的通知,后台Callable完成后,会调用done,通过由done触发一个事件线程已经完成的任务。

2、构建BackgroundTask类,提供onCompletion钩子函数,同时还支持进度标识,compute方法调用setProgress来以数字形式指定进度,这会引起事件线程调用onProgress,更新用户接口,可视化显示进度。

3、代码:

abstract class BackgroundTask<V>implements Runnable,Future<V>{

   private finalFutureTask<V> computation=newComputation();

  

  private class Computation extendsFutureTask<V>{

      public Computation(){

          super(new Callable<V>)(){

                  public V call() throws Exception{

                     return BackgroundTask.this.compute();

                  }

          });

      }

     protected final void done(){

         GuiExecutor.instance().execute(newRunnable(){

                public void run(){

                     Vvalue=null;

                    Throwable thrown=null;

                     booleancancelled=false;

                    try{

                        value=get();//得到计算完成的结果

                    }

                     catch(ExecutionException e){

                        thrown=e.getCause();

                     }

                     catch(CancellationException e){

                        cancelled=true;

                     }

                     catch(InterruptedExceptionconsumed){   }

                    finaly{

                         cnCompletion(value,thrown,cancelled);

                    }

                };

         });

    }

  }

   protected void setProgress(final int current,final intmax){

        GuiExecutor.instance().execute(new Runnable(){

           public void run(){onProgress(current,max);}

        });

   }

   //在后台线程中调用

   protected abstract Vcompute() throws Exception;

   //在事件线程中调用

   protected voidonCompletion(V result,Throwable exeception,boolean cancelled){}

   protected voidonProgress(int current,int max){}

}

 

startButton.addActionListener(new ActionListener(){

    publicvoid actionPerformed(ActionEvent e){

         class CancelListener implements ActionListener{

                BackgroundTask<?>task;

               public void actionPerformed(ActionEvent event){

                        if(task!=mull) task.cancel(true);

               }

          }

           final CancelListener listener=newCancelListstener();

           listener.task=newBackgroundTask<Void>(){

             public void compute(){

                  while(moreWork()&&!isCancelled())

                         doSomeWork();

                   returnnull;

             }

             public void onCompletion(boolean cancelled,String s,Throwableexception){

                   cancelButton.removeActionListenr(listener);

                  label.setText("done");

             }

           };

           cancelButton.addActionListener(listener);

           backgroundExec.execute(listener.task);

    }

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值