GUI代码的线程同步
Swing组件的事件处理和绘画代码,都是被一个线程依次调用的,
这样保证事件处理的同步执行。
这个线程就是 Event-Dispatching Thread。
为了避免可能的死锁,确保Swing组件和model的创建、修改和查找
只在Event-dispatching Thread中调用。
SwingUtilities的invokeLater(Runnable),invokeAndWait(Runnable)
都是将GUI代码加入Event-dispatching Thread的方法。
invokeLater()立即返回,而invokeAndWait()阻塞,直到Event-dispatching Thread
执行完指定的GUI代码。
invokeAndWait()方法也有导致deadlock的可能,尽量使用invokeLater()。
About Realized
Realized means that the component has been painted on screen, or is ready to be painted.
setVisible(true) and pack() cause a window to be realized, which in turn causes the components it contains to be realized.
同步会导致等待,当一个事件处理时间太长,后面的事件处理就得不到响应,给人响应不灵敏的感觉。
SwingWorker或者Timer类可以解决缓解这个问题,
其中SwingWorker是java-tutorials定义的类&#x