学习实践 JDK5 concurrent 并行包之executor(一)
这是介绍使用JDK 并行包之executor 第一部分,Executors 注意是复数,是最主要的工厂,通过它可以创建许多有用的东东,这部分介绍是如何创建一个固定的线程池。具体参考代码中的注释。
1 package net.vincent.study.executor;
2 import java.util.concurrent.ExecutorService;
3 import java.util.concurrent.Executors;
4
5
6 /** This is part1 of study executor package in concurrent on JDK 5.0
7 *
8 * @author wma
9 */
10 public class Part1 {
11
12 /**
13 Create a fixed threadPool for test.
14 * @param Number of thread of threadPool
15 * @return Created threadPool
16 * @throws null
17 */
18 public static ExecutorService getThreadPool( int numberOfThread){
19 if (numberOfThread <= 0 ){
20 return null ;
21 }
22 ExecutorService exec = Executors.newFixedThreadPool(numberOfThread);
23 return exec;
24 }
25 /**
26 * Create ruunbale, this runnable will random sleep.
27 * @return a Runnbale
28 */
29 public static Runnable getRunnable(){
30 Runnable run = new Runnable() {
31 public void run() {
32 long time = ( long ) (Math.random() * 1000 );
33 System.out.println( " Sleeping " + time + " ms " );
34 try {
35 Thread.sleep(time);
36 } catch (InterruptedException e) {
37 }
38 }
39 };
40 return run;
41 }
42 /** In main method, we create a threadPool(ThreadPoolExecutor) and capability is 4.
43 * After we submit 100 thread to this threadPool(ThreadPoolExecutor) and run it.
44 * Notice: If number of thread you submitted exceed capability, and threadPool(ThreadPoolExecutor) dont block Main method,
45 * because threadPool add other thread to queue.see code of ThreadPoolExecutor
46 *
47 * private final BlockingQueue<Runnable> workQueue;
48 * private final HashSet<Worker> workers = new HashSet<Worker>();
49 * try {
50 if (poolSize < corePoolSize && runState == RUNNING)
51 t = addThread(firstTask); // add other thread to queue.
52 } finally {
53 mainLock.unlock();
54 }
55 if (t == null)
56 return false;
57 t.start();
58 return true;
59
60 private Thread addThread(Runnable firstTask) {
61 Worker w = new Worker(firstTask);
62 Thread t = threadFactory.newThread(w);
63 if (t != null) {
64 w.thread = t;
65 workers.add(w);
66 int nt = ++poolSize;
67 if (nt > largestPoolSize)
68 largestPoolSize = nt;
69 }
70 return t;
71 }
72 *
73 * @param args
74 */
75 public static void main(String[] args) {
76 ExecutorService exe = getThreadPool( 4 );
77 for ( int i = 0 ; i < 100 ; i ++ )
78 exe.execute(getRunnable());
79 exe.shutdown();
80
81
82 }
83
84 }
85
1 package net.vincent.study.executor;
2 import java.util.concurrent.ExecutorService;
3 import java.util.concurrent.Executors;
4
5
6 /** This is part1 of study executor package in concurrent on JDK 5.0
7 *
8 * @author wma
9 */
10 public class Part1 {
11
12 /**
13 Create a fixed threadPool for test.
14 * @param Number of thread of threadPool
15 * @return Created threadPool
16 * @throws null
17 */
18 public static ExecutorService getThreadPool( int numberOfThread){
19 if (numberOfThread <= 0 ){
20 return null ;
21 }
22 ExecutorService exec = Executors.newFixedThreadPool(numberOfThread);
23 return exec;
24 }
25 /**
26 * Create ruunbale, this runnable will random sleep.
27 * @return a Runnbale
28 */
29 public static Runnable getRunnable(){
30 Runnable run = new Runnable() {
31 public void run() {
32 long time = ( long ) (Math.random() * 1000 );
33 System.out.println( " Sleeping " + time + " ms " );
34 try {
35 Thread.sleep(time);
36 } catch (InterruptedException e) {
37 }
38 }
39 };
40 return run;
41 }
42 /** In main method, we create a threadPool(ThreadPoolExecutor) and capability is 4.
43 * After we submit 100 thread to this threadPool(ThreadPoolExecutor) and run it.
44 * Notice: If number of thread you submitted exceed capability, and threadPool(ThreadPoolExecutor) dont block Main method,
45 * because threadPool add other thread to queue.see code of ThreadPoolExecutor
46 *
47 * private final BlockingQueue<Runnable> workQueue;
48 * private final HashSet<Worker> workers = new HashSet<Worker>();
49 * try {
50 if (poolSize < corePoolSize && runState == RUNNING)
51 t = addThread(firstTask); // add other thread to queue.
52 } finally {
53 mainLock.unlock();
54 }
55 if (t == null)
56 return false;
57 t.start();
58 return true;
59
60 private Thread addThread(Runnable firstTask) {
61 Worker w = new Worker(firstTask);
62 Thread t = threadFactory.newThread(w);
63 if (t != null) {
64 w.thread = t;
65 workers.add(w);
66 int nt = ++poolSize;
67 if (nt > largestPoolSize)
68 largestPoolSize = nt;
69 }
70 return t;
71 }
72 *
73 * @param args
74 */
75 public static void main(String[] args) {
76 ExecutorService exe = getThreadPool( 4 );
77 for ( int i = 0 ; i < 100 ; i ++ )
78 exe.execute(getRunnable());
79 exe.shutdown();
80
81
82 }
83
84 }
85