EventQueue.java 文件源码

java
阅读 32 收藏 0 点赞 0 评论 0

项目:VarJ 作者:
/**
    * Causes <code>runnable</code> to have its <code>run</code>
    * method called in the dispatch thread of
    * {@link Toolkit#getSystemEventQueue the system EventQueue}.
    * This will happen after all pending events are processed.
    * The call blocks until this has happened.  This method
    * will throw an Error if called from the event dispatcher thread.
    *
    * @param runnable  the <code>Runnable</code> whose <code>run</code>
    *                  method should be executed
    *                  synchronously on the <code>EventQueue</code>
    * @exception       InterruptedException  if any thread has
    *                  interrupted this thread
    * @exception       InvocationTargetException  if an throwable is thrown
    *                  when running <code>runnable</code>
    * @see             #invokeLater
    * @since           1.2
    */
   public static void invokeAndWait(Runnable runnable)
            throws InterruptedException, InvocationTargetException {

       if (EventQueue.isDispatchThread()) {
           throw new Error("Cannot call invokeAndWait from the event dispatcher thread");
       }

class AWTInvocationLock {}
       Object lock = new AWTInvocationLock();

       InvocationEvent event = 
           new InvocationEvent(Toolkit.getDefaultToolkit(), runnable, lock,
            true);

       synchronized (lock) {
           Toolkit.getEventQueue().postEvent(event);
           lock.wait();
       }

       Throwable eventThrowable = event.getThrowable();
       if (eventThrowable != null) {
           throw new InvocationTargetException(eventThrowable);
       }
   }
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号