Professional Documents
Culture Documents
Editor’s Note 2
by Jonathan Erickson
Techno-News
Gesture-Based Computing on the Cheap 3
With a single piece of inexpensive har dwar e — a multicolor ed glove — MIT r esear chers
ar e making gestur e-based inter faces mor e accessible.
Features
The NoSQL Alternative 5
by Ken North
Low-cost, high-per formance database options make gains.
Columns
Book Review 22
by Mike Riley
Examining Head First C#, Second Edition by Andrew Stellman and Jennifer Gr eene.
Other Voices 27
by Jake Sorofman
Automation, economics, and the birth of market-driven IT.
Effective Concurrency 28
by Herb Sutter
How to automate best practices for thr eads and raise the semantic level of our code
Entire contents Copyright© 2010, Techweb/United Business Media LLC, except where otherwise noted. No portion of this publication may be repro-
duced, stored, transmitted in any form, including computer retrieval, without written permission from the publisher. All Rights Reserved. Articles
express the opinion of the author and are not necessarily the opinion of the publisher. Published by Techweb, United Business Media Limited, 600
Harrison St., San Francisco, CA 94107 USA 415-947-6000.
D r. D o b b ’s D i g e s t [ ]
Editor’s Note
Good On Ya Atlassian:
A Job Well Done
L
et’s hear it for Atlassian (www.atlassian.com), the Australian vendor of software development and
collaboration tools. No, not for the quality of their tools (which is first rate; Atlassian has won more
than one Jolt Award). Nor for the cool, but unusual, names of their products (Clover, Fisheye,
Bamboo, Greenhopper, and Jira, among others). Rather, three cheers for the company’s humanitari-
an efforts, as exemplified by its recently announced Community Award.
The recipient of Atlassian’s first ever Community Award is Mercy Ships (www.mercyships.org), the organ-
ization that uses hospital ships to deliver free health care to the developing world. Since 1978, Mercy Ships
have visited more than 70 countries, providing services valued at more than $808 million, directly impacting
By Jonathan more than 2.5 million beneficiaries. More than 1200 crew worldwide, representing more than 40 nations are
Erickson, joined each year by 2000 short-term volunteers. Professionals including surgeons, dentists, nurses, health care
Editor In Chief trainers, teachers, cooks, seamen, engineers, and agriculturalists donate their time and skills to the effort.
Atlassian’s $10,000 Community Award will benefit the people of Togo, West Africa, through the charity’s
program of plastic reconstructive surgeries onboard the hospital ship Africa Mercy. In truth, Atlassian has been
supporting Mercy Ships for several years by donating some of those fine products with odd names. Since
October 2005, Atlassian donated software via an Atlassian Community License to Mercy Ships, a gift estimat-
ed at $30,000 according to the charity. With this cost savings, Mercy Ships surgeons can remove 60 facial
tumors from patients ostracized by their communities, do 120 cataract surgeries to restore sight to young and
old, correct 120 cleft lip/palates in children seen as cursed, correct 60 obstetric fistulas for women usually aban-
doned by their husbands and families, or offer 600 free dental procedures in areas where there is no dentist.
According to Mercy Ships CIO Chris Gregg, Mercy Ships Information Services began using Atlassian
Confluence as a wiki to enable information between the charity’s 15 offices and a ship in Africa to be easily
shared, commented upon, and edited, regardless of different geographical locations and time zones. They use
Atlassian JIRA to manage issues for IT projects, systems, and services. Procurement offices in the U.S.,
Holland, Germany, and the UK use the resource to collaborate on restocking items for the ship’s hospital and
community. Additionally, the ship’s Marine Operations is able to publish business management documents to
a standard that satisfies certification conditions to operate the Africa Mercy at sea and in port.
“The partnership with Atlassian, and Confluence in particular, has helped to increase the efficiency of our
organization and help us bring hope to more people,” says Sam Smith, Mercy Ships CEO. Daily, more than 400
Mercy Ships crew members from 40 nations onboard ship and hundreds of land-based support crew working
internationally in remote locations benefit from Atlassian’s wiki program through interaction with the charity’s
intranet.
Smith went on to say that “Mercy Ships has been fortunate to increase our capacity to serve the poor with
the addition of our new flagship, Africa Mercy. This ship more than doubled the capacity of all of the ships
in our history.”
You can see how Mercy Ships is transforming lives in West Africa in a video at
www.youtube.com/v/Cr6zB1qlwx0&hl=en_US&fs=1&.
And Atlassian? Beauty, mates!
EDITORIAL
E
ver since Steven Spielberg’s 2002 sci-fi movie Minority Report, in which a black-clad Tom VICE PRESIDENT GROUP SALES
Martha Schwartz
Cruise stands in front of a transparent screen manipulating a host of video images simply
by waving his hands, the idea of gesture-based computer interfaces has captured the imag- AUDIENCE DEVELOPMENT
ination of technophiles. Academic and industry labs have developed a host of prototype CIRCULATION DIRECTOR
Karen McAleer
gesture interfaces, ranging from room-sized systems with multiple cameras to detectors built into MANAGER
laptops’ screens. But MIT researchers have developed a system that could make gestural interfaces John Slesinski
much more practical. Aside from a standard webcam, like those found in many new computers, the
DR. DOBB’S
system uses only a single piece of hardware: a multicolored Lycra glove that could be manufactured 600 Harrison Street, 6th Floor, San
for about a dollar. Francisco, CA, 94107. 415-947-6000.
Other prototypes of low-cost gestural interfaces have used reflective or colored tape attached to www.drdobbs.com
the fingertips, but “that’s 2D information,” says Robert Wang, a graduate student in the Computer UBM LLC
Science and Artificial Intelligence Laboratory (CSAIL; http://www.csail.mit.edu/) who developed the
new system together with Jovan Popovic, an associate professor of electrical engineering and com- Pat Nohilly Senior Vice President,
puter science. “You’re only getting the fingertips; you don’t even know which fingertip [the tape] is Strategic Development and Business
Administration
corresponding to.” Wang and Popoviç’s system, by contrast, can translate gestures made with a
Marie Myers Senior Vice President,
gloved hand into the corresponding gestures of a 3D model of the hand on screen, with almost no Manufacturing
lag time. “This actually gets the 3D configuration of your hand and your fingers,” Wang says. “We
get how your fingers are flexing.” TechWeb
The most obvious application of the technology, Wang says, would be in video games: Gamers
Tony L. Uphoff Chief Executive Officer
navigating a virtual world could pick up and wield objects simply by using hand gestures. But Wang
John Dennehy, CFO
also imagines that engineers and designers could use the system to more easily and intuitively David Michael, CIO
manipulate 3D models of commercial products or large civic structures. John Siefert, Senior Vice President and
The glove went through a series of designs, with dots and patches of different shapes and colors, Publisher, InformationWeek Business
but the current version is covered with 20 irregularly shaped patches that use 10 different colors. Technology Network
Bob Evans Senior Vice President and
The number of colors had to be restricted so that the system could reliably distinguish the colors
Content Director, InformationWeek
from each other, and from those of background objects, under a range of different lighting conditions. Global CIO
The arrangement and shapes of the patches was chosen so that the front and back of the hand would Joseph Braue Senior Vice President,
be distinct but also so that collisions of similar-colored patches would be rare. For instance, Wang Light Reading Communications
explains, the colors on the tips of the fingers could be repeated on the back of the hand, but not on Network
Scott Vaughan Vice President,
the front, since the fingers would frequently be flexing and closing in front of the palm.
Marketing Services
Technically, the other key to the system is a new algorithm for rapidly looking up visual data in John Ecke Vice President, Financial
a database, which Wang says was inspired by the recent work of Antonio Torralba, the Esther and Technology Network
Harold E. Edgerton Associate Professor of Electrical Engineering and Computer Science in MIT’s Beth Rivera Vice President, Human
Department of Electrical Engineering and Computer Science and a member of CSAIL. Once a web- Resources
Fritz Nelson Executive Producer,
cam has captured an image of the glove, Wang’s software crops out the background, so that the glove
TechWeb TV
alone is superimposed upon a white background. Then the software drastically reduces the resolu-
tion of the cropped image, to only 40 pixels by 40 pixels. Finally, it searches through a database con-
taining myriad 40×40 digital models of a hand, clad in the distinctive glove, in a range of different
positions. Once it has found a match, it simply looks up the corresponding hand position. Since the
system doesn’t have to calculate the relative positions of the fingers, palm, and back of the hand on
the fly, it’s able to provide an answer in a fraction of a second.
A
By Ken North new generation of low-cost, high-per- premium on reliability. And social network appli-
formance database software is rapidly cations such as Facebook and Amazon.com have
emerging to challenge SQL’s domi- adopted BASE (basically available, soft state, even-
nance in distributed processing and tually consistent) properties over the more famil-
Big Data applications. Some companies have iar ACID (atomicity, consistency, isolation, dura-
already traded SQL’s rich functionality for these bility) ones to serve their massive Web user com-
new options that let them create, work with, and munities of millions.
manage large data sets. These differences are one reason non-relational
A big reason for this movement, dubbed NoSQL data stores, document-centric databases,
NoSQL, is that different implementations of Web, and column stores have gained traction. They’re
enterprise, and cloud computing applications have more like specialized tools rather than the Swiss
different requirements of their databases. Not Army knife functionality of SQL platforms.
every app requires rigid data consistency, for System architects should consider the special-
example. ized features and functions an app needs in choos-
Also, when an application uses data distributed ing a database. NoSQL databases can be built
across hundreds or even thousands of servers, sim- specifically for functions such as BI, OLTP, CRM,
ple economics points to using no-cost server soft- social networks, and data warehousing, and
ware as opposed to paying per-processor license include features such as scalability, partitioning,
fees. Once freed from license fees, you can scale security, and elasticity.
horizontally with commodity hardware or opt for
a cloud computing service and avoid the capital Scalability and High Availability
expenses altogether. Previous tools didn’t always For cloud computing and high-volume Web sites
facilitate this. such as eBay, Amazon, Twitter, and Facebook, scal-
Challenges to SQL’s hegemony are coming from ability and high availability are essential. In fact,
specialized products built from the ground up for they’re the reason distributed databases have
large-scale analytics and document storage, as well relaxed consistency requirements.
as for building operational systems that require Operational systems in high-availability envi-
high availability more than consistency when par- ronments must survive software, server, and net-
titioning data. work segment failures, and provide scalability
Applications such as online transaction pro- despite unpredictable surges in demand for com-
cessing, business intelligence, customer relation- puting resources. One approach to building such
ship management, document processing, and systems is to use distributed databases with a
social networking don’t have identical needs for shared-nothing architecture and horizontal parti-
data, query, or index types, nor do they have equiv- tioning. Elasticity and sharding (partitioning) —
alent requirements for consistency, scalability, and both NoSQL features — are solutions for scaling
security. out horizontally to provide availability and for pro-
For example, BI applications run analytical and cessing Big Data.
decision-support queries that can exploit bitmap A variety of data stores are gaining popularity
indexes for operations with gigabyte- or terabyte- for creating applications for scalable websites and
sized databases. Web analytics, drug discovery, elastic environments such as the private or public
financial modeling, and similar applications look cloud. Distributed key-value stores are great when
to distributed systems for efficiently processing you don’t need SQL rule enforcement, strong con-
gigabyte- to terabyte-sized data sets. OLTP puts a sistency, complex queries, integrated queuing, or
I
By Anthony William n “Associate Mutexes with Data to Prevent Races” (http://www.drdobbs.com/224701827), Herb
Sutter examined the importance of ensuring that the correct mutex is locked when accessing data,
then presented one technique for achieving this. In this article, I present an alternative technique
that I have used to achieve the same aims, in the form of the SynchronizedValue class template.
int readValue1()
{
std::lock_guard<std::mutex> lk(m1);
return value1;
}
int readValue2()
{
std::lock_guard<std::mutex> lk(m1); // oops: wrong mutex
return value2;
}
Moreover, managing the mutex lock also clutters the source code, making it harder to see what is
really going on.
The use of SynchronizedValue<T> solves both these problems —- the mutex is intimately tied to the
value, so you cannot access it without a lock, and yet access semantics are still straightforward. For
simple accesses, SynchronizedValue<T> behaves like a pointer-to-T; for example:
SynchronizedValue<std::string> value3;
std::string readValue3()
{
return *value3;
}
void setValue3(std::string const& newVal)
{
*value3=newVal;
}
void appendToValue3(std::string const& extra)
{
value3->append(extra);
}
Both forms of pointer dereference return a proxy object rather than a real reference, to ensure that
the lock on the mutex is held across the assignment or method call, but this is transparent to the user.
if(!u1->empty())
By calling update() you obtain an Updater object that holds a {
lock on the mutex protecting the data, and which can be used to u2->push_back(u1->front());
u1->pop_front();
access the protected data. The lock is held until the Updater }
}
object is destroyed, so you can safely perform multi-part opera-
tions. The Updater object also acts as a pointer-to-T, just like This works well in some scenarios, but not all — if the same two
SynchronizedValue does, but this time the lock is already held. objects are updated together in different sections of code, then you
For example, the following function adds a trailing slash to a need to take care to ensure that the Updater objects are construct-
path held in a SynchronizedValue<std::string>. The use of the ed in the same sequence in all cases. Otherwise, you have the poten-
Updater object ensures that the string hasn’t changed in between tial for deadlock. This is just the same as when acquiring any two
the query and the update. mutexes.
void addTrailingSlashIfMissing(SynchronizedValue<std::string> & path)
{
SynchronizedValue<std::string>::Updater u=path.update();
Under the Hood
We’ve seen how to use SynchronizedValue<T> to ensure that the
if(u->empty() || (*u->rbegin()!=’/’))
{ right mutex is always locked when accessing an object; now it’s
*u+=’/’;
}
time to look under the hood and see how it works.
} It really is very simple: At its heart, a SynchronizedValue<T> is
just a T object and an std::mutex. All the work is done by the proxy
Operations Across Multiple Objects objects returned from the operators.
Though SynchronizedValue<T> works very well for protecting a template<typename T>
single object of type T, nothing that we’ve seen so far solves the class SynchronizedValue
{
problem of operations that require atomic access to multiple T data;
std::mutex m;
objects unless those objects can be combined within a single struc- public:
DerefValue operator*();
ture protected by a single mutex. Updater operator->();
One way to protect access to two SynchronizedValue<T> objects Updater update();
};
is to construct a SynchronizedValue<T>::Updater for each object and
use those to access the respective protected values; for instance:
SynchronizedValue<std::queue<MessageType> > q1,q2; Basic Pointer
void transferMessage()
{
Operations
SynchronizedValue<std::queue<MessageType> >::Updater u1=q1.update(); The basic pointer dereference operator returns an instance of the
SynchronizedValue<std::queue<MessageType> >::Updater u2=q2.update();
private nested class SynchronizedValue<T>::DerefValue:
T& operator*()
{
This class holds an std::unique_lock<std::mutex>, which man- return data;
}
ages the lock and a reference to the protected T object. The lock is
acquired when the object is constructed by the call to operator*, and
released when the object is destroyed. DerefValue has a simple con- Wrapping It Up
version operator to T, which allows retrieval of the protected value, I’ve found classes like SynchronizedValue<T> invaluable in many
and also an assignment operator that allows the value to be set. projects, as it provides a simple way of ensuring that the right
mutex is always held when accessing protected data. It’s not a
operator T() panacea, but those cases where it doesn’t work likely require more
{
return data; careful thought anyway.
}
You can download the source code at http://i.cmpnet.com/
DerefValue& operator=(T const& newVal)
{
ddj/images/article/2010/code/synchronized_value.zip or from
data=newVal; http://www.stdthread.co.uk/syncvalue if you want to try it out. As
return *this;
} written, it relies on C++0x facilities, but it should be easy to sub-
stitute an alternative mutex implementation if desired.
The arrow operator is implemented similarly, except this time
it returns an instance of SynchronizedValue<T>::Updater. Because
SynchronizedValue<T>::Updater also has an implementation of the — Anthony Williams is author of the book C++ Concurrency in
arrow operator, the chaining rules for this operator mean that you Action (http://www.manning.com/williams/) and of the just::thread
can access members of the protected object directly using the nor- C++0x thread library (http://www.stdthread.co.uk/). He can be con-
mal pointer syntax, and the temporary Updater object holds the tacted at anthony.ajw@gmail.com.
lock for you. Just like DerefValue, the Updater object acquires the
lock in its constructor and releases it in its destructor.
This is the same Updater object we get from the update() mem-
ber function, so it has a plain pointer dereference operator that
returns a reference to the protected object in addition to the arrow
operator that returns the pointer: Retur n to Table of Contents
D
By Mark O. Pendergast ata quality and accuracy is key to suc- the value when it receives the focus.
Therefore, a field that holds currency can
cess for every application. Databases
limit the number of decimal places shown,
full of invalid phone numbers, have a dollar sign, and have commas separat-
improperly formatted email address- ing the thousands while it is not being
es, or out of range numeric data are of no use to changed, then automatically change to a form
anyone. Java Swing interface programmers know with just digits and decimals when the focus
is shifted to it. A type of formatter called a
all too well the work involved in error checking
MaskFormatter can provide a “type-in-the-
user inputs taken from vanilla JTextFields. Many blank” template to guide the users input.
probably have been tempted to implement inter- MaskFormatters are useful for data types
faces using JFormattedTextFields, but have been such as phone numbers, dates, and Social
put off by the complexity of masks, formatters, Security numbers.
• The third, and most important difference is
verifiers, abstract classes, and interfaces that are
that when combined with InputVerifiers they
required. In this article, I explain the proper use can handle all the parsing, range checking,
of the JFormattedTextField and its related classes and formatting required for processing GUI
to create forms that provide both guided input for form interfaces. For example, Figure 1(a)
users and validated data for the application. The shows a JFormattedTextField used to input
currency with the focus, Figure 1(b) shows
source code for a sample application and a library
the same JFormattedTextField without the
of reusable classes for common data entry items focus. Table 1 compares the code required to
based on JFormattedTextField is available for input, convert, and validate a double value
download at http://i.cmpnet.com/ddj/images/arti- using JTextField and JFormattedTextField.
cle/2010/code/JFormattedText.zip.
JFormattedTextFields are alternatives to Implementing a
JTextFields that are well worth using. They differ JFormattedTextField
from JTextFields in several ways: According to the Java API:
JFormattedTextField does not do the formatting itself,
• First, they maintain a current “value” associat- rather formatting is done through an instance of
ed with the field as an Object. The getValue and JFormattedTextField.AbstractFormatter, which is
setValue methods are provided to access it. obtained from an instance of JFormattedText-
• Second, they use formatter objects to control Field.AbstractFormatterFactory.
the display of the value. One formatter is
used to convert the value object to a String for What this really means is that to make a
display when the field does not have the JFormattedTextField operate, you need to create
focus; a separate formatter is used to convert
Figur e 1a
an instance of an AbstractFormatterFactory. The easiest way to do two “0”s to the right of the decimal point indicates only two deci-
this is to use a convenience class called DefaultFormatterFactory, mal places are to be shown (rounding up/down automatically). The
then attach it to the JFormattedTextField. The primary purpose of “,” acts as a grouping separator. Since there are three “#”s between
the DefaultFormatterFactory is to maintain three Abstract- the decimal point and the comma, the numbers to the left of the
Formatter classes for the JFormattedTextField. One abstract for- decimal will be grouped in sets of three. You only need to specify
matter is the default formatter, it is used if a more specific format- the position of the first grouping separator.
ter cannot be found; another is the display formatter used to for- The example above adds the grouping separator and dollar sign.
mat the value when the JFormattedTextField does not have the Both of these will cause parse errors when converting back to a
focus; and the third is the edit formatter, used to format the value number. Therefore, you will typically need a separate
when the JFormattedTextField has the focus. NumberFormatter for the edit and display formats. The one used
DefaultFormatterFactory dff = for the edit format would exclude the dollar sign, grouping separa-
new DefaultFormatterFactory(defFormatter, tor (comma), and use “#”s to the right of the decimal. For example:
displayFormatter, editFormatter);
JFormattedTextField tf = new JFormattedTextField(dff); NumberFormatter displayCurrencyFormatter =
new NumberFormatter(new DecimalFormat(“$ #,###.00”));
NumberFormatter editCurrencyFormatter =
The formatter classes are primarily responsible for providing new NumberFormatter(new DecimalFormat(“#.##”));
the body for two methods, stringToValue and valueToString. The getValue method of the JFormattedTextField calls the parse
stringToValue parses a String and creates an object of the proper method within the DecimalFormat class, and may return a Long or
type (Long, Double, Date, Integer, etc). valueToString takes the cur- a Double, depending on the String involved. If the String has a dec-
rent value and formats it for display. AbstractFormatter classes imal in it, a Double is returned, if it doesn’t, a Long is returned. To
may in turn rely on specific format classes such as DecimalFormat make the class of the return value more predictable (and avoid
and SimpleDateFormat. Creating formatter classes from scratch class cast exceptions), you should call the setValueClass method
can be a daunting task. Fortunately, the standard Java API provides and specify whether you want a Long, Double, Float, Short, Byte,
numerous formatter classes that can be used as is. The standard or Integer; for example:
class you use is determined by the type of data you need to format. displayCurrencyFormatter.setValueClass(Double.class);
Numeric data (currencies, counts, salaries) should use the editCurrencyFormatter.setValueClass(Double.class);
NumberFormatter class, fixed length string data should use the The following is a complete example for creating a
MaskFormatter class (social security numbers, credit card num- JFormattedTextField for entering salary:
bers, phone numbers, dates), and variable length string data will
// create the JFormattedTextField
need to have a custom formatter created based on the JFormattedTextField salaryField = new JFormattedTextField();
DefautFormatter class. // create the formatters, default, display, edit
NumberFormatter defaultFormatter = new NumberFormatter(new
DecimalFormat(“#.##”));
NumberFormatter NumberFormatter displayFormatter =
new NumberFormatter(new DecimalFormat(“$ #,###.00”));
A NumberFormatter can be used for any numeric field, age, NumberFormatter editFormatter = new NumberFormatter(new
DecimalFormat(“#.##”));
income, annual rainfall, quantity, cost, etc. A NumberFormatter // set their value classes
defaultFormatter.setValueClass(Double.class);
object is created using a constructor that accepts an object of type displayFormatter.setValueClass(Double.class);
NumberFormat as its only parameter. Typically a DecimalFormat editFormatter.setValueClass(Double.class);
// create and set the DefaultFormatterFactory
object (subclass of NumberFormat) is used. For example: DefaultFormatterFactory salaryFactory =
new
NumberFormatter currencyFormatter = DefaultFormatterFactory(defaultFormatter,displayFormatter,editFor
new NumberFormatter(new DecimalFormat(“$ #,###.00”)); matter);
salaryField.setFormatterFactory(salaryFactory);
In the above example, a DecimalFormat object is specified that
can take a numeric value and convert it to a String using the spec- MaskFormatter
ified pattern. The “#” character is replaced with a digit (or absent The MaskFormatter is best used to format Strings that have a fixed
if 0), the “0” is replaced with a digit. The fact that there are only length and set format. Examples include: dates, Social Security
numbers, phone numbers, credit-card numbers, ZIP codes, vehicle
ID numbers (VINs). MaskFormatters are not suitable for Strings
with variable numbers of characters, e.g. URLs and email address-
es. A single instance of the MaskFormatter can be used for both the
edit and display formats of an AbstractFormatterFactory. A
MaskFormatter object is created using a constructor that accepts a
formatting mask as its only parameter. The formatting mask spec-
Table 2: MaskFor matter characters. ifies what type of characters can appear in each position and any
literal characters. Literal characters are fixed in place and are not
changeable by the users; for example, the dash and parentheses in
a phone number would be considered literal characters. Table 2
shows the different mask characters used by the MaskFormatter.
For example, the mask “###-##-####” could be used for a
Social Security number. The #s allow the user to type in any num- Table 3: Masks for common data types.
ber, the dashes are literals and fixed in place. Similarly for a ZIP
code the mask “#####-####” could be used:
MaskFormatter zipMask = new MaskFormatter(“#####-####”); Custom Formatters
MaskFormatter ssnMask = new MaskFormatter(“###-##-####”); If you have a data type that is variable in length and does not fit
You can restrict what the user types in even more by invoking well with one of the existing formatter classes, or requires special
the setValidCharacters or the setInvalidCharacters method. For rules for conversion, then you can create your own formatter class
example, if you wanted to restrict the digits in a ZIP code to just based on the DefaultFormatter class or any of the other Formatter
2s and 3s, then you could make the following call: classes (e.g. NumberFormatter). You will need to override the
zipMask.setValidCharacters(“23”); //only allow 2, 3 in the zip code stringToValue and valueToString methods to provide the behavior
you desire. stringToValue accepts a String as an input, parses it,
Alternatively, if you wanted any digit but a 2 or a 3 then you and returns an Object. If the parsing fails, then a ParseException
could make the following call: should be thrown. The type of Object returned should match the
zipMask.setInvalidCharacters(“23”); // don’t allow 2,3 in the zip code. results of getValueClass. The code necessary to parse the String
will depend on the data you are parsing. If you are parsing a prop-
A final consideration when creating a MaskFormatter is setting er name, then your method may just check to see that the String is
the placeholder character. This character is inserted into the String not empty and force the first letter to be capitalized. If you are
if the current value has too few characters or when the user press- parsing a URL or email address, then you’ll want to employ a
es the delete or backspace keys while editing. It is common to use Pattern object to test the validity and sequence of the characters.
an underscore as the placeholder, but any character will do. To valueToString accepts an Object and converts it to a String. If
avoid confusion, select a character that is not used as a literal or the conversion fails, then a ParseException is thrown. The conver-
valid character in the mask. To set the placeholder, call the sion may be as simple as calling the toString() method of the Object
setPlaceholderCharacter method; for example: or the conversion may adjust the data before returning it. For
example, a field that holds a percentage may want to store 5.3% as
zipMask.setPlaceholderCharacter(‘_’); // use an underscore as a
placeholder
.053, but display it as 5.3%. In this case, the valueToString method
would multiply the value by 100, format it with the desired num-
Table 3 provides a few common masks. You may want to consider ber of decimal places, then tag on the percent sign.
using a fixed-width font (such as Courier) for the The EmailFormatter class extends the DefaultFormatter
JFormattedTextField. This makes the overall appearance and char- class and uses a precompiled pattern to check an email
acter spacing more pleasing to the eye. address. For more information on the email format and pattern
The example that follows creates a JFormattedTextField for matcher, see “How to Validate Email, SSN, Phone Numbers in
entering a date: Java Using Regular Expressions” by Zaheer Paracha (http://
// create a MaskFormatter for Dates www.zparacha.com/validate-email-ssn-phone-number-using-
JFormattedTextField dateField = new JFormattedTextField(); java-regular-expression/). The code that follows creates a
MaskFormatter dateFormatter = new MaskFormatter(“##/##/####
##:##:## UM”); JFormattedTextField for entering an email address using
dateFormatter.setValidCharacters(“0123456789AP”);
dateFormatter.setPlaceholderCharacter(‘_’); EmailFormatter.java class, which you can download at
dateFormatter.setValueClass(String.class);
DefaultFormatterFactory dateFormatterFactory = new
http://i.cmpnet.com/ddj/images/article/2010/code/JFormattedText.
DefaultFormatterFactory(dateFormatter); zip.
dateField.setFormatterFactory(dateFormatterFactory);
JFormattedTextField emailField = new JFormattedTextField();
The example that follows creates a JFormattedTextField for EmailFormatter emailFormatter = new EmailFormatter();
DefaultFormatterFactory emailFormatterFactory =
entering a Social Security number: new DefaultFormatterFactory(emailFormatter);
emailField.setFormatterFactory(emailFormatterFactory);
JFormattedTextField ssnField = new JFormattedTextField();
MaskFormatter ssnFormatter = new MaskFormatter(“###-##-####”); If you want a custom formatter that handles numbers, then you
ssnFormatter.setValueClass(String.class);
ssnFormatter.setPlaceholderCharacter(‘_’); should create a class that extends NumberFormatter. The
DefaultFormatterFactory ssnFormatterFactory = new
DefaultFormatterFactory(ssnFormatter); PercentFormatter class extends NumberFormatter to create a class
ssnField.setFormatterFactory(ssnFormatterFactory);
to handle percentage data. The constructor accepts a other for valid values. The verify method first retrieves the text
NumberFormat object to handle formatting and parsing. The from the JComponent and tries to parse it to a double. If an excep-
valueToString method first multiplies the value by 100, then calls tion is thrown, then the foreground color is set to
super class’s valueToString method. Finally a percent is added and INVALID_COLOR and false is returned. If an exception is not
the String returned. The stringToValue first strips off any extra thrown, then the value is tested to see if is in a valid range. If it is,
whitespace or percent signs, then converts to a double using the the foreground color is set to the VALID_COLOR and true is
super class’s stringToValue method, divides by 100, then returns a returned. If the value is out of range then the foreground color is
Double object. In this way, the value is stored in a form amenable set to INVALID_COLOR and false is returned. (Also see
to using in calculations, but displayed in a manner the user DoubleVerifier.java, which you can download at
expects. http://i.cmpnet.com/ddj/images/article/2010/code/JFormattedTextr
The code that follows creates a JFormattedTextField for entering .zip.)
a percent using PercentFormatter.java, which you can download at To use this InputVerifier, first create an instance of the verifier, then
http://i.cmpnet.com/ddj/images/article/2010/code/JFormattedText. attach it to the JFormattedTextField using the setInputVerifier method.
zip.
JFormattedTextField salaryField = new JFormattedTextField();
JFormattedTextField percentField = new JFormattedTextField(); DoubleVerifier salaryVerifier = new DoubleVerifier(0,1000000);
PercentFormatter percentFormatter = salaryField.setInputVerifier(salaryVerifier); // attach the
new PercentFormatter(new DecimalFormat(“#.##”)); verifier
DefaultFormatterFactory percentFormatterFactory =
new DefaultFormatterFactory(percentFormatter);
percentField.setFormatterFactory(percentFormatterFactory);
Putting It All Together
InputVerifiers Creating a JFormattedTextField that does it all requires you to do
By default, when the focus is lost on a JFormattedTextField the the following steps:
rules for COMMIT_OR_REVERT are followed. That is, if the text 1. Create an instance of a formatter for display mode (when the con-
typed in is legal, the value is updated and the new value is dis- trol does not have the focus)
played using the display formatter; if the text is not legal, then the 2. Create an instance formatter for edit mode (when the control does
text being edited is discarded and the old value is displayed. A have the focus)
JFormattedTextField can also be set to use the rules for COMMIT. 3. Create an instance DefaultFormatterFactory based on the two for-
Under those rules, when an invalid string is entered, it is left dis- matters
played, but the value is not changed. When this happens the text 4. Attach the DefaultFormatterFactory to the JFormattedTextField us-
shown on the screen does not match the value stored in the ing the setFormatterFactory method
JFormattedTextField. So it seems the choice is to lose the edit or 5. Create an instance of an InputVerifier
have a mismatch in values. Neither of these options is desirable. To 6. Attach the InputVerifier to the JFormattedTextField using the set-
correct this, all you need to do is to attach an InputVerifier to the InputVerifier method
JFormattedTextField. An InputVerifier provides the capability to // create the JFormattedTextField
force the control to retain the focus if it has an invalid value. JFormattedTextField salaryField = new JFormattedTextField();
// create the formatters, default, display, edit
Further, the formatters attached to a JFormattedTextField usually NumberFormatter defaultFormatter = new NumberFormatter(new
DecimalFormat(“#.##”));
only check the format of the data; an InputVerifier can perform NumberFormatter displayFormatter =
new NumberFormatter(new DecimalFormat(“$ #,###.00”));
range checks and other validity checks as well. NumberFormatter editFormatter = new NumberFormatter(new
An InputVerifier is a class that inherits the InputVerifier class. DecimalFormat(“#.##”));
// set their value classes
These classes will contain a constructor and must define the body defaultFormatter.setValueClass(Double.class);
displayFormatter.setValueClass(Double.class);
of an abstract method named “verify.” This method is called when- editFormatter.setValueClass(Double.class);
ever the focus is about to be lost on a control. If verify returns true, // create and set the DefaultFormatterFactory
DefaultFormatterFactory salaryFactory =
this means all is well and the focus can be moved. If verify returns new DefaultFormatterFactory(defaultFormatter,
displayFormatter,editFormatter);
false, then the focus remains on the control. The verify method salaryField.setFormatterFactory(salaryFactory);
// create and attach an InputVerifier
accepts a JComponent as input (the JFormattedTextField) and then DoubleVerifier salaryVerifier = new
can do whatever manipulations it needs before returning true or DoubleVerifier(0,1000000);// salaries 0 to 1 mil
salaryField.setInputVerifier(salaryVerifier);
false. The following example is a reusable InputVerifier to handle // attach the verifier
double values. As you can see, this is quite a bit of code to create a fancy equiv-
The data fields define the minimum and maximum permissible alent of a JTextField. Doubtless this is one of the reasons why the
values. The constructor and the included mutator methods can set JFormattedTextField is not covered in many Java texts or used as
these. Two Color values are also present, one for invalid values, the frequently by programmers. Fortunately, object-oriented program-
ming can come to your aid. Instead of repeating the sequence of (minValue, maxValue). Four constructors are provided, a no-arg
code for every field in every program, just create a few reusable constructor that uses the default values, a constructor that accepts
classes for common data types. Adding PropertySupport to the the number of decimal places, a constructor that accepts the min
classes makes them easy to integrate into the Netbeans GUI and max values, and a constructor that accepts the min value, max
designer palette, allowing you to add them to your program graph- value, the current value, and number of decimal places. Accessor
ically like any other control. and mutator methods are provided to change the maxValue,
The code project attached to this article (available for download at minValue, and decimalPlaces.
http://i.cmpnet.com/ddj/images/article/2010/code/JFormattedTextr) The constructor first checks the min and max values, generat-
provides examples via the following reusable classes: ing an IllegalArgumentException if min is greater than max. A
DoubleVerifier (discussed previously) is then created and attached
• FormattedTextFields using the setInputVerifier method. Three NumberFormatters are
o FormattedCurrencyField
o FormattedDateField created. The display and edit formatters are created based on the
o FormattedDoubleField DecimalFormat class. Display includes the grouping character
o FormattedEmailField while the edit does not. These formatters are then used to create a
o FormattedIntegerField DefaultFormatterFactory, which in turn is attached using the
o FormattedPercentField setFormatterFactory method. PropertyChange support is provided
o FormattedPhoneField
o FormattedSSNField with the propertySupport variable and implemented using calls to
• Custom verifiers the firePropertyChange method in the mutator methods. The
o DateVerifier FormattedDoubleField class can be compiled in NetBeans and
o DoubleVerifier added to the GUI palette using the Palette Manager.
o EmailVerifier Program code that uses the FormattedDoubleField just needs to
o IntegerVerifier
o MaskVerifier call the getValue and setValue methods. getValue will return an
(used by JFormattedPhoneField and JFormattedSSNField) Object that can be cast to a Double, setValue assumes a Double
o PercentVerifier object will be given. A simple adder using three
FormattedDoubleFields — xField, yField, zField — would look like:
• Custom formatters
Double x = (Double)xField.getValue();
o EmailFormatter Double y = (Double)yField.getValue();
o PercentFormatter zField.setValue(new Double(x+y));
try{
double y = Double.parseDouble(yField.getText());
if(y < min || y > max) {
JOptionPane.showMessageDialog(this, “invalid value for y”);
return;
}
}
catch(NumberFormatException exY){
JOptionPane.showMessageDialog(this, “invalid value for y “);
return;
}
Conclusion
This article has demonstrated how to use JFormattedTextFields for
numeric data, fixed size string data, and custom data. Using
JFormattedTextFields in place of JTextFields requires extra upfront
coding for initializing the interface, but saves code when accessing
and processing the data. JFormattedTextFields also provide a more
fool-proof interface and better guidance to the user. Coding effi-
ciency and reusability is further enhanced if custom classes based
on JFormattedTextField are created for generic data types.
M
By Ivar Jacobson and ore than 12 years have passed Language”. The criticism was exaggerated, but
Steve Cook since the Unified Modeling not unfounded. Likewise, the original leaders of
Language (UML) became a stan- the agile movement were negative to modeling.
dard (www.uml.org). During these They said “no modeling — just code.” Many peo-
years, opinions of UML have varied between ple were skeptical about the tools, so they worked
delight and distaste. In this article, we discuss the more with UML sketches on the whiteboard than
deficiencies of the current UML specification and formally with the tools themselves.
propose how to make it agile, leaner, smarter, and However, the pendulum is now swinging
more flexible — in short, how to prepare it for the back. The tools have become better. Criticism
future so that users can be confident that their from academics has mostly stopped. Agility has
investments in UML today will increase in value come to big companies and modeling is agile if
going forward. done sensibly (and not as a “silver bullet”).
At the beginning of the ’90s, there were 26 Microsoft, for instance, has implemented UML in
published methods on object-orientation, most Visual Studio 2010, alongside domain-specific
with its own notation with its own set of icons. It languages. Other important standards such as
was in this environment that UML was born. SysML are implemented as extensions to UML.
Although Grady Booch, Jim Rumbaugh, and Ivar Thus it seems that today the world looks upon
Jacobson initiated the design of what became UML with a more balanced view. UML is not the
UML, many other contributors (including Steve panacea that it was sometimes sold as 10 years
Cook) quickly joined the effort and the Object ago. Nor is it as bad as some academics, agilistas,
Management Group (OMG) launched the result and competitors have claimed. It is a practical
(www.omg.org). UML quickly made most other tool to raise the level of abstraction of software
methods — along with their own notations — from code to system level. Many organizations
obsolete. UML eventually became the standard claim benefits from their use of UML, ranging
we had hoped for, and toolbuilders and practi- from improved communication among developers
tioners rapidly adopted the new approach. to productivity gains using code generation from
Since UML first had outstanding success, we UML models.
all knew that the pendulum would swing in the UML is a good language to describe software
opposite direction some day — and we were systems at a higher level than code. Properly used
right. After a few years the setback arrived, but UML can improve productivity and quality. After
admittably for good reasons. For instance, at the several years of consolidation, the OMG (the
outset there weren’t many good UML tools. Some owner of the UML) is taking the initiative to
were very advanced, but hard to use. That disap- improve it, having issued a Request for
pointed many users and hindered the wide adop- Information (RIF) on the Future Development of
tion of UML. The language received criticism UML in 2009 under the leadership of Steve Cook.
from the academic world; for example David The results of this RFI showed that users and
Parnas nicknamed it the “Undefined Modeling vendors want UML to be leaner, more expressive,
I
always look forward to new releases in the “Head First” series due to the fact that they are based on industry standard, open
of books. The approach used in these titles remind me of high technologies like XHTML and CSS. As such, they are better opti-
school textbooks presented in a much more energized, engag- mized for text reflow and presentation on electronic reading
ing way. This updated edition of Head First C# features the devices like Sony’s E-Reader and Apple’s iPad. There is also a
.NET 4.0 framework and the Visual Studio 2010 IDE. I didn’t read more personal reason for my format preference since I worked on
the first edition, so I can’t comment on what has changed, but the the predecessor to the EPUB specification, known as the Open
material related to the latest .NET and Visual Studio features is rel- eBook Publication Structure. Unfortunately, for a book like Head
evant and well represented. First C# with its highly graphical presentation and need to main-
Head First C# follows the progression one would expect in an tain fixed positioning of these images for print-centric output,
introductory programming text, but not quite in the style “Head converting such a title to EPUB would take a considerable
First” employs. Building a Windows Form-based application con- amount of effort and cost. As such, O’Reilly only offers the title
necting to a contact database has never been presented quite this in PDF format. I used GoodReader on the iPad to read the book,
way before. Copious screenshots, sketches, hand-written notes, and while GoodReader is a super application for rendering PDF
and illustrations punctuate the material, especially with code list- and Microsoft Office-formatted documents, it still can’t compare
ings. There are even crossword puzzles concluding some of the to the less constrained reading experience that EPUB-rendering
chapters to help emphasize the concepts being taught! Some may apps like Apple’s iBooks provides. Even with the iPad’s 768x1024
find this style distracting, but I enjoyed seeing this detail present- portrait screen resolution, I had to occasionally resize, scroll,
ed in this new, information-rich way. Debugging, object-oriented reset, rinse-cycle-repeat, to adequately read the book and its
methodologies, types, references, ensue, collections, exceptions, smaller font call-outs. In the larger paper book edition, these
events and delegates are each given space to help readers get com- page design choices work. However, in the PDF, they are a big
fortable with how C# uses these aspects and why they are so pow- pain. Consequently, I would advise those interested in reading
erful. The 15th and final chapter on LINQ is one of the best I’ve this book to obtain the print edition rather than struggle through
seen in terms of helping readers grasp as quickly as possible the the electronic formatted version. Another bone of contention
power of this remarkable and potent technology. regarding the PDF is that O’Reilly resampled the images (except
Three lab exercises (“A Day at the Races,” “The Quest,” and for the first two pages — the cover) for black and white reproduc-
“Invaders”) help readers assemble real-world applications leverag- tion only. This makes sense for the print version to keep costs
ing what was learned in the sections preceding these labs. They do low, but the electronic edition could have greatly benefited from
a super job of showing how the foundational aspects of what C# the color imagery, especially with color-highlighted code listings,
provides can be put into gratifying use. application screen shots, and even the call-out photos. With the
Coauthors Andrew Stellman and Jennifer Greene effectively use cost of the PDF edition only a few dollars less than the print ver-
a potpourri of images, callouts, gags, and highlights to maintain sion, there are really no other advantages to the electronic edition
readers’ interest and understanding of the topics covered in the of the book.
book. While these may be too distracting to more serious readers, I My electronic publishing rant aside, the content of Head First
found them helpful and, yes, a bit corny at times. But they kept my C# is solid and entertaining. Seasoned developers need not apply,
attention and helped reinforce the less obvious aspects of C# in as the “Head First” series best caters to the younger, less experi-
action. For those seeking a more serious but very effective large enced crowd who are interested in learning new technologies in a
trim size book, I recommend holding out for Mike Murach & fun, approachable way.
Associates treatment on the new release, as I found their C# 2008
book an excellent, straight-talking guide to learning the language.
Like most new book titles I review these days, I read the elec-
tronic edition of Head First C#. I prefer to read EPUB versions Retur n to Table of Contents
A
by Jonathan Erickson s executive director of the CodePlex Dr. Dobb’s: To date, has the
Foundation (www.codeplex.org/), Foundation enjoyed any “big
Paula Hunter has a lot of irons in the wins?”
fire. She recently took some time to Hunter: We launched in
chat with Jonathan Erickson. September 2009, and I think
our biggest win is what we’ve
Dr. Dobb’s: Paula, can you give us a brief accomplished in such a short
overview of the CodePlex Foundation? time. One of those first early milestones was the
Hunter: The CodePlex Foundation is a new, publication of our Project Acceptance and
independent open source foundation that Operations Guidelines where we mapped out our
enables corporations to innovate with more mission and guidelines for submitting a project to
agility by collaborating with open source com- the Foundation. We introduced a “Project
munities. Our core mission is to provide gover- Gallery” model as an organizational structure for
nance and legal mechanisms necessary to ensure managing Foundation projects. The museum
clarity and certainty for corporations that want metaphor provides a way to group projects with-
to participate in open source. We also develop in a taxonomy, similar to how art collections are
and promote best practices, processes, and legal organized and presented in a museum exhibition.
structures that meet the needs of open source For example, sponsors can host entire galleries of
developers. projects with related applications and technolo-
We’re platform, technology, and license agnos- gies. Introducing these guidelines has helped us
tic — which is how we differ from other founda- attract interesting projects and project sponsors,
tions. We made a conscious effort to differentiate and we’re picking up momentum.
ourselves in this way. This allows us to provide So far, we have the ASP.NET Open Source
unique support and services that complement the Gallery that has four active projects. The Web
important work of other foundations. Forms MVP project is the most recent addition to
We formed to encourage greater use of OSS this gallery. We also have the Systems
by corporations and developers by providing Infrastructure and Integration Gallery, consisting
support and services to achieve the following of two active projects with more on the way.
objectives: We’ll continue growing these galleries and
adding new ones along the way. That’s the beauty
• Make it easy to participate in open source of it. The community can have a strong influence
• Provide a standard, but flexible contribution
agreement on the types of projects and applications we sup-
• Create a legal entity/framework that enables port simply by submitting to sponsor a gallery.
code to be shared easily with reduced risk
• Lower legal risk and costs of bringing open Dr. Dobb’s: What will the Foundation be like two
source projects into the commercial world years from now?
• Reduce the time, legal complexity, and
expense of contributing projects to the open Hunter: More projects, more galleries, more
source community sponsors, more community, more impact, more
independence. I’ve set a goal for 2010 of adding at least two spon-
sors, at least two more galleries, and at least one additional board
member. In two years I plan to have more sponsors — which will
prove our independence for those who are still skeptics — and a
tiered sponsorship model. I’m working to attain 501.6c status as a
non-profit organization, and looking at different models under
which we can accept members at different levels of participation
and contribution. The goal is to diversify the organization, increas-
ing our footprint, influence, and credibility in the OSS community,
and demonstrating our value to commercial software companies,
non-software companies that use OSS and enterprise IT.
A
By Avo Reid pple wants its users to have an opti- technologies that are in ascendancy. If we suc-
mal experience using its devices. ceed, people will buy them and if we don’t they
That’s what attracted people to the won’t.”
Macintosh and created the cult-like In his now famous “Thoughts on Flash”
following that defined Mac Users and PC Users. memo, (http://www.apple.com/hotnews/thoughts-
But it is interesting that not until Apple on-flash/), Jobs writes, “Flash was created during
made the Intel Transition (for details, see the PC era -– for PCs and mice. Flash is a success-
http://en.wikipedia.org/wiki/Apple%E2%80%9 ful business for Adobe, and we can understand
3Intel_transition), changing the CPU of why they want to push it beyond PCs. But the
Macintosh computers from PowerPC processors mobile era is about low power devices, touch
to Intel x86 processors, in 2006–2007, that interfaces and open web standards -– all areas
sales really took off, especially for laptop sales. where Flash falls short.”
The gate to the walled garden was opened to all But what’s most interesting about the memo is
who might wander in to take a gander. the definition of ‘openness’. Steve Jobs writes that
Arguably, many of these new users were like “Adobe’s Flash products are 100% proprietary.
me, intrigued by Apple products, functionality, They are only available from Adobe, and Adobe
and usability but hesitant to commit because of has sole authority as to their future enhancement,
the high price to pay in terms of leaving behind pricing, etc. While Adobe’s Flash products are
the years of investment and familiarity in widely available, this does not mean they are
Windows. open, since they are controlled entirely by Adobe
What sealed the deal for me was that the Mac and available only from Adobe. By almost any def-
now appeared to be more open, at least a little inition, Flash is a closed system.”
more open than before. Now I could install He goes on to admit that “Apple has many pro-
VMWare Fusion and have access to Windows and prietary products too. Though the operating sys-
all the software I use for business, but still be able tem for the iPhone, iPod, and iPad is proprietary,
to use the Mac OS X and all of its software. I was we strongly believe that all standards pertaining to
a new customer and I have to say it will take a lot the Web should be open. Rather than use Flash,
to get me to switch back. Apple has adopted HTML5, CSS, and JavaScript -
So why does Apple act so guarded against any- – all open standards.” We might also point out
one entering its Walled Garden? Is it really so here that there is currently only one way to devel-
that the user will have an optimal experience? I op and publish an iPhone Application today, and
have to say I probably would not have become a that is to use Apple products.
user unless they opened the gate the way they did John Warnock and Chuck Geschke in their
in 2006, intentionally or not. rebuttal “Our Thoughts on Open Markets”
Recently, Apple has been making the case that (http://www.adobe.com/choice/openmarkets.html)
Adobe Flash is an inferior, evolutionary ‘dead- state, “If the web fragments into closed systems,
end’ technology that won’t be allowed entrance if companies put content and applications behind
into the Walled Garden; i.e., Flash Videos and walls, some indeed may thrive — but their suc-
Flex/Flash Applications will never run on an cess will come at the expense of the very creativ-
iPhone or iPad. As Steve Jobs stated at the 2010 ity and innovation that has made the Internet a
D8 Conference (http://allthingsd.com/d/), “We revolutionary force.”
don’t think Flash makes a great product, so we’re They continue to argue for the consumer, “We
leaving it out…Instead, we’re going to focus on believe that consumers should be able to freely
R
By Jake Sorofman ecently, I had a conversation with a • Liquidity — or the absence thereof.
well-known industry analyst — a very Traditional pricing models have forced enter-
prises to engage in onerous, long-term com-
creative guy drawn to big ideas. He got mitments, removing the liquidity that drives
me thinking about a big idea that has market-efficient pricing.
been rattling around in my brain to some degree of
distraction. In my estimation, the idea is nothing But this is changing — and fast:
short of transformational to the economics of
enterprise computing. • Efficiency. Emerging approaches to automa-
tion make system scale and complexity far less
It’s a story about taking back control of IT and prohibitive and intimidating for enterprise IT
truly rationalizing costs. The story has two parts: organizations to handle in-house.
• Switching costs. Deep automation also allows
• Part 1: Returning control and flexibility to IT you to regenerate systems that are ready to
by making system workloads autonomous, deploy to new environments, setting work-
self-healing, and portable across deployment loads free to run anywhere.
environments. • Liquidity. At the same time, elastic computing
• Part 2: Allowing IT leadership to make dynam- models allow you to pay as you go. Systems
ic decisions about where workloads should can be moved and capacity can be consumed
run based on optimizations for price and serv- on demand.
ice levels. Think of this as the logical evolution
of IT financial management.
IT efficiency increases, switching costs
In combination, these two concepts have the decrease, liquidity emerges, and a true market is
potential to change everything. born. Then, introduce decision support for driving
IT has long been beholden to outsourced serv- price and SLA optimizations and CIOs can dra-
ice providers for managing systems. In some cases, matically improve the efficiency of IT spending.
the comparative cost of outsourcing system man- As context for what is possible, consider the
agement is lower due to scale economies and other achievements we’ve seen in modern manufactur-
built-in efficiencies. But, more often than not, it’s ing. Supply and capacity are dynamically shifted
the predictability of a contract relationship that IT to the highest-yield demand. Capacity is allocated
leadership covets: Fixed bid contracts are easily based on contract values, delivery commitments,
forecast and service-level agreements allow IT to and the on-hand availability of raw materials. Raw
share some risk. materials are procured based on competitive bids.
But the cost of admission is exceptionally high. Production runs are scheduled against available
For a very large company a contract may exceed capacity.
$40 or $50 million — a year. The entire manufacturing process is automated
Why have CIOs been willing to spend this and dynamically optimized. Why shouldn’t this be
much? the inspiration for the future of enterprise IT?
• Efficiency. Traditionally, it has been more effi- — Jake Sorofman works for rPath, a provider of
cient to throw systems “over the wall” due to
service providers’ comparative cost advantage automated application deployment tools. You can
through specialization. contact Jake at jsorofman@rpath.com.
• Switching costs. Once your systems are
deployed, it has been too expensive to move
them to a new provider; you’re locked in — Return to Table of Contents
and so is your price.
A
By Herb Sutter s described in “Use Threads Correctly This article will show how to implement the
= Isolation + Asynchronous Messages” pattern, including a reusable helper to automate
(www.drdobbs.com/high-perform- the common parts, in any of the popular main-
ance-computing/215900465 [1]), to use stream languages and threading environments,
threads well we want to follow these best practices: including C++, C#/.NET, Java, and C/Pthreads. We
will go beyond the basic pattern and also tie the
• Keep data isolated, private to a thread where active object’s private thread lifetime directly to the
possible. Note that this doesn’t mean using a lifetime of the active object itself, which will let us
special facility like thread local storage; it just
means not sharing the thread’s private data by leverage rich object lifetime semantics already
exposing pointers or references to it. built into our various languages. Most of the sam-
• Communicate among threads via asynchro- ple code will be shown in C++, but can be directly
nous messages. Using asynchronous mes- translated into any of the other languages (for
sages lets the threads keep running independ- example, destructors in C++ would just be
ently by default unless they really must wait
for a result. expressed as disposers in C# and Java, local object
• Organize each thread’s work around a mes- lifetime in C++ would be expressed with the using
sage pump. Most threads should spend their statement in C# or the “finally dispose” coding
lifetime responding to incoming messages, so idiom in Java).
their mainline should consist of a message
pump that dispatches those messages to the
message handlers. Active Objects:
The Caller’s View
First, let’s look at the basics of how an active
Using raw threads directly is trouble for a object is designed to work.
number of reasons, particularly because raw An active object owns its own private thread,
threads let us do anything and offer no help or and runs all of its work on that private thread.
automation for these best practices. So how can Like any object, an active object has private data
we automate them? and methods that can be invoked by callers. The
A good answer is to apply and automate the difference is that when a caller invokes a method,
Active Object pattern [2]. Active objects dramati- the method call just enqueues a message to the
cally improve our ability to reason about our active object and returns to the caller immediate-
thread’s code and operation by giving us higher- ly; method calls on an active object are always
level abstractions and idioms that raise the nonblocking asynchronous messages. The active
semantic level of our program and let us express object’s private thread mainline is a message pump
our intent more directly. As with all good pat- that dequeues and executes the messages one at a
terns, we also get better vocabulary to talk about time on the private thread. Because the messages
our design. Note that active objects aren’t a novel- are processed sequentially, they are atomic with
ty: UML and various libraries have provided sup- respect to each other. And because the object’s pri-
port for active classes. Some actor-based lan- vate data is only accessed from the private thread,
guages already have variations of this pattern we don’t need to take a mutex lock or perform
baked into the language itself; but fortunately, we other synchronization on the private shared state.
aren’t limited to using only such languages to get Here is an example of the concurrency seman-
the benefits of active objects. tics we want to achieve for calling code:
already waiting in the queue, then joins with the private thread to
wait for it to drain the queue and end. Note that the destructor/dis- That’s what we want to enable. Next, let’s consider how to actu-
poser is the only call on the active object that is a blocking call ally write a class with objects that are active and behave in this way.
from the viewpoint of the caller.
Expressing thread/task lifetimes as object lifetimes in this way An Active Helper
lets us exploit existing rich language semantics to express bound- The first thing we want to do is to automate the common parts
ed nested work. For example, in C# or Java, it lets us exploit the and write them in one reusable place, so that we don’t have to
usual language support and/or programming idiom for stack-based write them by hand every time for each new active class. To do
local lifetimes by writing a using block or the Dispose pattern: that, let’s introduce a helper class Active that encapsulates a
// C# example thread behind a messaging interface and provides the basic mes-
using( Active a = new Active() ) { // creates private thread sage-sending and automatic pumping facilities. This section
…
a.SomeWork(); // enqueues work shows a straightforward object-oriented implementation that can
…
a.MoreWork(); // enqueues work be implemented as shown in any of our major languages (even in
…
} // waits for work to complete and joins with private thread
C, where the class would be written as a struct and the object’s
methods would be written as plain functions with an explicit
As another example, in C++ we can also express that a class opaque “this” pointer). After that, we’ll narrow our focus to C++
data member is held by value rather than by pointer or reference, specifically and look at another version we can write and use even
and the language guarantees that the class member’s lifetime is tied more simply in that language.
to the enclosing object’s lifetime: The member is constructed when The Active helper below provides the private thread and a mes-
the enclosing object is constructed, and destroyed when the sage queue, as well as a sentinel done message that it will use to
enclosing object is destroyed. (The same can be done by hand in signal the private thread to finish. It also defines a nested Message
C# or Java by manually wiring up the outer object’s dispose func- class that will serve as the base of all messages that can be
tion to call the inner one’s.) For example: enqueued:
A C++0x Version
The aforementioned OO-style helper works well in any main- Next, we can use the lambda functions language feature to
stream language, but in any given language we can often make it make an implementing class like Backgrounder even simpler to
even easier by using local language features and idioms. For exam- write, because we don’t even have to write the external “launcher”
ple, if we were writing it in C++, we could observe that Message method and the actual body in different places…we can simply
and its derived classes are simply applying the usual “OO way” of write each method the same way we write it naturally, and send the
rolling your own function objects (or functors), and Execute could body of the message as a lambda function:
as well be spelled operator(), the function call operator. The only class Backgrounder {
public:
reason for the Message base class is to provide a way to hold and void Save( string filename ) { a.Send( [=] {
later invoke an arbitrary message, whereas in C++0x we already …
} ); }
have std::function<> as a handy way to hold and later invoke any
void Print( Data& data ) { a.Send( [=, &data] {
suitable callable function or functor. …
So let’s leverage the convenience of C++ function objects. We’ll } ); }
avoid a lot of the “OO Message hierarchy” boilerplate. Active will private:
PrivateData somePrivateStateAcrossCalls;
be a simpler class. Derived classes will be easier to write. What’s Active a;
not to like? };