//为事件分派线程安排一个作业
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
}
});
下面是摘自javadoc里面的内容,略微翻译了一下
public static void invokeLater(Runnable doRun)
Causes doRun.run() to be executed asynchronously on the AWT event dispatching thread. This will happen after all pending AWT events have been processed. This method should be used when an application thread needs to update the GUI. In the following example the invokeLater call queues the Runnable object doHelloWorld on the event dispatching thread and then prints a message.
翻译一下意思大概就是:调用doRun.run()实在AWT的事件派遣线程中异步执行的,这个函数直到所有的AWT事件被执行完了才会被执行。当一个应用程序需要更新他的界面时,你应该调用这个函数。看下面的例子
Runnable doHelloWorld = new Runnable() {
public void run() {
System.out.println("Hello World on " + Thread.currentThread());
}
};
SwingUtilities.invokeLater(doHelloWorld);
System.out.println("This might well be displayed before the other message.");
输出结果为:
This might well be displayed before the other message.
Hello World on Thread[AWT-EventQueue-0,6,main]
If invokeLater is called from the event dispatching thread -- for example, from a JButton's ActionListener -- the doRun.run() will still be deferred(推迟) until all pending events have been processed. Note that if the doRun.run() throws an uncaught exception the event dispatching thread will unwind (not the current thread)(注意,如果doRun.run()抛出未捕获的异常事件调度线程会撤销).
Additional documentation and examples for this method can be found in How to Use Threads, in The Java Tutorial.
As of 1.3 this method is just a cover for java.awt.EventQueue.invokeLater().
Unlike the rest of Swing, this method can be invoked from any thread.