使用Executor实现SwingUtilities

public class SwingUtilities{
    
    
  private static final ExecutorService exec = Executors.newSingleThreadExecutor(new SwingThreadFactory());
  private static volatile Thread swingThread;

  private static class SwingThreadFactory implements ThreadFactory{
    
    
    swingThread = new Thread(r);
    return swingThread;
  }

  public static boolean isEventDiapatchThread(){
    
    
    return Thread.currentThread() == swingThread;
  }

  public static void invokeLater(Runnable task){
    
    
    exec.execute(task);
  }

  public static void invokeAndWait(Runnable task)
  throws InterruptedException, InvocationTargetException{
    
    
    Future f = exec.submit(task);
    try{
    
    
      f.get();
    }catch(ExecutionException e){
    
    
      throw new InvocationTargetException(e);
    }
  }

}

猜你喜欢

转载自blog.csdn.net/weixin_37632716/article/details/118914420