为什么在此代码中出现ConcurrentModificationException?

时间:2018-06-29 07:30:29

标签: android

我得到的错误是:

E/AndroidRuntime: FATAL EXCEPTION: HandlerThread
              Process: com.ssriram.noisesensor, PID: 21735
              java.util.ConcurrentModificationException
                  at java.util.LinkedList$LinkIterator.next(LinkedList.java:124)
                  at com.ssriram.noisesensor.Dispatcher.accept(Dispatcher.java:15)
                  at com.ssriram.noisesensor.SoundAlert.run(SoundAlert.java:37)
                  at com.ssriram.noisesensor.SubService$1.run(SubService.java:57)
                  at android.os.Handler.handleCallback(Handler.java:815)
                  at android.os.Handler.dispatchMessage(Handler.java:104)
                  at android.os.Looper.loop(Looper.java:207)
                  at android.os.HandlerThread.run(HandlerThread.java:61)

Dispatcher类为:

public class Dispatcher<T> implements Callback<T> {

private List<Callback<T>> callbacks = new LinkedList<>();

@Override
public void accept(T t) {
    for (Callback<T> callback : callbacks) {
        callback.accept(t);
    }
}

public void registerCallback(Callback<T> callback) {

    callbacks.add(callback);
}

}

然后我在SoundAlert.java中运行以下代码

this.dispatcher.accept(this.getAvg());

我使用Runnable在SubService类中运行SoundAlert:

soundAlert3.run();
            if (running)
                backgroundHandler.postDelayed(runnable, 1000);

有人能让我知道这是什么原因吗?

0 个答案:

没有答案