在Runnable方法中捕获异常

时间:2017-07-29 11:27:57

标签: java multithreading exception runnable executorservice

我正在寻找一种方法来捕捉NullPointerException课程中的Runnable。 我有run调用此方法:

private void cooccurrence(int contextIndex, int targetIndex) {
        try {
            Double old_value = X.get(contextIndex, targetIndex);
            X.set(contextIndex, targetIndex, old_value + 1);
        }catch (NullPointerException e) {
            X.growData(X.nz_length + 1);
            X.addItem(contextIndex, targetIndex,  1);
        }
}

我将此代码放在另一个调用类ThreadForX implements Runnable的类中:

final BlockingQueue<JSONObject> queue = new
ArrayBlockingQueue<>(5000000);
int json_object_in_queue = 0;

ExecutorService pool = Executors.newFixedThreadPool(2);

DMatrixSparseTriplet X = new DMatrixSparseTriplet(200000,200000,0);

JSONObject inQueue_jsonObject;
while ((inQueue_jsonObject = queue.poll()) != null) {
        Runnable readTweet = new ThreadForX(contextLenght, inQueue_jsonObject, X, final_vocabulary);
        pool.execute(readTweet);
}
pool.shutdown();
while (!pool.isTerminated()) { }

正如您所看到的,NullPointerException对于添加项目非常重要,因此我需要contextIndextargetIndex的值

0 个答案:

没有答案
相关问题