Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Build Java Apps that Can Multitask

The java.util.concurrent package in J2SE 5.0 provides a new thread framework for handling many of the low-level details associated with creating, executing, and managing threads. Take a closer look at its important features.


advertisement
f you worked with thread programming in C or C++ or prior versions of Java, you know what a headache managing the threads in your code can be. In single-threaded programs, a bug in the code causes an application to fail at the same point each and every time. In multithreaded programs, however, the failure occurs only when certain conditions are met. Because predicting all the conditions that can cause application failure is so difficult, thread programming is challenging. Some programmers avoid the challenge all together, while others—savvy problem-solvers—stay glued to their computers until they get it right.

The J2SE 5.0 platform includes a new package of concurrency utilities. The classes in this package are building blocks for concurrent classes or applications used in concurrent designs. The concurrency utilities include the following:
  • A high-performance, flexible thread pool
  • A framework for asynchronous execution of tasks
  • A host of collection classes optimized for concurrent access
This article introduces the J2SE 5.0 framework classes and their important features. The accompanying downloadable code provides simple and easy-to-use examples that demonstrate all the new thread framework classes. Running the examples while you read the article gives you a better understanding of each feature.

Executor Framework
The Executor framework provides simple standardized extensible classes that offer useful functionality that otherwise is tedious or difficult to implement manually. This framework standardizes invocation, scheduling, and execution. It provides support for controlling asynchronous tasks according to a set of execution policies.

The Executor interface executes submitted runnable tasks. It provides a way to decouple task submission from the mechanics of task execution. Programmers normally use an Executor instead of explicitly creating threads. The Executor interface also offers both synchronous and asynchronous execution of tasks.

For synchronous execution, use the following commands:

Class MySynExecutor implements Executor{ public void execute(Runnable r) { r.run(); } }

For asynchronous execution, use the following commands:

Class MyASynExecutor implements Executor{ public void execute(Runnable r) { new Thread(r).start(); } }

ExecutorService Class
The ExecutorService class provides methods for managing termination and tracking the progress of one or more asynchronous tasks. The MyExecutorService.java file in the accompanying code download demonstrates the process of managing termination. It initiates a ThreadPool of size three and adds the threads sequentially. When the number of threads reaches the pool size, it invokes the shutdown method. After calling the shutdown() method, the pool does not accept any new tasks for execution. After waiting 10 seconds, the pool invokes shutDownNow(). This method makes a best effort to terminate all running tasks. In the example the application fails to terminate the executing threads.

ScheduledExecutorService
The ScheduledExecutorService class is my favorite. It is very handy for scheduling tasks that run periodically, which is especially useful for cleanup jobs (e.g., cleaning up all the temp files created by your application, etc.). The MyScheduledExecutorService.java file demonstrates the scheduling process by printing beep every five seconds:


final Runnable beeper = new Runnable() { public void run() { System.out.println("beep"); } }; final ScheduledFuture beeperHandle = scheduler.scheduleAtFixedRate(beeper, 1, 5, SECONDS);



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap