blob: 5405d806f0de6a41cd68f3da279df245777f6d81 [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= DevFaqBackgroundThread
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqBackgroundThread
:description: Apache NetBeans wiki DevFaqBackgroundThread
:toc: left
:toc-title:
:syntax: true
=== What is a background thread and why do I need one?
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 link:DevFaqThreading.asciidoc[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>
[source,xml]
----
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()`.
=== Apache Migration Information
The content in this page was kindly donated by Oracle Corp. to the
Apache Software Foundation.
This page was exported from link:http://wiki.netbeans.org/DevFaqBackgroundThread[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-02-07, and needs to be reviewed.