Java中的线程池

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.cn.gbx;
 
import java.util.Date;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
 
public class TestThread {
     
    public static void main(String[] args) {
         
        //Java中提供的三个线程池
//      ExecutorService threadPool = Executors.newCachedThreadPool();
//      ExecutorService threadPool2 = Executors.newFixedThreadPool(3);
//      ExecutorService threadPool3 = Executors.newSingleThreadExecutor();
//      for (int i = 1; i <= 10; ++i) {
//          final int task = i;
//          threadPool.execute( new Runnable() {
//              public void run() {
//                  for (int j = 1; j <= 10; ++j) {
//                      System.out.println(Thread.currentThread().getName() + " is looping " + j + " at task " + task);
//                  }
//              }
//          });
//      }
//      threadPool.shutdown();
         
         
         
         
        // Callable 与 future 的应用
         
//      ExecutorService threadPool = Executors.newCachedThreadPool();
//      Future<String> future = threadPool.submit(
//          new Callable<String>() {
//              @Override
//              public String call() throws Exception {
//                  Thread.sleep(2000);
//                  return "hello";
//              }
//          }
//      );
//      System.out.println("等待结果:");
//      try {
//          System.out.println("结果为: " + future.get());
//      } catch (InterruptedException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      } catch (ExecutionException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      }
         
         
        //利用CompletionService提交一组 Callable
        ExecutorService threadPool = Executors.newCachedThreadPool();
        CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(threadPool);
        for (int i = 1; i <= 10; ++i) {
            final int seq = i;
            completionService.submit(
                    new Callable<Integer>() {
 
                        @Override
                        public Integer call() throws Exception {
                            Thread.sleep(2000);
                            return seq;
                        }
                    }
                );
        }
        for (int i = 1; i <= 10; ++i) {
            try {
                System.out.println(completionService.take().get());
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

  

 

posted @   E_star  阅读(254)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示