Java:并行处理一个数组,查找哪个位置出现异常
Java: Processing an array in parallel, find which position an exception occurred
首先,我有一个数组,除了位置 5 之外,它都填充了 0,位置 5 填充了 "a"(故意放在那里以抛出 NumberFormatException)。
然后我调用 testMethod 传递数组、数组的大小以及将有多少个 Callable。
在此示例中,数组的大小为 10,并且有 4 个可调用对象。数组以块的形式处理:
第一个块是位置 0 和 1
第二块是位置 2 和 3
第三块是位置 4 和 5
第四块是位置 6 和 7
第五个块是位置 8 和 9
第六个块是位置 10
我需要找出 NumberFormatException 发生在哪个位置,或者更一般意义上:我需要知道发生任何异常时的位置。
所以我可以在消息中打印出来"Execution exception occurred at position 5"
我对使用 ExceutorService/Callables 很陌生,所以我不太确定如何实现这个...
如果使用我当前的设置无法实现...是否有类似的方法来执行此并行处理,同时让我能够找到发生异常的位置?
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class ThreadTest {
private final static ArrayList<Callable<Boolean>> mCallables = new ArrayList<>();
private final static ExecutorService mExecutor = Executors.newFixedThreadPool(4);
public static void main(String[] args) throws Exception {
/*Fill the array with 0's, except for position 5 which is a letter and will throw number format exception*/
final String[] nums = new String[10];
for (int i = 0; i < 5; i++) {
nums[i] = "0";
}
nums[5] = "a";
for (int i = 6; i < nums.length; i++) {
nums[i] = "0";
}
testMethod(nums, 10, 4);
}
static void testMethod(String[] nums, int size, int processors) throws Exception {
mCallables.clear();
int chunk = (size / processors) == 0 ? size : size / processors;
System.out.println("Chunk size: "+chunk);
for (int low = 0; low < size; low += chunk) {
final int start = low;
final int end = Math.min(size, low + chunk);
mCallables.add(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
System.out.println("New call");
for (int pos = start; pos < end; pos++) {
System.out.println("Pos is " + pos);
System.out.println("Num is " + nums[pos]);
double d = Double.parseDouble(nums[pos]);
} //end inner loop
return true;
} //end call method
}); //end callable anonymous class
}
try {
List<Future<Boolean>> f = mExecutor.invokeAll(mCallables);
for (int i = 0; i < f.size(); i++) {
f.get(i).get();
}
} catch (ExecutionException e) {
String s = e.toString();
System.out.println(s);
System.out.println("Execution exception"); //need to write here which pos the numberFormat exception occurred
}
mExecutor.shutdown();
}
}
你不能在 Double.parseDouble
行上添加一个 try/catch 并抛出一个包含位置的异常吗?
首先,我有一个数组,除了位置 5 之外,它都填充了 0,位置 5 填充了 "a"(故意放在那里以抛出 NumberFormatException)。
然后我调用 testMethod 传递数组、数组的大小以及将有多少个 Callable。
在此示例中,数组的大小为 10,并且有 4 个可调用对象。数组以块的形式处理:
第一个块是位置 0 和 1 第二块是位置 2 和 3 第三块是位置 4 和 5 第四块是位置 6 和 7 第五个块是位置 8 和 9 第六个块是位置 10
我需要找出 NumberFormatException 发生在哪个位置,或者更一般意义上:我需要知道发生任何异常时的位置。
所以我可以在消息中打印出来"Execution exception occurred at position 5"
我对使用 ExceutorService/Callables 很陌生,所以我不太确定如何实现这个...
如果使用我当前的设置无法实现...是否有类似的方法来执行此并行处理,同时让我能够找到发生异常的位置?
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class ThreadTest {
private final static ArrayList<Callable<Boolean>> mCallables = new ArrayList<>();
private final static ExecutorService mExecutor = Executors.newFixedThreadPool(4);
public static void main(String[] args) throws Exception {
/*Fill the array with 0's, except for position 5 which is a letter and will throw number format exception*/
final String[] nums = new String[10];
for (int i = 0; i < 5; i++) {
nums[i] = "0";
}
nums[5] = "a";
for (int i = 6; i < nums.length; i++) {
nums[i] = "0";
}
testMethod(nums, 10, 4);
}
static void testMethod(String[] nums, int size, int processors) throws Exception {
mCallables.clear();
int chunk = (size / processors) == 0 ? size : size / processors;
System.out.println("Chunk size: "+chunk);
for (int low = 0; low < size; low += chunk) {
final int start = low;
final int end = Math.min(size, low + chunk);
mCallables.add(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
System.out.println("New call");
for (int pos = start; pos < end; pos++) {
System.out.println("Pos is " + pos);
System.out.println("Num is " + nums[pos]);
double d = Double.parseDouble(nums[pos]);
} //end inner loop
return true;
} //end call method
}); //end callable anonymous class
}
try {
List<Future<Boolean>> f = mExecutor.invokeAll(mCallables);
for (int i = 0; i < f.size(); i++) {
f.get(i).get();
}
} catch (ExecutionException e) {
String s = e.toString();
System.out.println(s);
System.out.println("Execution exception"); //need to write here which pos the numberFormat exception occurred
}
mExecutor.shutdown();
}
}
你不能在 Double.parseDouble
行上添加一个 try/catch 并抛出一个包含位置的异常吗?