先引用一段话,来自<Filthy Rich Clients>
Whenever you run a Swing application,
three threads are automatically created.
The first one is the main thread,
which runs your application’s main method.
A second thread, called the toolkit thread,
is in charge of capturing the system events,
like keyboard key presses or mouse movements.
Although this thread is vital,
it is only part of AWT implementation nand never runs application code.
Capture events are sent over to a third thread, the EDT.
-----------------------------------------------------------------------------
大意如下:
但你运行一个 Swing 的程序时,会自动创建三个线程。
第一个就是主线程,负责执行你的 main 方法
第二个叫 toolkit 线程,负责捕捉系统事件,比如键盘、鼠标移动等
虽然这个 toolkit 非常重要,但是你不会有任何代码在这个线程上执行
Whenever you run a Swing application,
three threads are automatically created.
The first one is the main thread,
which runs your application’s main method.
A second thread, called the toolkit thread,
is in charge of capturing the system events,
like keyboard key presses or mouse movements.
Although this thread is vital,
it is only part of AWT implementation nand never runs application code.
Capture events are sent over to a third thread, the EDT.
-----------------------------------------------------------------------------
大意如下:
但你运行一个 Swing 的程序时,会自动创建三个线程。
第一个就是主线程,负责执行你的 main 方法
第二个叫 toolkit 线程,负责捕捉系统事件,比如键盘、鼠标移动等
虽然这个 toolkit 非常重要,但是你不会有任何代码在这个线程上执行
这个线程的作用是,把这些事件传递给第三个线程:EDT
那 EDT 是什么呢?
EDT 叫做事件指派线程(Event Dispatcher Thread)
这个线程十分重要,他负责接收来自 toolkit 线程的事件,
然后派发给对应的控件的监听器,然后对应控件的监听器中对应方法的代码就会执行了
注意,Swing 有一个单线程规范,即:
所有的界面相关的更新,都应该在 EDT 上执行
而耗时的后台运行,不应该在 EDT 上执行