The Event-Dispatching Thread

Swing采用单一事件分发线程来处理所有事件及绘制操作,确保每个事件处理完毕才执行下一个,并避免绘制过程中被事件打断。为防止死锁,Swing组件及其模型必须仅从事件分发线程创建、修改和查询。推荐使用invokeLater方法在事件分派线程中创建GUI,以避免多线程问题。
    Swing event-handling and painting code executes in a single thread, called the event-dispatching thread. This ensures that each event handler finishes executing before the next one executes and that painting isn't interrupted by events. To avoid the possibility of deadlock, you must take extreme care that Swing components and models are created, modified, and queried only from the event-dispatching thread.

Note: We used to say that you could create the GUI on the main thread as long as you didn't modify components that had already been realized. [PENDING: The following red text belongs in a footnote.] Realized means that the component has been painted onscreen, or is ready to be painted. The methods setVisible(true) and pack cause a window to be realized, which in turn causes the components it contains to be realized. While this worked for most applications, in certain situations it could cause problems. Out of all the demos in the Swing Tutorial, we encountered a problem only in ComponentEventDemo. In that case, sometimes when you launched the demo, it would not come up because it would deadlock when updating the text area if the text area had not yet been realized, while other times it would come up without incident.

To avoid the possibility of thread problems, we recommend that you use invokeLater to create the GUI on the event-dispatching thread for all new applications. If you have old programs that are working fine they are probably OK; however you might want to convert them when it's convenient to do so.



AnrManager: startAsyncDump: AnrDumpRecord{ Input dispatching timed out (Waiting to send non-key event because the touched window has not finished processing certain input events that were delivered to it over 500.0ms ago. Wait queue length: 6. Wait queue head age: 5511.6ms.) ProcessRecord{e154cc2 2374:com.waterflowing.light/u0a13} IsCompleted:false IsCancelled:false } 09-30 00:51:44.816 486 501 I AnrManager: appNotResponding-got this lock: ProcessRecord{e154cc2 2374:com.waterflowing.light/u0a13} Input dispatching timed out (Waiting to send non-key event because the touched window has not finished processing certain input events that were delivered to it over 500.0ms ago. Wait queue length: 6. Wait queue head age: 5511.6ms.) 09-30 00:51:44.816 486 501 I AnrManager: dumpAnrDebugInfo begin: AnrDumpRecord{ Input dispatching timed out (Waiting to send non-key event because the touched window has not finished processing certain input events that were delivered to it over 500.0ms ago. Wait queue length: 6. Wait queue head age: 5511.6ms.) ProcessRecord{e154cc2 2374:com.waterflowing.light/u0a13} IsCompleted:false IsCancelled:false }, isAsyncDump = false 09-30 00:51:44.817 486 501 I AnrManager: dumpAnrDebugInfoLocked: AnrDumpRecord{ Input dispatching timed out (Waiting to send non-key event because the touched window has not finished processing certain input events that were delivered to it over 500.0ms ago. Wait queue length: 6. Wait queue head age: 5511.6ms.) ProcessRecord{e154cc2 2374:com.waterflowing.light/u0a13} IsCompleted:false IsCancelled:false }, isAsyncDump = false 09-30 00:51:44.843 486 2867 I AnrManager: sys/kernel/debug/binder/timeout_log isn't exist 09-30 00:51:44.857 486 501 I AnrManager: dumpStackTraces begin! 分析日志
09-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值