Book excerpt: Executing tasks in threads
Learn how to execute tasks in threads in this excerpt from Java Concurrency in Practice (Brian
Goetz et al. (Addison Wesley Professional, May 2006)). The authors
present the Executor interface as the basis for a
flexible and powerful framework for asynchronous task execution
that supports a wide variety of task execution policies.
Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes and Doug Lea,
September 2006
Is your code ready for the next wave in commodity computing?
We are standing on the cusp of a real sea change in commodity
hardware architectures—the move away from individual
processing units that run at high speeds and temperatures to a
team of connected units that individually (now at least)
are less powerful, but make up for that several times over in sheer
numbers. This shift is pervasive, from Unix servers to game
consoles. Read on as Humphrey Sheil maps out the history of
parallelism, examines the current hardware offerings from the main
vendors, looks at some of the seminal software theory, and
identifies the strong and weak areas of the Java platform as they
relate to parallelism.
Humphrey Sheil,
July 2006
Build application search features modeled on Web searches
Building search features in applications modeled on Web searches
provides users with a familiar and intuitive search interface. It
keeps the user interface for searching simple and consistent across
the application. This article presents a framework that can be used
to develop simple yet powerful and extensible searching
functionality in applications, more specifically, database-driven
applications.
Suneel Parthasarathy,
March 2005
Pool resources using Apache's Commons Pool Framework
Resource pooling is not new and is being widely used to conserve
and optimize the usage of resources like threads, sockets, and
database connections. Web server implementations routinely use
thread pool implementations for performance and scalability
reasons. Commons Pool, from the Apache Software Foundation, may not
be the first when it comes to implementation, but it deserves its
own place for defining a framework that allows any arbitrary object
to be pooled. This article discusses Commons Pool and shows how to
use it to implement a thread pool.
Murali Kosaraju,
January 2005
Write thread-safe servlets
You finished your Web application, thoroughly tested it, and it's
now deployed. You're feeling bullet proof and 10-feet tall, and
then it happens...the customer-support calls start coming in:
"We're seeing some strange things. A customer is seeing someone
else's information!" As your professional career flashes before
your eyes, you scan for any possible scenarios that you may have
missed. "Did I test it under load? Could that make a difference? I
haven't run into this kind of problem before. Why now?" Well, if
you're a Java programmer and developing Web applications, then
thread safety is an issue you must contend with regardless of your
application type or project size. Your servlet and JavaServer Pages
code is subject to thread safety issues due to the way your servlet
container uses it. This article presents thread safety in simple
terms and explains why it can be an issue for your servlets. You
also learn how to avoid writing non-thread-safe servlets and how to
make servlets thread-safe.
Phillip Bridgham,
July 2004
Customize SwingWorker to improve Swing GUIs
The presentation layer of Swing-based applications mainly consists
of event-handling logic, layout properties, and graphical user
interface (GUI) components. While such code should be easy to
program because of the Swing programming model's single-threaded
nature, more complicated concurrent constructs such as
SwingWorker are often needed to perform time-consuming
tasks while preserving screen liveliness. In this article, Yexin
Chen discusses some negative consequences caused by
SwingWorker usage and illustrates how to customize
SwingWorker to achieve additional architectural design
goals.
Yexin Chen,
June 2003
Simply Singleton
Sometimes it's appropriate to have exactly one instance of a class: window managers, print spoolers, and filesystems are prototypical
examples. Typically, those types of objects—known as singletons—are accessed by disparate objects throughout a software system,
and therefore require a global point of access. Of course, just when you're certain you will never need more than one instance,
it's a good bet you'll change your mind.
David Geary,
April 2003
The thread threat
February 14, 2003
Vladimir Roubtsov,
February 2003
Java Tip 132: The taming of the thread
With MutableThread and ThreadWatchDog,
you can make your threads come back to life and continue running
seamlessly even after thread death.
Roy M. Pueschel,
November 2002
Achieve strong performance with threads, Part 4
My previous three articles explored an assortment of thread concepts: the Thread class, the Runnable interface, exceptions
and the run() method, synchronization, thread scheduling, the wait/notify mechanism, and thread interruption. This month's
Java 101 concludes the thread series by focusing on thread groups, volatility, thread-local variables, timers, and the ThreadDeath
class. Want more on threads? The sidebar "Finalization and Threads" describes how various thread concepts combine to finalize
objects.
Jeff Friesen,
August 2002
Achieve strong performance with threads, Part 3
This month, I continue my four-part thread series by focusing on thread scheduling, the wait/notify mechanism, and thread
interruption. You'll investigate how either a JVM or an operating-system thread scheduler chooses the next thread for execution.
As you'll discover, priority is important to a thread scheduler's choice. You'll examine how a thread waits until it receives
notification from another thread before it continues execution and learn how to use the wait/notify mechanism for coordinating
the execution of two threads in a producer-consumer relationship. Finally, you'll learn how to prematurely awaken either a
sleeping or a waiting thread for thread termination or other tasks. I'll also teach you how a thread that is neither sleeping
nor waiting detects an interruption request from another thread.
Jeff Friesen,
July 2002
Study guide: Achieve strong performance with threads, Part 3
Welcome to the Java 101 study guide. This guide
complements "
Achieve Strong Performance with Threads, Part 3." It provides a
glossary of terms specific to that article, tips and cautions, new
homework, solutions to last month's homework, and Jeff Friesen's
answers to questions from your fellow students.
The Java 101 study guides are evolving
documents—they change periodically. For example, if you
submit a question long after Jeff posts the relevant study guide,
your question and his answer will eventually make its way onto that
guide. Furthermore, from time to time, he will post additional
examples and other material that clarifies an article's topic, so
be sure to revisit the
study guides periodically.
Jeff Friesen,
July 2002
Study guide: Achieve strong performance with threads, Part 4
Welcome to the Java 101 study guide. This guide
complements "
Achieve Strong Performance with Threads, Part 4." It provides a
glossary of terms specific to that article, tips and cautions, new
homework, solutions to last month's homework, and Jeff Friesen's
answers to questions from your fellow students.
The Java 101 study guides are evolving
documents—they change periodically. For example, if you
submit a question long after Jeff posts the relevant study guide,
your question and his answer will eventually make its way onto that
guide. Furthermore, from time to time, he will post additional
examples and other material that clarifies an article's topic, so
be sure to revisit the
study guides periodically.
Jeff Friesen,
July 2002
Achieve strong performance with threads, Part 2
Is creating multithreaded Java programs hard? With the information gleaned from Part 1 of Java 101's thread series only, you
might answer no. After all, last month I showed you how easy it is to create thread objects, start threads that associate
with those objects by calling Thread's start() method, and perform simple thread operations by calling other Thread methods,
such as the three overloaded join() methods. Yet many developers face difficulty when developing properly behaving multithreaded
programs. All too often, their programs function erratically or produce erroneous values. For example, a multithreaded program
might store incorrect employee details, such as name and address, in a database. The name might belong to one employee, whereas
the address belongs to another. What causes that strange behavior? The lack of synchronization: the act of serializing, or
ordering one at a time, thread access to those code sequences that let multiple threads manipulate class and instance field
variables, and other shared resources. I call those code sequences critical code sections.
Jeff Friesen,
June 2002
Study guide: Achieve strong performance with threads, Part 2
Welcome to the Java 101 study guide. This guide
complements "
Achieve Strong Performance with Threads, Part 2." It provides a
glossary of terms specific to that article, tips and cautions, new
homework, solutions to last month's homework, and Jeff Friesen's
answers to questions from your fellow students.
The Java 101 study guides are evolving
documents—they change periodically. For example, if you
submit a question long after Jeff posts the relevant study guide,
your question and his answer will eventually make its way onto that
guide. Furthermore, from time to time, he will post additional
examples and other material that clarifies an article's topic, so
be sure to revisit the
study guides periodically.
Jeff Friesen,
June 2002
Some reader favorites:
EJB fundamentals and session beans
Create a scrollable virtual desktop in Swing
Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API
Free Download - 5 Minute Product Review. When slow equals Off: Manage the complexity of Web applications - Symphoniq
![]()
Free Download - 5 Minute Product Review. Realize the benefits of real user monitoring in less than an hour. - Symphoniq