java.util.concurrent backport to Java 1.4
When I blogged in September about using the Java threading classes introduced in Java 1.5, I didn’t know a similar library was available in Java 1.4. It is, courtesy of the backport-util-concurrent package maintained by Dawid Kurzyniec at Emory University.
The package provides versions of most of the
java.util.concurrent
classes,
but converted to Java 1.4.
The key feature missing is the ability to use
generics.
For example,
using Java 1.4,
you can’t define a Callable
anonymous inner class using syntax like:
void showSearch(final String target) throws InterruptedException { Future<String> future = executor.submit(new Callable<String>() { public String call() { return searcher.search(target); } }); displayOtherThings(); // do other things while searching try { displayText(future.get()); // use future } catch (ExecutionException ex) { cleanup(); return; } }
to specify the call
method returns a string,
as in the above code excerpted from the Java 1.5 API for the Future
interface.
And,
of course,
you can’t define a Future
of type string
that would be returned from the
ExecutorService
‘s submit
method,
as shown above.
Without generics,
you’re confined to returning Object
s and casting them
to the expected type.
You don’t get the compile-time validity checking provided by Java 1.5 generics,
but most Java developers are used to using narrowing casts like this.
This backport of the concurrent package came in handy this week when I wanted to use its features on a project that had to run in Java 1.4. It seems a lot of code from Java 1.5 has been backported to earlier Java versions, like Java annotations. Thank you, Dawid and Emory, and thank you for releasing the code into the public domain so the code may be “may be freely reproduced, distributed, transmitted, used, modified, built upon, or otherwise exploited by anyone for any purpose, commercial or non-commercial.”
UPDATE 7 hours later:
I should mention that some of the code for the backport,
as well as much of Java 1.5’s java.util.concurrent
package,
came from Doug Lea’s
util.concurrent
package.
I woke up this morning and realized I had neglected to mention
where the initial ideas for the Java 1.5 concurrency classes came from.
Professor Lea,
from the State University of New York at Oswego,
created most of the ideas and code behind the improved multi-threading techniques
that made it into Java 1.5.
He also is a co-author of
Java Concurrency in Practice,
which Stuart Halloway recommended in my September 27 blog
as being a great source to learn the effective use of the Java 1.5 concurrency package
(and thus the backported version).
The benefit of favoring the Emory University backport over Doug Lea’s
original code is that code using the backport allows a smooth
transition to Java 1.5.
The transition mostly would involve renaming your import
s.
The backport’s class names and APIs are identical to what’s in Java 1.5.
Doug Lea favors using the Emory backport over his older util.concurrent
package.