JAVA5 Concurrent Study 2008-11-25 15:25:33 Tags: java5 concurrent Leisure Workplace Copyright statement: Original works, if necessary reprint, please contact the author. Otherwise, legal liability will be held. Java5 Concurrent Learning after JAVA5, a fundamental change has taken place in the concurrency thread, the most important of which is a new stack of APIs for starting, scheduling, and managing threads. After Java5, it is better to start the line via executor turndown using thread Start (). In new features, it is easy to control the thread's startup, execution, and shutdown processes, and it is easy to use the characteristics of the thread pool. A, creating a task task is a class that implements the Runnable interface. When you create the real run method. II. Perform tasks The task is performed through the Java.util.concurrent.ExecutorService interface object, which is created by a static method of the tool class java.util.concurrent.Executors. Executors factory and practical methods for the Executor, Executorservice, Scheduledexecutorservice, Threadfactory, and callable classes defined in this package. Executorservice provides a way to manage termination, and a way to generate Future to track one or more asynchronous task execution conditions. You can turn off Executorservice, which causes it to stop accepting new tasks. When closed, the execution program terminates, no tasks are performed, no tasks are pending, and new tasks cannot be submitted. Executorservice.execute (New testrunnable ()); 1, create executorservice through static methods of tool class Java.util.concurrent.Executors. Executors the factories and practical methods of the Executor, Executorservice, Scheduledexecutorservice, Threadfactory, and callable classes defined in this package. For example, creating aA Executorservice instance, Executorservice is actually a thread pool management tool: Executorservice Executorservice = Executors.newcachedthreadpool (); Executorservice executorservice = Executors.newfixedthreadpool (3); Executorservice executorservice = Executors.newsinglethreadexecutor ();
2, add the task to the thread to perform when a task is added to a thread pool, the thread pool creates a thread for each task that is automatically executed at a later time. Third, closes the execution service object Executorservice.shutdown (); Iv. comprehensive examples of package concurrent;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
/**
* Created by IntelliJ idea.
*
* @author leizhimin 2008-11-25 14:28:59
*/
public class Testcachedthreadpool {
public static void Main (string[] args) {
Executorservice Executorservice = Executors.newcachedthreadpool ();
Executorservice Executorservice = Executors.newfixedthreadpool (5); Executorservice Executorservice = Executors.newsinglethreadexecutor ();
for (int i = 0; i < 5; i++) {
Executorservice.execute (New testrunnable ());
System.out.println ("************* a" + i + "*************");
}
Executorservice.shutdown ();
}
}
Class Testrunnable implements Runnable {
public void Run () {
System.out.println (Thread.CurrentThread (). GetName () + "thread was invoked. ");
while (true) {
try {
Thread.Sleep (5000);
System.out.println (Thread.CurrentThread (). GetName ());
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
Run Result: ************* A0 *************
A1 *************
The pool-1-thread-2 thread was invoked.
A2 *************
The pool-1-thread-3 thread was invoked.
The pool-1-thread-1 thread was invoked.
A3 *************
A4 *************
The pool-1-thread-4 thread was invoked.
The pool-1-thread-5 thread was invoked.
Pool-1-thread-2
Pool-1-thread-1
Pool-1-thread-3
Pool-1-thread-5
Pool-1-thread-4
Pool-1-thread-2
Pool-1-thread-1
Pool-1-thread-3
Pool-1-thread-5
Pool-1-thread-4
...... V. Get the return value of the execution of a task after Java5, the tasks are divided into two classes: one is the class that implements the Runnable interface, the other is the class that implements the callable interface. Both can be executed by Executorservice, but the runnable task does not return a value, and the callable task has a return value. and the callable call () method can only be performed through the Executorservice submit (Callable <T> Task) method and returns a <T> Future <t> Waiting to complete the Future.
public interface callable<v>
The task that returns the result and may throw an exception. The implementing person defines a method called call without any arguments. The callable interface is similar to Runnable, both of which are designed for classes whose instances might be executed by another thread. However, Runnable does not return a result and cannot throw a checked exception. The Executors class contains some practical methods for converting from other common forms into callable classes. The call () method in callable is similar to the runnable run () method, which has a return value, which is not. When a callable object is passed to the Executorservice submit method, the call method is automatically executed on a thread and returns the execution result future object. Similarly, the Runnable object is passed to the Executorservice submit method, the Run method is automatically executed on a thread, and the execution result future object is returned, but the Get method is called on the future object, and Null is returned. Unfortunately, in the Java API documentation, this piece of the introduction is very confusing, it is estimated that the translator has not figured out why. Or the comment is not in place. See below for example: import java.util.ArrayList;
Import java.util.List;
Import java.util.concurrent.*;
/**
* Callable Interface Test
*
* @author leizhimin 2008-11-26 9:20:13
*/
public class Callabledemo {
public static void Main (string[] args) {
Executorservice Executorservice = Executors.newcachedthreadpool ();
list<future<string>> resultlist = new arraylist<future<string>> ();
Create 10 tasks and execute
for (int i = 0; i < i++) {
Use Executorservice to perform callable types of tasks and save the results in future variables
future<string> Future = executorservice.submit (new Taskwithresult (i));
To store the results of a task execution in a list
Resultlist.add (future);
}
Traversing the results of a task
for (future<string> fs:resultlist) {
try {
System.out.println (Fs.get ()); Print the results of each thread (Task) execution
catch (Interruptedexception e) {
E.printstacktrace ();
catch (Executionexception e) {
E.printstacktrace ();
finally {
Initiates a sequential shutdown, performs a previously committed task, but does not accept new tasks. If it is already closed, the call has no other effect.
Executorservice.shutdown ();
}
}
}
}
Class Taskwithresult implements Callable<string> {
private int id;
public taskwithresult (int id) {
This.id = ID;
}
/**
* The specific process of a task, which is automatically executed on a thread once the task is passed to the Executorservice submit method.
*
* @return
* @throws Exception
*/
Public String Call () throws Exception {
System.out.println ("Call () method is automatically called, work ... "+ Thread.CurrentThread (). GetName ());
A simulated, time-consuming operation
for (int i = 999999 i > 0; i--);