|
Articles Index | Threads Index
Swing components are not inherently thread safe, and as a
general rule, after Swing components have been made visible on
the screen, you can only safely modify their data from the event
thread. If you modify Swing component data from any thread
other than the event dispatching thread, you must take precautions to ensure
data integrity. An exception to this rule is the Sometimes, you cannot avoid modifying Swing component data from outside the event dispatching thread after it has been made visible. An example is spawning a thread to handle a long operation such as reading or writing a large file over the net. The advantage to spawning a new thread is that the user interface is made available to the user for other operations during the long file operation. The spawned thread is a new thread, and therefore, not on the event dispatching thread even if it is spawned by an event dispatching thread method.
This article explains how to use the
Example Application
You can view the full source code in the Editor class file.
See Running the Example to find out how to compile and run the example in both English and Japanese. Method Overview
In this example, the read and write operations are threaded
to return control to the user interface so the user can
elect to cancel either operation. The
The
The following subsections group and briefly explain the methods
involved in threading the read and write operations and
synchronizing data in the
Threaded OperationsThe following methods work together to return user interface control to the user during the read from file or write to file operation. They also ensure that text read from the file is set on the text area and text gotten from the text area and saved to the file are handled in a thread safe manner from the event dispatching thread.
Critical to ensuring that all Swing component modifications
are made from the event dispatching thread are the
Read from file operation:
Write to file operation:
Ensuring the Event ThreadAny modifications made to Swing components within the event-handling methods are made by the event dispatching thread and are perfectly thread safe. This program includes a method that checks whether an operation is on the event dispatching thread before allowing the operation to continue, as an extra safeguard to prevent a non-event dispatching thread from unsafely accessing a component or its data. Editor.ensureEventThread
The following methods call
Data Integrity
The
The
High-Level View of Application
The Java VM
creates and starts one event dispatching thread per application to call a Swing
component's event handling methods. The The diagram below charts the read operation showing which method are called by the event dispatching thread and which are not. You can apply what you learn here to the write operations; they are nearly identical.
When the user invokes the read operation, the event dispatching thread
calls the
While
The
Spawning the
The thread executing within the
After the text is read, it needs to be set on the text area, but
current execution is not on the event dispatching thread.
To put the set operation onto the event dispatching thread, the
Internationalization
This example is internationalized and localized for English
and Japanese. In an internationalized program, string values are
read from a properties file that contains translations for the language
in use in the form of key and value pairs. So, instead of creating
strings directly in your code, you create a For example, instead of creating the File menu like this,
JMenu filemenu = new JMenu("File");
you would create the File menu as shown below
where
JMenu filemenu = new JMenu(
messages.getString("FileMenu")).
An internationalized application can be adapted (localized) to as many cultures as needed. The locale information indicates the language, country, and variant of the language to use. For example, you might localize an application for the country Switzerland, the language German, and the Swiss German variant (the spoken dialect). Properties FilesProperties files follow a naming convention so the application can locate and load the correct file at runtime. This example is internationalized and localized for English and Japanese, and includes the properties files listed below. See Running the Example In Two Languages below for information on how these files are used by the application at runtime.
Processing Locale Information
In this example, the
A call to
Running the Example In Two LanguagesThe default properties file is a copy of either the English or Japanese file. That way, the correct locale is loaded when the application is invoked with either no command-line arguments, or command-line arguments that do not map to an available properties file. //compile javac Editor.java compile //run with no arguments java Editor Japanese VersionTo run the Japanese version of the application using the Japanese language properties file, type: //compile javac Editor.java //run with arguments java Editor ja JP ![]() English VersionTo run the English version using the English language properties file, type: //compile javac Editor.java //run with arguments java Editor en US ![]() See the Internationalization lesson in Essentials of the Java Programming Language: A Hands-On Guide for more information Conclusion and Further Reading
Working with Swing components requires care to ensure data integrity in your user interface. Swing components are not generally thread safe for performance reasons. However, if all your Swing component operations are done from the event dispatching thread, you have nothing to worry about.
In the event you have to access Swing components from
a thread other than the event dispatching thread, you must use
the
Always use the An excellent examples-based reference is Java Thread Programming: The Authoritative Solution by Paul Hyde. This is an accessible and complete treatment that inexperienced and experienced developers alike should find worthwhile. The Doing Two or More Tasks at Once lesson in The Java Tutorial provides excellent introductory coverage as well. Also see Concurrent Programming in Java by Doug Lea and visit his web site. See these other related articles:
AcknowledgmentsMany thanks to Naoto Sato who provided the Japanese unicode translations for the Japanese version of the application, and to Paul Hyde and Joseph Bowbeer who reviewed code and and answered questions during the development phase for this article.
Monica Pawlan, a staff writer for the Java Developer Connection (JDC), is author of Essentials of the Java Programming Language: A Hands-On Guide (Addison-Wesley, 2000), and co-author of Advanced Programming for the Java 2 Platform (Addison-Wesley, 2000). Have a question about programming? Use Java Online Support.
1 As used on this web site, the terms "Java virtual machine" or "JVM" mean a virtual machine for the Java platform. | |||||||||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||