我正在尝试收集发生异常的所有计数以及异常的名称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) {
}
}
}
Updated:
如果我有这样的东西 - 我得到Null Pointer Exception
40 个线程和 4000 个任务。为什么?
catch (SQLException e) {
synchronized(this) {
exceptionMap.put(e.getCause().toString(), counter_exception.incrementAndGet());
}
}