简体   繁体   English

Java中的线程。 “ AWT-EventQueue-0” java.util.ConcurrentModificationException

[英]Threads in Java. “AWT-EventQueue-0” java.util.ConcurrentModificationException

I have a timer, it makes every 0.02 seconds of action. 我有一个计时器,它每0.02秒执行一次操作。 The actions there checking on the condition, if it is carried out - call Thread for playing music. 检查条件是否存在(如果已执行)的操作-调用Thread播放音乐。 The problem is that while the melody is playing, this condition is again checked. 问题在于,在播放旋律时,会再次检查此情况。 In result Exception: Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException 结果异常:线程“ AWT-EventQueue-0”中的异常java.util.ConcurrentModificationException

- Background.java
....
 Thread eventMusic = new Thread(new EventMusic());
... check different conditions ... (every 0.02s)
 if everything is ok  -> eventMusic.start();

- EventMusic.java
package Shooter2Dv23082013;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class EventMusic implements Runnable{
    @Override
    public void run() {

        try {
            Player p = new Player(new FileInputStream(getClass().getClassLoader().getResource("res/nameSong.mp3").getPath()));
            p.play();
        } catch (JavaLayerException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();}


    }
}

The question is. 问题是。

  1. Create multiple threads? 创建多个线程? How to create threads for each condition (ie, a lot of threads, but I do not know how many of them will, after work - it will closed) 如何为每个条件创建线程(即,有很多线程,但我不知道在工作后会有多少线程-它将关闭)

  2. Frankly, it begs the observer template, if all conditions are met - notify () thread that wants to play a song, for different occasions. 坦率地说,如果满足所有条件,它将乞求观察者模板-在不同的情况下,要播放歌曲的notify()线程。

You haven't shown enough code to pin-point the problem but one of your threads have tried to modify a collection that one of your other threads is iterating over currently. 您没有显示足够的代码来查明问题所在,但是您的一个线程试图修改一个集合,而该集合是您其他线程之一正在迭代的。

The JavaDocs for ConcurrentModificationException say JavaDocs for ConcurrentModificationException

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible. 当不允许对对象进行同时修改时,检测到该对象的同时修改的方法可能会引发此异常。 For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. 例如,它一般容许的,而另一个线程上进行迭代一个线程修改集合。

If a thread is allowed to continue iterating over a collection which is simultaneously being modified the program behaviour would become non-deterministic and it would fail at an undetermined time leading into hard to find bugs. 如果允许线程继续迭代同时被修改的集合,则程序行为将变得不确定,并且它将在不确定的时间内失败,从而导致难以发现错误。 Hence, this exception is thrown by the Iterator and the program is said to have failed fast rather than failing later. 因此,此异常由Iterator引发,并且据说该程序快速失败而不是稍后失败。 Iterators that do this are known as fail-fast iterators. 执行此操作的迭代器称为快速失败迭代器。

first, your problem is probably in your check-function. 首先,您的问题可能出在您的检查功能上。

You could use some kind of lock to prevent a condition-check while playing music: 您可以使用某种锁来防止在播放音乐时进行条件检查:

ReentrantLock lock = new ReentrantLock();


Thread eventMusic = new Thread(new EventMusic());
lock.lock();
... check different conditions ... (every 0.02s)
if everything is ok  -> eventMusic.start();
lock.unlock();


public class EventMusic implements Runnable{
@Override
public void run() {

    try {
        lock.lock();
        Player p = new Player(new FileInputStream(getClass().getClassLoader().getResource("res/nameSong.mp3").getPath()));
        p.play();
    } catch (JavaLayerException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();}
    finally{
       lock.unlock();
    }

 }
}

You should place the lock somewhere so both classes can access it 您应该将锁放置在某个位置,以便两个类都可以访问它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM