The Java.util.concurrent package in J2SE 5.0 provides a new thread framework component that handles many low-level details related to building, executing, and managing threads. In this article we will take a closer look at the important features of it.
If you use C, C + +, or a previous version of Java for multithreaded programming, you know how frustrating it is to manage threads in your code. In a single-threaded program, bugs that cause application failures in your code appear at the same point each time. However, in multithreaded programs, there are only a few reasons why failure occurs when you encounter one. Multithreaded programming is challenging because it is very difficult to anticipate all the conditions that might cause an application to fail. Some programmers fundamentally avoid this challenge, while others--intelligent problem-solving people--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 create blocks (blocking) for concurrent classes (concurrent Classe) or for applications used in concurrent designs. The concurrency tool contains some of the following:
· High-performance, flexible thread pool
· A framework component that performs transactions asynchronously
· Collection class host (host) optimized for concurrent access
This article describes the J2SE 5.0 framework component classes and their important features. The download code for this article provides some simple, easy-to-use examples that demonstrate all the new thread framework component classes. You can have a better understanding of these features by running these examples after you read the content of the article.
Executor (Actuator) Framework component
The Executor framework component provides a simple, standard, extensible class that provides useful functionality that, without these features, can be tedious and difficult to implement manually. This framework component standardizes calls, scheduling, and performing operations. It provides support for controlling asynchronous transactions through a set of execution policies.
The executor interface performs a committed transaction that can be run. It provides a way for us to separate transaction submissions from the transaction execution mechanism. Programmers typically use executor instead of explicitly (explicitly) to create threads. The executor interface also provides synchronous and asynchronous execution of transactions.
For synchronous execution, use 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 (Actuator Service) class
The Executorservice class provides a way to manage the termination of one or more asynchronous transactions and to track the execution of transactions. The Myexecutorservice.java file in the code download demonstrates the process of terminating a management transaction. It initializes a thread pool of size three and then adds the threads in turn. When the number of threads reaches the size limit of the thread pool, it calls the shutdown (shutdown) method. After the shutdown () method is invoked, the thread pool no longer accepts the execution of the new transaction. After waiting 10 seconds, the thread pool calls Shutdownnow (). This method will do its best to terminate all running transactions. In the example, the application failed to attempt to terminate the running thread.