Threading Java

Java threads have been through many transitions. As a result, the guidelines for writing multi-threaded applications are always changing. Here are my three basic pre-Java 5 rules for working with threads:
1. Never extend the Thread class. It’s surprising to me just how many Java programmers still do this, and articles encourage it.
2. Always keep a handle on the threads you create and name them. This important for when you want to gracefully shutdown your multi-threaded applications.
3. Package the task you want to multi-thread into a class, have that class implement Runnable, and pass an instance of it to a thread for processing.
In Java 5 will we see that rules one and two are redundant, and rule three is important. Let’s hope you’ve implementing Runnables!
The Java 5 way
One of my favourite packages in the Java 5 API is java.util.concurrent. Since its arrival, I no longer need to create threads or thread pools. Instead, the focus is on packaging tasks as Runnables – rule three. Also, if I want to schedule 10,000 emails to be sent tomorrow morning at 7am, they can now be sent in parallel. Previously, I’d have used java.util.timer for scheduling the emails, but these would have to be processed sequentially owing to single-threaded design of the timer service. Java 5 gives us more toys to play with. Take a look at the following code snippet:
ExecutorService executor =
Executors.newFixedThreadPool(2);
executor.execute(runnable1);
executor.execute(runnable2);
executor.shutdown();
Executors is a factory class for creating ExecutorService(s). In this case, the ExecutorService executor is a thread pool that has been configured with two worker threads. With our pre-configured runnables (runnable1 and runnable2), all we need to do is pass each of them to the executor’s execute method to be processed in parallel using the specified number of worker threads.
To wrap it up, the executor.shutdown() method is called to initiate an orderly shutdown of all worker threads. All previously submitted runnables are allowed to complete but no new ones can be added.
ScheduledExecutorService se = Executors.newScheduledThreadPool(10);
se.schedule(runnable1, 5, TimeUnit.SECONDS);
se.schedule(runnable2, 2000, TimeUnit.NANOSECONDS);
This is a brief overview of how to schedule runnables to execute at some point in the future within the thread pool. Also note that with Java 5, we can now go down to even nano-seconds! We’ve only just scratched the surface of the concurrency API within Java 5. But what we have learned is that you don’t need to create your own threads and provider yet another thread pool implementation. I recommend taking a closer look around the java.util.concurrent package, because it is one of those parts of the Java 5 API that just doesn’t get the press it duly deserves.
AspectJ 5 Final
In other developments, Aspect Oriented Programming (AOP) is a programming methodology that works alongside Object-Oriented Programming (OOP). OOP focuses upon separating and encapsulating concerns into packages (such as Payroll), classes (such as Account) and methods (such as deposit and withdrawal). Concerns such as logging and error handling can’t be cleanly encapsulated in such a fashion because they ‘crosscut’ (or bleed) into every part of the system. AOP strives to remove such crosscutting concerns, leaving OOP to focus upon what it does best: creating clean, modular and encapsulated programming entities.
AspectJ 5 Final is a major step forward for the AOP community. It signifies the successful merger of the two leading AOP Open Source Software Vendors AspectJ and AspectWerkz. The result is a standardized language and toolkit for AOP built on top of the Eclipse 3 Platform. The AspectJ Development Toolkit (AJDT) 1.3 was released on 20th December, 2005 for the Eclipse 3.1. This includes support for Java 5 and the annotations style support for aspects. This enables Eclipse Java programmers with direct development support for AOP using Java 5 off the new unified AspectJ platform.


