33import java .io .IOException ;
44import java .net .ServerSocket ;
55import java .net .Socket ;
6- import java .util .concurrent .Executor ;
7- import java .util .concurrent .ExecutorService ;
8- import java .util .concurrent .Executors ;
6+ import java .util .ArrayList ;
7+ import java .util .Date ;
8+ import java .util .List ;
9+ import java .util .concurrent .*;
910import java .util .concurrent .atomic .AtomicLong ;
1011
1112public class JSR166ConcurrencyUtilities {
@@ -36,12 +37,14 @@ public static void main(String[] args) throws IOException {
3637 System.out.println(atomicVariableExample.next());
3738 }
3839 */
39-
40- /*
4140 JSR166ConcurrencyUtilities jsr166ConcurrencyUtilities = new JSR166ConcurrencyUtilities ();
41+ /*
42+
4243 jsr166ConcurrencyUtilities.threadPool();;
4344
4445 */
46+
47+ // jsr166ConcurrencyUtilities.callable();
4548 }
4649
4750 public void threadPool () throws IOException {
@@ -61,6 +64,23 @@ public void run() {
6164 }
6265 }
6366
67+ public void callable () {
68+ ExecutorService executor = Executors .newFixedThreadPool (10 );
69+ List <Future <String >> list = new ArrayList <Future <String >>();
70+ Callable <String > callable = new JSR166ConcurrencyCallable ();
71+ for (int i = 0 ; i < 100 ; i ++) {
72+ Future <String > future = executor .submit (callable );
73+ list .add (future );
74+ }
75+ for (Future <String > fut : list ){
76+ try {
77+ System .out .println (new Date ()+ "::" +fut .get ());
78+ }catch (InterruptedException | ExecutionException e ){
79+ e .printStackTrace ();
80+ }
81+ }
82+ }
83+
6484 class Handler {
6585 void process (Socket s ){
6686
@@ -84,4 +104,12 @@ long next() {
84104 }
85105 }
86106
107+ class JSR166ConcurrencyCallable implements Callable <String > {
108+
109+ @ Override
110+ public String call () throws Exception {
111+ Thread .sleep (100 );
112+ return Thread .currentThread ().getName ();
113+ }
114+ }
87115}
0 commit comments