地质所 沉降监测网建设项目
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.javaweb.common.utils.thread.exception;
 
 
 
import com.javaweb.common.utils.thread.exception.util.ExceptionMessageFormat;
import com.javaweb.common.utils.thread.exception.util.factory.ExceptionMsgFormatFactory;
 
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
 
/**
 * 子线程异常,子线程出现异常时抛出
 * @author zengyuanjun
 */
public class ChildThreadException extends Exception {
 
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 5682825039992529875L;
    /**
     * 子线程的异常列表
     */
    private List<Exception> exceptionList;
    /**
     * 异常信息格式化工具
     */
    private ExceptionMessageFormat formatter;
    /**
     * 锁
     */
    private Lock lock;
 
    public ChildThreadException() {
        super();
        initial();
    }
 
    public ChildThreadException(String message) {
        super(message);
        initial();
    }
 
    public ChildThreadException(String message, StackTraceElement[] stackTrace) {
        this(message);
        setStackTrace(stackTrace);
    }
 
    private void initial() {
        exceptionList = new ArrayList<Exception>();
        lock = new ReentrantLock();
        formatter = ExceptionMsgFormatFactory.getInstance().getFormatter(ExceptionMsgFormatFactory.STACK_TRACE);
    }
 
    /**
     * 子线程是否有异常
     * @return
     */
    public boolean hasException() {
        return exceptionList.size() > 0;
    }
 
    /**
     * 添加子线程的异常
     * @param e
     */
    public void addException(Exception e) {
        try {
            lock.lock();
            e.setStackTrace(e.getStackTrace());
            exceptionList.add(e);
        } finally {
            lock.unlock();
        }
    }
 
    /**
     * 获取子线程的异常列表
     * @return
     */
    public List<Exception> getExceptionList() {
        return exceptionList;
    }
 
    /**
     * 清空子线程的异常列表
     */
    public void clearExceptionList() {
        exceptionList.clear();
    }
 
    /**
     * 获取所有子线程异常的堆栈跟踪信息
     * @return
     */
    public String getAllStackTraceMessage() {
        StringBuffer sb = new StringBuffer();
        for (Exception e : exceptionList) {
            sb.append(e.getClass().getName());
            sb.append(": ");
            sb.append(e.getMessage());
            sb.append("\n");
            sb.append(formatter.formate(e));
        }
        return sb.toString();
    }
 
    /**
     * 打印所有子线程的异常的堆栈跟踪信息
     */
    public void printAllStackTrace() {
        printAllStackTrace(System.err);
    }
 
    /**
     * 打印所有子线程的异常的堆栈跟踪信息
     * @param s
     */
    public void printAllStackTrace(PrintStream s) {
        for (Exception e : exceptionList) {
            e.printStackTrace(s);
        }
    }
 
}