img
.
entire method does not have to be synchronized. Why lock an entire method when only a few lines
of code in the method have to be locked? A better idea is to synchronize the smallest section of
code possible in a method that needs to be protected.
You can do this by using the synchronized keyword in the method code (see Code Example 10-3).
Notice that the synchronized keyword is used to protect only the section of code in the method
that needs to be protected. This will allow for more concurrency and will also help with the
performance of programs.
Example 10-3 Synchronizing Part of a Method>
public void aMethod() {
if (some_condition)
synchronize (this) {
// Do synchronized work here
}
else {
// Do unsynchronized work here
}
}
}
5. Threaded class downloads
A problem we often see with large Java programs is the time it takes to download all of the class
files needed for execution. This problem can be solved by creating a custom class loader, which
downloads only the initial class files needed for the program to begin its execution. Then while the
program begins its execution, thread(s) can be created to continue the download of other class files
in the background. Cyrus InterSoft offers a commercial solution to problems like this. Cyrus offers
a number of Java resources that aid in the download and execution of Java programs. For example,
they are able to begin the execution of a Java program even before all of it is downloaded, as well
as run multiple Java programs inside the same Java VM. Improvements like these can have a
dramatic effect on the performance of Java programs.
General Tips and Hints
1. It is very helpful in a thread dump analysis to give the thread a meaningful name. If you
spawn many threads without meaningful names, it becomes next to impossible to figure
out what is really going on.
2. I'd say the biggest issue I've run into at customer sites is the mismatch between the ease of
Java thread syntax and semantics. Everybody talks about how easy threads are to do in
Java, and what these people are always referring to is the ease with which someone can
write some threaded code, the syntax. What often is overlooked until too late is how
complex the thread semantics can be and how difficult these portions of the code will be
to debug. Thus, one often ends up with a situation where some fairly novice developers
have created code that is too complex for them to debug.
3. Many customers running on Solaris (JDK 1.1) do not realize that by default they are using
green threads. They then wonder why their quad processor E4000 runs no better (or worse)
than a single-processor NT box, or why the load is not distributed across processors.
Daemon Threads
Search WWH :
Custom Search
Previous Page
Multithreaded Programming with JAVA - Topic Index
Next Page
Multithreaded Programming with JAVA - Bookmarks
Home