2016-11-03 7 views
0

次のコードでは、ScheduledExecutorにはタスクがあります。プロダクションではいくつかのタスクがありますが、現在はテストしています。とにかく、私は例外を処理できる必要があります。以下の私のコードはこれを行いますが、私のGUIは応答しなくなります。マークされた行が問題のように見えます。エラーが発生した場合、どのようにすべての例外を集めることができますか?例外ブロックの取得ScheduledExecutorService

SwingUtilities.invokeLater(new Runnable() { 
    @Override 
    public void run() { 
     CommandInterface ci = tc.getTask().getCommandInterface(); 
     ScheduledExecutorService scheduler = 
      taskManager.getComponentInterface().getThreadPool(); 
     ScheduledFuture<?> st = scheduler.schedule(
      new TimedRunnable(ci), new Date(
       ci.getScheduledDate().getTime() 
       - System.currentTimeMillis()).getTime(), TimeUnit.MILLISECONDS); 

       //This line causes blocking -> 
     SwingUtilities.invokeLater(new AlertRunnable(
      taskManager.getComponentInterface().getAlertList(), st)); 
    } 
    }); 
+0

あなた 'AlertRunnable'クラスの' run'方法で例外をキャッチ? – VGR

+0

正確です。私は同じことをした別の例を見つけました。私は解決策を投稿します。 –

答えて

0
   SwingUtilities.invokeLater(new Runnable() { 
       @Override 
       public void run() { 
        CommandInterface ci = tc.getTask().getCommandInterface(); 
        taskManagerInterface.getComponentInterface().getThreadPool().schedule(new TimedRunnable(ci, taskManagerInterface.getComponentInterface()), new Date(ci.getScheduledDate().getTime() - System.currentTimeMillis()).getTime(), TimeUnit.MILLISECONDS); 

       } 
      }); 

時限式実行可能にされた状態で:

/** 
* This class runs at a specific time. 
*/ 
public static class TimedRunnable implements Runnable, Serializable { 

    ExecutorService workerExecutor = Executors.newSingleThreadExecutor(); 
    CommandInterface commandInterface; 
    ComponentInterface componentInterface; 

    public TimedRunnable(CommandInterface commandInterface, ComponentInterface componentInterface) { 
     this.commandInterface = commandInterface; 
     this.componentInterface = componentInterface; 
    } 

    @Override 
    public void run() { 
     //submit the callable to the progressHelper 
     Future future = workerExecutor.submit(new Callable() { 
      @Override 
      public Object call() throws Exception { 
       try { 
        ReturnInterface returnInterface = (ReturnInterface) commandInterface.call(); 
        returnInterface.submitResult(); 
       } catch (Exception ex) { 
        throw new RuntimeException(ex); 
       } 
       return null; 
      } 
     }); 

     try { 
      Object get = future.get(); 
     } catch (InterruptedException | ExecutionException ex) { 
      Throwable cause = ex.getCause(); 
      Throwable cause1 = cause.getCause(); 

      if (cause1 instanceof CommandInterfaceException) { 
       System.out.println("[MyItemTree].scheduleTask Cause 1= COMMANDINTERFACE EXCEPTION"); 
       this.componentInterface.getAlertList().addAlert(((CommandInterfaceException) cause1).getResolverFormInterface()); 
      } 
     } 

    } 

} 
関連する問題