Sun in the JAVA5, the Java thread of the class library to do a lot of expansion, in which the thread pool is one of the new features of JAVA5, in addition to the thread pool, there are many multi-threaded related content, for multi-threaded programming has brought great convenience. In order to write high-performance and stable and reliable multithreaded programs, the new content of the thread part is particularly important.
The contents of the JAVA5 line assigns feature are all under Java.util.concurrent, which contains a number of interfaces and classes, and familiarity with this part of the API feature is a difficult learning process. At present, the information and books on this aspect are few and far between, and the book on the thread of the big belongs to the knowledge level before Java5.
Of course, the new features do not have a need for long-line programs, before JAVA5 General can write excellent multi-threaded program. But the price is different.
The basic idea of the thread pool is also the idea of an object pool, which opens up a memory space in which many (not dead) threads are stored, and the thread execution schedule is handled by the pool manager. When a thread task is taken from a pool, the threads object is pooled after execution, which avoids the performance overhead of repeatedly creating thread objects and saves system resources.
Before Java5, to implement a thread pool is quite difficult, now Java5 for us to do everything, we just need to follow the provided API to use, can enjoy the great convenience of the thread pool.
JAVA5 's thread pool can be divided into two categories: a fixed-size thread pool and a variable-size connection pool.
Before using the thread pool, you must know how to create a thread pool, and in Java5, you need to know about the API of the Java.util.concurrent.Executors class, which provides a lot of static methods for creating connection pools that you must master.
A fixed-size thread pool, Newfixedthreadpool:
[Java]View Plaincopy
- Package app.executors;
- Import java.util.concurrent.Executors;
- Import Java.util.concurrent.ExecutorService;
- /**
- * Java Thread: thread pool
- *
- * @author Feng Xiaowei
- */
- Public class Test {
- public static void Main (string[] args) {
- //Create a thread pool that can reuse a fixed number of threads
- Executorservice pool = Executors.newfixedthreadpool (5);
- //Create thread
- Thread T1 = new MyThread ();
- Thread t2 = new MyThread ();
- Thread t3 = new MyThread ();
- Thread T4 = new MyThread ();
- Thread T5 = new MyThread ();
- //Put the thread into the pool for execution
- Pool.execute (t1);
- Pool.execute (T2);
- Pool.execute (T3);
- Pool.execute (T4);
- Pool.execute (T5);
- //close thread pool
- Pool.shutdown ();
- }
- }
- Class MyThread extends Thread {
- @Override
- public Void Run () {
- System.out.println (Thread.CurrentThread (). GetName () + "executing ... ");
- }
- }
Output Result:
[HTML]View Plaincopy
- Pool-1-thread-1 is executing ...
- Pool-1-thread-3 is executing ...
- Pool-1-thread-4 is executing ...
- Pool-1-thread-2 is executing ...
- Pool-1-thread-5 is executing ...
Change the parameters in Executorservice pool = Executors.newfixedthreadpool (5): Executorservice pool = Executors.newfixedthreadpool (2), The output is:
[HTML]View Plaincopy
- Pool-1-thread-1 is executing ...
- Pool-1-thread-1 is executing ...
- Pool-1-thread-2 is executing ...
- Pool-1-thread-1 is executing ...
- Pool-1-thread-2 is executing ...
As can be seen from the above results, the Newfixedthreadpool parameter specifies the maximum number of threads that can be run, which will not run after the number of lines loads in. Second, threads that join the thread pool are managed, and the threads are not affected by the order of accession.
Second, single task thread pool, Newsinglethreadexecutor:
Just change the Executorservice pool = Executors.newfixedthreadpool (2) in the above code to Executorservice pool = Executors.newsinglethreadexecutor ();
Output Result:
[HTML]View Plaincopy
- Pool-1-thread-1 is executing ...
- Pool-1-thread-1 is executing ...
- Pool-1-thread-1 is executing ...
- Pool-1-thread-1 is executing ...
- Pool-1-thread-1 is executing ...
As you can see, each call to the Execute method is, in fact, the last call to the Thread-1 run method.
Three, variable size thread pool, Newcachedthreadpool:
Similar to the above, just change the way the pool was created: executorservice pool = Executors.newcachedthreadpool ();
Output:
[HTML]View Plaincopy
- Pool-1-thread-1 is executing ...
- Pool-1-thread-2 is executing ...
- Pool-1-thread-4 is executing ...
- Pool-1-thread-3 is executing ...
- Pool-1-thread-5 is executing ...
This approach is characterized by the creation of thread pools for new threads as needed, but reusing them when previously constructed threads are available.
Iv. delayed Connection pool, Newscheduledthreadpool:
[Java]View Plaincopy
- Package app.executors;
- Import java.util.concurrent.Executors;
- Import Java.util.concurrent.ScheduledExecutorService;
- Import Java.util.concurrent.TimeUnit;
- /**
- * Java Thread: thread pool
- *
- * @author Feng Xiaowei
- */
- Public class Test {
- public static void Main (string[] args) {
- //Create a thread pool that can schedule commands to run after a given delay or perform them on a regular basis.
- Scheduledexecutorservice pool = Executors.newscheduledthreadpool (2);
- //created implements the Runnable interface object, the thread object of course also implements the Runnable interface
- Thread T1 = new MyThread ();
- Thread t2 = new MyThread ();
- Thread t3 = new MyThread ();
- //Put the thread into the pool for execution
- Pool.execute (t1);
- //Using the method of deferred execution style
- Pool.schedule (T2, timeunit.milliseconds);
- Pool.schedule (T3, ten, timeunit.milliseconds);
- //close thread pool
- Pool.shutdown ();
- }
- }
- Class MyThread extends Thread {
- @Override
- public Void Run () {
- System.out.println (Thread.CurrentThread (). GetName () + "executing ... ");
- }
- }
Readers can try to change the parameters of Executors.newscheduledthreadpool (2) to get more experience, of course, let
[Java]View Plaincopy
- @Override
- Public void Run () {
- System.out.println (Thread.CurrentThread (). GetName () + "executing ... ");
- }
into an infinite loop, you can get more usage about Pool.shutdown ().
V: Single-task delayed connection pool (similar to the above, it is not written). Of course, we can also customize the thread pool, here will not write, tired ah ...
Transferred from: http://blog.csdn.net/coding_or_coded/article/details/6856014
Java executors (thread pool)