What is the event dispatching thread?

前端 未结 4 1542
感情败类
感情败类 2020-12-18 04:07

I know what \"thread\" means and if I understand the event dispatching thread (EDT) as \"just a thread\", it explains a lot but, apparently, it does not explain everything.

4条回答
  •  半阙折子戏
    2020-12-18 04:20

    The important thing to remember is that Swing classes are not thread-safe. This means that you always should call Swing methods from the same thread, or you risk getting weird or undefined behavior.

    So the solution: only call Swing methods from a single thread. This is the EDT thread - it's not special in any way other than that it is the thread designated to call swing methods from.

    Now you may ask why are Swing methods not thread safe? After several unsuccessful attempts, GUI toolkit designers discovered that it's inherently impossible to design a thread-safe GUI toolkit. Too often events are passed in opposite directions (input events from bottom to top, application events from top to bottom) which always leads to deadlocks. So that's just the way it is.

提交回复
热议问题