-
Notifications
You must be signed in to change notification settings - Fork 1
DevFaqBackgroundThread
As with most user interface (UI) toolkits, Swing is single threaded. That means there is one and only one thread that should create or alter the state of UI components, and that is the AWT Event Dispatch Thread (also known as the EDT or the "event thread"). It processes things like key and mouse events and calls components to respond to them.
This also means that code that responds to a key or mouse event, or some call triggered by one, should run very quickly, because the user can be typing or clicking, but the entire application is blocked from responding to more events until your code exits. So sometimes you will want to move expensive or slow work onto a background thread.
A background thread is any thread that is not the event thread.
If you are running on some background thread, but need to modify some Swing component, a simple way to do this is <pre>
EventQueue.invokeLater(new Runnable() {
public void run() {
//this code can work with Swing
}
});</pre>
Note that the caveat about Swing includes creating components - it is probably not safe to even construct Swing components on a background thread, because of synchronization on Component.getTreeLock()
.
The content in this page was kindly donated by Oracle Corp. to the Apache Software Foundation.
This page was exported from http://wiki.netbeans.org/DevFaqBackgroundThread , that was last modified by NetBeans user Braiam on 2010-03-03T21:56:44Z.
NOTE: This document was automatically converted to the AsciiDoc format on 2018-01-26, and needs to be reviewed.
Apache NetBeans is an effort undergoing incubation at The Apache Software Foundation (ASF).
Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, communications, and decision making process have stabilized in a manner consistent with other successful ASF projects.
While incubation status is not necessarily a reflection of the completeness or stability of the code, it does indicate that the project has yet to be fully endorsed by the ASF.
This wiki is an experiment pending Apache NetBeans Community approval.