Академический Документы
Профессиональный Документы
Культура Документы
Welcome to the Core Java Technologies Tech Tips for July 27,
2005. Here you'll get tips on using core Java technologies and
APIs, such as those in Java 2 Platform, Standard Edition (J2SE).
This issue covers:
* Swing "Urban Legends"
* From Runtime.exec() to ProcessBuilder
These tips were developed using Java 2 Platform Standard
Edition Development Kit 5.0 (JDK 5.0). You can download JDK 5.0
at http://java.sun.com/j2se/1.5.0/download.jsp.
This issue of the Core Java Technologies Tech Tips is written by
John Zukowski, president of JZ Ventures, Inc.
(http://www.jzventures.com).
You can view this issue of the Tech Tips on the Web at
http://java.sun.com/developer/JDCTechTips/2005/tt0727.html
See the Subscribe/Unsubscribe note at the end of this newsletter
to subscribe to Tech Tips that focus on technologies and products
in other Java platforms.
For more Java technology content, visit these sites:
java.sun.com - The latest Java platform releases, tutorials, and
newsletters.
java.net - A web forum for collaborating and building solutions
together.
java.com - Hot games, cool apps -- Experience the power of Java
technology.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - SWING "URBAN LEGENDS"
Developers who work with Swing components often hear about
certain ways of doing things that they assume are the right ways
to work with Swing. Like "urban legends" that purport to be
accounts of actual events but never really happened, some of
these Swing techniques are incorrect. In this tip, you'll learn
about a number of these Swing urban legends -- approaches that
will hinder the performance of your Swing applications. In some
cases, the performance reduction might only be nanoseconds, but
if you want the best performance profile, even cutting a handful
of nanoseconds adds up over time.
EventQueue.invokeLater(runner);
}
}
If you run Threads, you'll see that the main thread has
a priority of 5, and the event thread runs at a priority 6.
>> java Threads
Main Thread priority: 5
Event Thread priority: 6
The higher priority for the event thread is desirable. You want
your user interfaces to be responsive. But, you don't want to
extend that higher priority to non-event processing tasks. So be
sure to lower the priority of user-created threads initialized
from the event dispatch thread. This means:
Change:
Thread t = new Thread(longTask);
t.start();
To:
Thread t = new Thread(longTask);
t.setPriority(Thread.NORM_PRIORITY);
t.start();
Threads created with this new priority will not compete for
processing time with the event dispatch thread. If there is
something to run on the event dispatch thread, it will win -not the worker thread. You might consider creating
a WorkerThread class for just this purpose. That way you
won't have to keep calling setPriority() for all new threads
created from the event dispatch thread, or use a thread pool
through the following new classes in the
java.util.concurrent package:
o Executors.newCachedThreadPool()
For a thread pool with unbound size
o Executors.newFixedThreadPool(int size)
For a thread pool of fixed size > 1
o Executors.newSingleThreadExecutor()
For a thread pool of fixed size = 1
Executor was added to the standard libraries with JDK 5.0.
Use SwingUtilities For Running Tasks on the Event Dispatch Thread
This isn't really an urban legend, but rather an explanation of
the use of the EventQueue class in the first legend. Many people
are familiar with the SwingUtilities class for the use of
invokeLater() and invokeAndWait(). Where did this EventQueue
class come from?
The answer is that all these methods in SwingUtilities wrap
return savedValue;
}
}
}
For some more insights into Swing performance, see the
transcript of the SDN chat, Getting High Performance from Your
Desktop Client
(http://java.sun.com/developer/community/chat/JavaLive/2005/jl0215.html).
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - FROM RUNTIME.EXEC() TO PROCESSBUILDER
Before JDK 5.0, the only way to fork off a process and execute
it local to the user runtime was to use the exec() method of the
java.lang.Runtime class. JDK 5.0 adds a new way of executing
a command in a separate process, through a class called
ProcessBuilder. You can find ProcessBuilder in the java.lang
package (like Runtime and Process). This tip discusses and
compares both approaches.
If you're familiar with the Runtime class, you know that it also
allows you to discover memory usage and add a shutdown hook. But
probably the most popular use of the class prior to 5.0 was to
execute a command in a separate process. This was done through
one of the six versions of the exec() method of Runtime:
public Process exec(String command)
throws IOException
public Process exec(String command,
String[] envp)
throws IOException
public Process exec(String command,
String[] envp,
File dir)
throws IOException
public Process exec(String[] cmdarray)
throws IOExceptionjava
public Process exec(String[] cmdarray,
String[] envp)
throws IOException
public Process exec(String[] cmdarray,
String[] envp,
File dir)
throws IOException
Before you call the exec() method, you specify the command and
its arguments, environment variable settings, and working
directory. All versions of the method return a java.lang.Process
object for managing the created process. This allows you to get
the input or output stream of the subprocess and exit status
(among other available information).
Here's an example, DoRuntime, that shows how to execute
a command with the original Runtime class. The command to run is
09:30
09:30
09:30
09:23
AM
AM
AM
AM
<DIR>
<DIR>
.
..
1,146 DoRuntime.class
724 DoRuntime.java
http://java.sun.com/developer/copyright.html
Core Java Technologies Tech Tips
July 27, 2005
Trademark Information: http://www.sun.com/suntrademarks/
Java, J2SE, J2EE, J2ME, and all Java-based marks are trademarks
or registered trademarks of Sun Microsystems, Inc. in the
United States and other countries.