Tom's Blog
Still using StringBuffer? That's sooo Java 1.4
Published by Tom |
July 17, 2007 06:53 AM EDT |
Pop quiz:
Hashtable is to HashMap as StringBuffer is to ...
<fill in the blank>
Answer: StringBuilder.
I recently worked on a Java project where the target environment was Java 1.5. Although Java 1.5 has been out for almost three years, the client was just upgrading to it to take advantage of its language features and APIs.
While working on the project, I noticed most developers continued to use the StringBuffer class when StringBuilder would have been the better choice. In asking around, most developers said they were unaware of StringBuilder.
In case you're using Java 1.5 or 1.6 but not yet using StringBuilder, StringBuilder is an unsynchronized version of the tried-and-true StringBuffer class. Most of StringBuffer's public methods are synchronized to allow multiple threads to read and modify the string simultaneously. But since StringBuffer is almost always used to build up a string within a method, or to build a string over several method calls within a single-threaded environment, the synchronized nature of StringBuffer is overkill. An article in Dr. Dobb's Journal in June 2006 estimated switching from StringBuffer to StringBuilder could speed string building by 38%.
That's why Sun added StringBuilder to the language in JDK 5. None of StringBuilder's methods is synchronized, so the class is not meant to be used when multiple threads need to access the string. In multi-threaded contexts, you will want to use StringBuffer. But consider your own code. How many times have you needed to share a StringBuffer between multiple threads? You'll probably find that StringBuilder is often the better choice.
Tuesday July 17, 2007 Permalink
Comments [6]
Independence Day in D.C.
Published by Tom |
July 05, 2007 10:55 PM EDT |
Yesterday saw another great celebration on the National Mall in Washington
of our nation's declared independence.
Two hundred thirty-one years ago,
the Continental Congress adopted Thomas Jefferson's
draft of the
Declaration
of Independence.
"Thomas Jefferson" looks on as "Benjamin Franklin" reads the Declaration of
Independence on the National Mall in Washington, D.C.
This year, they brought a veteran of World War II to read the last part of the Declaration, and filmmaker Ken Burns talked about his upcoming World War II documentary, The War, which recounts the war from soldiers who fought it. I heard no mention of any active war going on, or of any of the men and women fighting in it. Iraq already seems like a war we're fighting to forget.
Rockets red glare light up the boats on the Potomac River during the
fireworks finale.
At around 5 p.m., a lightning storm prompted police to evacuate the open areas of the Mall and the Marine Corps Memorial. Officers asked picnickers and others staking out seats for the concert and fireworks to seek shelter in the various museums and memorials. The storm passed through after about an hour, and the 8 p.m. concert at the Capitol began on time, as did the fireworks an hour later. Last year we watched the fireworks from the Lincoln Memorial. This year, we were able to enjoy the view from our home in Rosslyn.
The fireworks show was great, as usual, but this year I thought it was marred a bit by two orbiting police helicopters, one to the east of the Mall and one to the west. Security was visibly tighter this year, the terror tenor of our times.
And to put another damper on an otherwise perfect evening, three men who put on the fireworks display were hurt and burned, one seriously, when unexploded fireworks went off about 15 minutes after the finale. I was still looking toward the Lincoln Memorial and saw two or three fireworks explode at ground level. May the injured fireworkers recover fully.
Thursday July 05, 2007 Permalink
Eclipse 3.2 JUnit runner gets confused connecting to server?
Published by Tom |
June 26, 2007 05:24 PM EDT |
I opened an Eclipse project today,
ran a unit test,
and got a socket exception I'd never seen before.
The project was one I had set aside a few weeks ago
after playing with the NetBeans 6 preview release.
After opening the project in Eclipse, I went straight to one of the JUnit test classes, made a small tweak to one of the test methods, then hit my usual Alt-Shift-X + T keyboard shortcut to run the test case with JUnit. Instead of seeing a green or red bar, Eclipse just sat there staring at me, saying it was running the test class with JUnit. The console view showed the red "terminate" button in bright red, indicating the run was proceeding, albeit at an exceedingly slow pace. After about 30 seconds, the console displayed:
Could not connect to: : 3393 java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:519) at java.net.Socket.connect(Socket.java:469) at java.net.Socket.A socket connection error? I was just trying to run a local JUnit test, not connect with any remote server.(Socket.java:366) at java.net.Socket. (Socket.java:179) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.connect(RemoteTestRunner.java:560) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:377) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
My first theory was I must have been playing with remote debugging for this application a few weeks ago and configured Eclipse to connect with a remote JVM. I spent a minute going through the Eclipse configuration for the JUnit test to check out its settings. I saw nothing set for any remote JUnit connection. (I'm not even sure Eclipse's JUnit runner can do that.) Everything looked right, so I ran the test again and got the same connection refused exception.
My second theory was that I hadn't rebuilt the application since upgrading to JSE 1.6.0_01 from 1.6.0, and that Eclipse was doing its best to find a running 1.6.0 JVM to connect with. (This seemed far-fetched, but a rebuild only took a couple of seconds.) A rebuild didn't solve the problem.
My third theory was I had been using NetBeans for so long I must have forgotten how to run the JUnit test in Eclipse. Perhaps I was telling Eclipse to debug a remote application instead of running JUnit. I ran the test again, this time through the menu option. No luck.
That sent me searching the web for the solution. I found it pretty quickly, but not the underlying reason behind the problem.
The solution was to restart Eclipse. Why this worked I don't know, since I had just launched Eclipse minutes before. Apparently the JUnit runner thread in Eclipse attaches to an Eclipse server thread to run the tests. It would seem the client thread was trying to connect to the wrong port (3393) or that the server thread that had been listening on port 3393 for runtime requests failed. Either way, I would have expected Eclipse to log the error. Strangely, the only item in the Eclipse error log said:
Warnings while parsing the commands from the 'org.eclipse.ui.commands' and 'org.eclipse.ui.actionDefinitions' extension points.with a sub-message saying:
Commands should really have a category: plug-in='org.codehaus.groovy.eclipse', id='org.codehaus.groovy.eclipse.debug.ui.testShortcut.debug', categoryId='org.eclipse.debug.ui.category.debug'Well, I did recently install the Groovy plugin. Did that cause the problem? If so, Eclipse thinks not being able to connect with the JUnit runtime is just a warning?
Anyone have the real answer as to what caused Eclipse to get so confused while trying to launch the JUnit runner? None of the web pages I viewed talking about the problem mentioned the cause for the failure.
Tuesday June 26, 2007 Permalink
IBM Strikes Out in Second Life
Published by Tom |
June 22, 2007 11:20 AM EDT |
I left the real world yesterday to "attend" a technical
briefing
in
Second Life,
hosted by IBM,
on what Web 2.0 means for business.
I want to congratulate IBM for experimenting with virtual worlds.
But in this case,
the pretend physical nature of the online briefing detracted from the message
and added nothing discernable.
I spent more time fighting the Second Life client application
than I did listening to the IBM presenters.
My generically clad avatar looks at the right side of the virtual stage during
IBM's technical briefing.
Holding a meeting in a 3D virtual world promises new tools for collaboration. You could hold a main meeting, break out into smaller groups as needed while still easily rejoining the main group, share notes, share software, demo software on a virtual computer in the virtual world, and draw on whiteboards that can be stretched to fit your needs, using colored pens that never run dry. I'm unsure what capabilities Second Life provides today to do any of these things, but I don't think talking is one of them. Attendees to the IBM session had to dial a regular conference telephone line to hear the presenters.
I say IBM struck out by holding this technical briefing in Second Life because the presenters merely talked, showed slides, and provided handouts. You don't need a 3D virtual world to do these things. The bad part was Second Life detracted from the actual content of the briefing by having to deal with virtual-world activities instead of merely listening, reading and thinking.
First, I had trouble finding the conference room. The coordinates IBM provided took me to what looked like a sand-dune filled desert with a beautiful virtual sunset on the horizon. The only other thing I could see was one or two other virtual attendees walking around aimlessly. Flapping my arms eventually got me there. You see, the presentation was held on a platform floating in space above the ground. You had to fly up several meters to see it. (In Second Life you can fly.) Strike one.
Second, the Second Life client isn't very stable. It froze and crashed while I was trying to move around. Strike two.
Here is me trying to view the slides in Second Life. I captured this screenshot when
the slide was in focus.
Fourth, Second Life forces you to create a new name for yourself while visiting. You can choose a first name, but you have to choose from a list of Second Life family names. As a result, you can't tell who the IBM speaker at the podium is without someone translating that "Foobar Frobney" (or whatever) is really IBM employee Alfredo Gutierrez. Strike, um, three and a half.
Even though Second Life's virtual-world wasn't the best forum for this technical briefing, I want to give IBM credit for trying. Virtual reality holds promise for providing better, more natural tools for online collaboration than simple slideshows and telephone conference lines. However, IBM will need to learn to use the best tool for the job. If you are just going to talk and show slides, there are more effective technologies today than Second Life.
Friday June 22, 2007 Permalink
Comments [1]
Sun Tech Days in D.C. a Mini JavaOne
Published by Tom |
June 11, 2007 08:43 AM EDT |
I spent last Thursday at the
International Trade Center
in Washington attending Sun
Tech Days
2007,
the last stop in a 15-city world technology tour
showcasing what's new in Java and Solaris.
Here are some highlights of the day,
and notes from the keynote address by
Sun Microsystems CEO Jonathan Schwartz.
Overall,
the day was like a mini JavaOne:
exposure to new technologies without actually teaching you how to use them.
With most technical sessions lasting just 50 minutes,
exposure is about all one can expect.
Session highlights
- Sun considers
GlassFish
a production-ready JEE 5 application server.
I hadn't been following the GlassFish project, so it was good to learn about its relative maturity. GlassFish V2 (in beta) adds clustering support. - Java 6 added features to JMX to make managed-bean development easier
using annotations.
I learned this in an aside during a JMX talk that focused on JMX features in Java 5. Yes, Java 5 has been out for almost three years, but Sun treats it like new technology because most companies and developers haven't migrated from 1.3 or 1.4. And it was good to hear Sun advocating and explaining JMX because developers could benefit from using its instrumentation and monitoring features in their applications, but the JMX learning curve has always been steep. - jMaki
tries to simplify Ajax development by unifying the APIs of popular Ajax libraries.
jMaki provides JSP tags that help you call Ajax components from other Ajax frameworks, such as Dojo, Script.aculo.us, Yahoo UI Widgets, and Google's Ajax framework. - Sun is focusing more on JRuby than Groovy because of Rails.
This isn't actually new, but it was refreshing to hear a Sun engineer acknowledge that Sun's newfound excitement over JRuby is based on Rail's current sexiness quotient, and that attention to other JVM languages like Groovy likely will suffer a little as a result. - Web 2.0 is still a vague concept.
After I left the session on "AJAX and Web 2.0 Frameworks," two attendees both mentioned that they still had no idea what Web 2.0 means. The speaker never once defined it, and she left no time at the end of her presentation for questions. - Sun engineer evangelists can get pretty tired after a 15-city world tour.
One engineer played music from his laptop, drowning out part of another engineer's talk, and joked that audience members should use the corners of the room to relieve themselves. Another engineer went through his slides with the enthusiasm of a cow chewing cud, saying things like "as you can see in the code here" while the actual code was hidden because his NetBeans display had only about 40% of the real-estate showing the code window, and he was too tired to open the window or scroll the code to the right.
Jonathan Schwartz's keynote address
Someone who didn't lack enthusiasm was CEO and President Jonathan Schwartz. He started the morning with a keynote address giving his vision for Sun's future. Schwartz is a thought-provoking speaker. His confidence provides a sense that you want to believe in his vision for success. As much as I think some of Sun's products are pretty cool, I still wonder whether Sun will succeed in differentiating its products sufficiently to win over the market.
Sun Microsystems CEO Jonathan Schwartz
speaks at Sun Tech Days 2007 on June 7
in Washington, D.C.
Schwartz said the growth of Internet access will come mostly from consumers using mobile devices. More of the world uses a phone to access the Internet than a PC, he said. The United States is an anomaly in so many people owning expensive computers, he said. If you're going to meet the world's demand for mobile online access, "you're going to need to figure out how to work with that."
One way Sun is going to work with the growth of mobile Internet devices will be to develop a new mobile-phone software platform called JavaFX. JavaFX, which includes JavaFX Mobile, is intended to make developing mobile phone software easier, and will run existing Java ME applications. Sun announced JavaFX at JavaOne last month, and released JavaFX Script, a new declarative scripting language to build "rich content" applications. Schwartz said Sun will release JavaFX as free open source software. The license will be partly restrictive. The JavaFX website says handset manufacturers will need to purchase an OEM license to embed JavaFX on their devices. Perhaps JavaFX will solve some of the difficulties developing mobile applications for Java ME, where every application has to be customized for nearly every possible device.
Schwartz spoke at some length of Sun's commitment to open source software. He said Sun spent $500 million to release Solaris as open source, both in staff time and the cost of procuring intellectual property. Sun also provides a free open source Java development kit, a free IDE (NetBeans), a free JEE server (GlassFish), and other open source projects that must cost Sun a large amount of money.
"Unfortunately the most important
audience we have to serve has
no money and spends no money.
It's you. And we love you."
--Jonathan Schwartz
Sun's large investment in the free, open source software business is still one
area I haven't figured out.
My question has always been will Sun ever generate enough goodwill or product synergies
to sell hardware to go along with that free software?
Schwartz seemed to acknowledge the difficulty in transferring
the gift of software into sales of profitable hardware:
"Unfortunately the most important audience we have to serve has no money and spends no money,"
Schwartz said.
"It's you [the developer]," he said with a laugh,
"and we love you."
audience we have to serve has
no money and spends no money.
It's you. And we love you."
--Jonathan Schwartz
Will the love from the developer community transform into money? For example, Schwartz said that 70% of Sun's free Solaris operating system is installed on non-Sun hardware, like Dells and HPs. He said Solaris's exposure beyond Sun hardware opens the door to new customers. But do companies or developers installing Solaris for free on an x86 platform ever end up buying Sun blade servers, Sun disk arrays or Sun tape storage?
Jonathan Schwartz at Sun Tech Days
Although he acknowledged Sun makes its payroll selling commodity products attractive to the "slower" market segments, the growth (and profit) is in the faster segments. "If you're in our business," he said, "you don't want to hang out too long at the bottom."
For innovative products, he mentioned Sun's focus on power-efficient hardware, its new Sun Fire X4500 storage server with 24 terabytes capacity at less than $2 per gigabyte, and its portable, self-contained data center that fits inside a standard shipping container (Project Blackbox). The primary costs in running a data center today, he said, are people to run them, real estate, and electricity, in that order. Sun is therefore focusing on products that reduce the need for human intervention, fit in a smaller area, and consume less power.
Will the focus on high-margin, innovative hardware help Sun succeed? As a longtime Java developer, I have a special warm feeling for Sun. Sun gave us the Java platform. Sun donated the popular Tomcat web application server to open source. And Sun's recent attention back to Java developers has been heartening, with its major improvements to NetBeans, its development of GlassFish, and Java's new dynamic/scripting-language support, including the development of Ruby as a first-class JVM language. Yet most of what I heard from Schwartz on Thursday was that giving us all this software for free costs a lot of money, and Sun's focus needs to be on selling innovative hardware. What I didn't hear Schwartz explain was why Sun is focusing on free, open source software for developers -- how it helps Sun's bottom line -- and thus a strong feeling that the support will continue.
Monday June 11, 2007 Permalink


