Java 具有多线程的Collection.sort()中的NullpointerException
我有一个类应该使用多线程来测试我的斐波那契函数Java 具有多线程的Collection.sort()中的NullpointerException,java,multithreading,nullpointerexception,executorservice,Java,Multithreading,Nullpointerexception,Executorservice,我有一个类应该使用多线程来测试我的斐波那契函数 public class PerformanceTesterImpl implements PerformanceTester{ public static List<Long> executionTimesList = new ArrayList(); public static List<Runnable> tasksList = new ArrayList(); public int fib;
public class PerformanceTesterImpl implements PerformanceTester{
public static List<Long> executionTimesList = new ArrayList();
public static List<Runnable> tasksList = new ArrayList();
public int fib;
public PerformanceTestResult performanceTestResult;
@Override
public PerformanceTestResult runPerformanceTest(Runnable task, int calculationCount, int threadPoolSize) {
for(int i=0; i<calculationCount; i++){
tasksList.add(createTask(fib));
}
ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
for(Runnable r : tasksList){
executor.execute(r);
}
executor.shutdown();
try {
executor.awaitTermination(1, TimeUnit.MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Here all threads should complete all work. Is it OK?
mapValues();
return performanceTestResult;
}
private PerformanceTestResult mapValues(){
Collections.sort(executionTimesList);
performanceTestResult = new PerformanceTestResult(getSum(executionTimesList), (Long)executionTimesList.get(0), (Long)executionTimesList.get(executionTimesList.size()-1));
return performanceTestResult;
}
public Runnable createTask (final int n) {
fib = n;
Runnable runnable = new Runnable() {
@Override
public void run() {
long startTime = System.currentTimeMillis();
FibCalc fibCalc = new FibCalcImpl();
fibCalc.fib(n);
long executionTime = System.currentTimeMillis() - startTime;
executionTimesList.add(executionTime);
}
};
return runnable;
}
private static long getSum(List<Long> executionTimes){
long sum = 0;
for(long l : executionTimes){
sum += l;
}
return sum;
}
}
公共类PerformanceTesterImpl实现PerformanceTester{
public static List executionTimesList=new ArrayList();
public static List tasksList=new ArrayList();
公共int fib;
公共绩效结果绩效结果;
@凌驾
public PerformanceTestResult runPerformanceTest(可运行任务、int计算计数、int线程池大小){
for(int i=0;iArrayList不是线程安全的
从Javadoc:
* If multiple threads access an <tt>ArrayList</tt> instance concurrently,
* and at least one of the threads modifies the list structurally, it
* <i>must</i> be synchronized externally. (A structural modification is
* any operation that adds or deletes one or more elements, or explicitly
* resizes the backing array; merely setting the value of an element is not
* a structural modification.) This is typically accomplished by
* synchronizing on some object that naturally encapsulates the list.
* If no such object exists, the list should be "wrapped" using the
* {@link Collections#synchronizedList Collections.synchronizedList}
* method. This is best done at creation time, to prevent accidental
* unsynchronized access to the list:<pre>
* List list = Collections.synchronizedList(new ArrayList(...));</pre>
executionTimesList在所有线程之间共享。它们在您的代码中并行运行,不进行同步。因此,如果一个线程在列表上工作,但尚未完成其工作,并且cpu将优先权授予在列表上工作的另一个线程,则出现任何不一致状态是合乎逻辑的,第一个线程将处于不一致状态当它将被重新启动时。
您必须同步对静态字段executionTimesList的访问您还可以发布stacktrace吗?谢谢!我使用CopyOnWriteArrayList而不是arraylist
public void run() {
long startTime = System.currentTimeMillis();
FibCalc fibCalc = new FibCalcImpl();
fibCalc.fib(n);
long executionTime = System.currentTimeMillis() - startTime;
synchronized (this) {
executionTimesList.add(executionTime);
}
}