void () { UIUtil.invokeAndWaitIfNeeded(() -> { SwingUtilities.invokeLater(ENTER_MODAL); flushSwingQueue(); synchronized (this) { blockSwingThread(); SwingUtilities.invokeLater(new MyRunnable("1")); ApplicationManager.getApplication().invokeLater(new MyRunnable("3"), ModalityState.nonModal()); SwingUtilities.invokeLater(LEAVE_MODAL); SwingUtilities.invokeLater(ENTER_MODAL); SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(new MyRunnable("2")); ApplicationManager.getApplication().invokeLater(new MyRunnable("5"), ModalityState.nonModal()); }); ApplicationManager.getApplication().invokeLater(new MyRunnable("4"), ModalityState.nonModal()); } flushSwingQueue(); SwingUtilities.invokeLater(LEAVE_MODAL); flushSwingQueue(); checkOrder(5); }); }