java110
2020-06-02 72378797a483b754122dfd368ce7cddec949e262
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
package com.java110.log.executor;
 
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
 
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
 
/**
 * Created by wuxw on 2017/4/25.
 */
public class ExecutorTest  extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public ExecutorTest( String testName )
    {
        super( testName );
    }
 
    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( ExecutorTest.class );
    }
 
    public void testThrowException(){
        ExecutorService executorService = Executors.newFixedThreadPool(100);
 
        int i = 0;
        List<Future<Integer>> futureList = new ArrayList<Future<Integer>>();
        while(i < 5) {
            Future<Integer> sum = executorService.submit(new PrintInt());
            i++;
            futureList.add(sum);
        }
 
        for(Future<Integer> sum : futureList) {
            try{
 
                    System.out.println(sum.get());
 
            }catch (Exception e){
                System.out.println("异常了");
                e.printStackTrace();
            }
        }
    }
 
    public void testThrowException1(){
        ExecutorService executorService = Executors.newFixedThreadPool(100);
 
        int i = 0;
        while(i < 5) {
            Future<Integer> sum = executorService.submit(new PrintInt());
            i++;
 
            try{
 
                System.out.println(sum.get());
 
            }catch (Exception e){
                System.out.println("异常了");
                e.printStackTrace();
            }
 
        }
 
    }
 
}