What are these threads which are spwaned when a Java application begins its execution?

不打扰是莪最后的温柔 提交于 2019-12-29 07:49:41

问题


I have created a simple Java application which has a JFrame and few JButtons. When I tried to inspect the java application using JVMTI I found that though I did not create any explicit threads there were lot of them spawned.

I could find the following threads:

  • DestroyJavaVM
  • AWT-EventQueue-0
  • AWT-Shutdown
  • AWT-XAWT- Daemon Thread
  • Java2D Disposer- Daemon Thread
  • Thread-0- Daemon Thread [Created by the JVMTI Agent]
  • Signal Dispatcher- Daemon Thread
  • Finalize- Daemon Thread
  • Reference Handler- Daemon Thread

Most of them were in Runnable state. Can someone tell me the function of these threads?


回答1:


These threads are used by the underlying libraries to manage the widgets, display, event-loop, and other plumbing that is needed for your graphical application.

A GUI application usually has a lot of moving parts, and if you've noticed you don't have to explicitly write any code to manage these parts (e.g., updating the screen, or drawing a button, or handling a mouse movement). Is is this set of background threads that are responsible for managing these parts, and making it as easy as possible for you to focus on your application logic.

These threads are spawned by the libraries that you use (e.g., AWT, Swing, etc.) and usually clean themselves (and the resources that they manage) up upon termination.




回答2:


I can tell you one, and guess at three...

  • AWT-EventQueue-0

This is the GUI/Awt/Swing thread. Anything writing to the GUI must be executing on this thread! Sometimes if I think there is a point where another thread MIGHT be writing to the GUI, I'll go to the point where I think the contention might be happening and assert that the name of the current thread matches "AWT-EventQueue-0".

I really wish Sun had released a "Debug" version of the library that asserted correct threading usage in all the components--it would have saved a lot of headaches and saved Java from having a really bad REP for crashing GUIs when it's really people who don't understand about this thread.

These are probably all related to the threaded garbage collector:

  • Java2D Disposer- Daemon Thread
  • Finalize- Daemon Thread
  • Reference Handler- Daemon Thread


来源:https://stackoverflow.com/questions/2239186/what-are-these-threads-which-are-spwaned-when-a-java-application-begins-its-exec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!