The java. util. concurrent package in J2SE 5.0 provides a new thread framework component that processes many low-level details related to creation, execution, and management threads. In this article, we will take a closer look at its important features.
If you use C, C ++, or a previous Java version for multi-threaded programming, you will know how troublesome it is to manage threads in code. In a single-threaded program, the bug that causes application failure in the code appears at the same point each time. However, in multi-threaded programs, failure occurs only when there are some causes. It is difficult to predict all the conditions that may cause application failure, so multi-threaded programming is challenging. Some programmers fundamentally avoid this challenge, while others-wise people who solve the problem-sit in front of their computers until the problem is solved.
The J2SE 5.0 platform contains a new concurrency tool package. The classes in this package are used to create blocking for concurrent classes (concurrent classe) or applications used in concurrent design ). The concurrency tool contains the following content:
· High-performance and flexible Thread Pool
· Framework component for asynchronous transaction execution
· Set-Class host (host) optimized for concurrent access)
This article introduces J2SE 5.0 framework component classes and their important features. The download code in this article provides some simple and easy-to-use examples, which demonstrate all the new thread framework component classes. After reading the article, you can run these examples to better understand these features.
Executor framework component
The Executor framework component provides a simple, standard, and extensible class that provides some useful functions. Without these functions, we need to implement them manually, it will be monotonous and difficult. This framework component standardizes the calling, scheduling, and execution operations. It supports Controlling Asynchronous transactions through a set of execution policies.
The Executor interface executes committed transactions that can be run. It provides a way to separate the transaction commit from the transaction execution mechanism. Programmers usually use Executor instead of explicit threads. The Executor interface also provides synchronous and asynchronous execution of transactions.
For synchronous execution, run the following command:
Class MySynExecutor implements Executor{
public void execute(Runnable r) {
r.run();
}
}
For Asynchronous execution, use the following command:
Class MyASynExecutor implements Executor{
public void execute(Runnable r) {
new Thread(r).start();
}
}
ExecutorService (Executor Service) Class
The ExecutorService class provides methods for managing the termination and tracking of one or more asynchronous transactions. The MyExecutorService. java file in the Code download demonstrates the process of managing transaction termination. It initializes three thread pools and adds threads in sequence. When the number of threads reaches the thread pool size limit, it calls the shutdown method. After the shutdown () method is called, the thread pool no longer accepts the execution of new transactions. Wait 10 seconds later, the thread pool calls shutDownNow (). This method will do its best to terminate all running transactions. In this example, the application failed to terminate the running thread.
ScheduledExecutorService (scheduling executor Service)
The ScheduledExecutorService class is my favorite class. It is very convenient to schedule those cyclically executed transactions, and the periodically executed transactions are especially useful for clearing jobs (such as clearing temporary files created by your application. Download the MyScheduledExecutorService. java file in the Code and send a "beep" every five seconds to demonstrate the scheduling process:
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep"); }
};
final ScheduledFuture beeperHandle =scheduler.scheduleAtFixedRate(beeper, 1, 5, SECONDS);