blob: 8ddb9a1cbd72899b97186076bcf7eee8ac4c113c [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
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.
--><mediawiki xmlns="http://www.mediawiki.org/xml/export-0.3/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="0.3" xml:lang="en" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.3/ http://www.mediawiki.org/xml/export-0.3.xsd">
<siteinfo>
<sitename>NetBeans Wiki</sitename>
<base>http://wiki.netbeans.org/Main_Page</base>
<generator>MediaWiki 1.15.1</generator>
<case>first-letter</case>
<namespaces>
<namespace key="-2">Media</namespace>
<namespace key="-1">Special</namespace>
<namespace key="0"/>
<namespace key="1">Talk</namespace>
<namespace key="2">User</namespace>
<namespace key="3">User talk</namespace>
<namespace key="4">NetBeans Wiki</namespace>
<namespace key="5">NetBeans Wiki talk</namespace>
<namespace key="6">File</namespace>
<namespace key="7">File talk</namespace>
<namespace key="8">MediaWiki</namespace>
<namespace key="9">MediaWiki talk</namespace>
<namespace key="10">Template</namespace>
<namespace key="11">Template talk</namespace>
<namespace key="12">Help</namespace>
<namespace key="13">Help talk</namespace>
<namespace key="14">Category</namespace>
<namespace key="15">Category talk</namespace>
</namespaces>
</siteinfo>
<page>
<title>DevFaqBackgroundThread</title>
<id>7214</id>
<revision>
<id>34857</id>
<timestamp>2010-03-03T21:56:44Z</timestamp>
<contributor>
<username>Braiam</username>
<id>3851</id>
</contributor>
<minor/>
<comment>orthography (provably-&gt;probably)</comment>
<text xml:space="preserve">__NOTOC__
===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 [[DevFaqThreading| 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 &lt;pre&gt;
EventQueue.invokeLater(new Runnable() {
public void run() {
//this code can work with Swing
}
});&lt;/pre&gt;
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 &lt;tt&gt;Component.getTreeLock()&lt;/tt&gt;.</text>
</revision>
</page>
</mediawiki>