我正在尝试收集所有异常发生的计数和异常的名称,ConcurrentHashMap
以便我应该知道这个异常发生了多少次。
因此,在我的 catch 块中,我有一个地图,它将继续添加异常的名称,并且出现总数。
以下是我which I have modified to always throw SQL Exception
每次用于测试目的的代码,以便我可以看到异常计数是否准确。
所以某些场景-
1)如果我选择线程10
数和任务数50
,那么在该映射中,我可以看到该特定字符串的 500 异常
2)但是如果我选择线程40
数和任务数,500
那么我20000
在该地图中看不到异常,它显示在19000
.
我的问题是为什么?我在这里做错了什么?
class Task implements Runnable {
public static final AtomicInteger counter_exception = new AtomicInteger(0);
public static ConcurrentHashMap<String, Integer> exceptionMap = new ConcurrentHashMap<String, Integer>();
@Override
public void run() {
try {
//Making a db connection and then executing the SQL-
} catch (SQLException e) {
exceptionMap.put(e.getCause().toString(), counter_exception.incrementAndGet());
} catch (Exception e) {
}
}
}
更新:
如果我有这样的东西——我会得到Null Pointer Exception
40 个线程和 4000 个任务。为什么?
catch (SQLException e) {
synchronized(this) {
exceptionMap.put(e.getCause().toString(), counter_exception.incrementAndGet());
}
}