地质所 沉降监测网建设项目
zmk
2024-05-16 6cc8b13ee47303a907f8e57b018406c46d8928e4
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
package com.javaweb.common.utils.thread.test;
 
 
 
import com.javaweb.common.utils.thread.MultiThreadHandler;
import com.javaweb.common.utils.thread.exception.ChildThreadException;
import com.javaweb.common.utils.thread.parallel.MultiParallelThreadHandler;
 
import java.util.HashMap;
import java.util.Map;
 
 
public class TestCase implements Runnable {
 
    private String name;
    private Map<String, Object> result;
    
    public TestCase(String name, Map<String, Object> result) {
        this.name = name;
        this.result = result;
    }
    
    @Override
    public void run() {
        // 模拟线程执行1000ms
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 模拟线程1和线程3抛出异常
        if(name.equals("1") || name.equals("3")) {
            throw new RuntimeException(name + ": throw exception");
        }
        Map<String, Object> t=(Map<String, Object>)result.get(name);
        if(t==null){
            t=new HashMap<String, Object>();
            t.put("msg", "成功!");
            t.put("result", "success");
            result.put(name,t);
        }
 
    }
    
    public static void main(String[] args) {
        
        System.out.println("main begin \t=================");
        Map<String, Object> resultMap = new HashMap<String, Object>(8, 1);
        MultiThreadHandler handler = new MultiParallelThreadHandler();
//        ExecutorService service = Executors.newFixedThreadPool(3);
//        MultiThreadHandler handler = new ParallelTaskWithThreadPool(service);
        TestCase task = null;
        // 启动5个子线程作为要处理的并行任务,共同完成结果集resultMap
        for(int i=1; i<=5 ; i++){
            task = new TestCase("" + i, resultMap);
            handler.addTask(task);
        }
        try {
            handler.run();
        } catch (ChildThreadException e) {
            System.out.println(e.getAllStackTraceMessage());
        }
        
        System.out.println(resultMap);
//        service.shutdown();
        System.out.println("main end \t=================");
    }
}