繁体   English   中英

如何安全地遍历线程内的集合?

[英]how to do I safely iterate over a collection inside a thread?

我有一个Java / blazeds应用程序,其中包含一个数组“ _games ”,该数组是2个订阅了相同blazeds主题的玩家。 据我所知,这个问题少于40%的连接尝试成功了,到目前为止,我在调试该问题上的努力都没有成功-它似乎是无声的抛出,并且行为是随机的,除了第一对对似乎比连接更幸运随后的尝试。我已经看到了ConcurrentModificationException,并且在该主题上阅读的内容表明我的代码存在并发问题。

在这种情况下,我要遍历线程内的数组“ _games”,我想知道如何安全地执行此操作,因为其他客户端将尝试在随机时间访问此变量。

更新 #2:

在调试线程内的for循环时,似乎当前项始终相同,因此迭代器不会前进和/或被后续请求重置,因此我始终= 0,或者如果使用for(Game g:_games),那么g总是数组中的第一项。

谢谢!

public class GameService {

private static List<Game> _games;
private static GameServiceThread thread;


public GameService(MessageTemplate template) {
    this.template = template;
}

public Game CreateOrJoinGame(GamePlayer player)
{

    Game currentGame = new Game();
    boolean isFull = false;
    for (Game g: _games)
    {

        ArrayCollection myCollection = g.myCollection;
        currentGame = g;
        if(!g.IsFull())
        {
            myCollection.add(player);

            if(g.IsFull())
            {
                isFull = true;
                currentGame.myCollection = myCollection;
                System.out.print("User Count..." + myCollection.size() + "\n");
            }
            break;
        }

    }
    if(isFull)
    {
        return currentGame;
    }
    else
    {
        Game creator = CreateGame(player);
        return creator;
    }
}


public void start() {
     if (thread == null) {
        if(_games == null)
        {
          _games = new ArrayList<Game>();
        }        
            thread = new GameServiceThread(this.template);
            thread.start();
     }
}
public void AddPlayer(GamePlayer player)
{
    _allPlayers.add(player);
}

//嵌套的静态线程类

public static class GameServiceThread extends Thread {

        @Override
    public void run() {

        this.running = true;
        while (this.running) 
        {
            for (Game g: _games)
            {
                 //do work
            }

        }
     }


    private void sendGameUpdate(final Game game) {}

尽管我不认为这是我的问题,但该服务仅在flex-servlet中注册以获取信息。

<bean id="gameFeedService" class="com.acme.services.GameService">
    <constructor-arg ref="defaultMessageTemplate" />
    <flex:remoting-destination />
</bean>

编辑 :添加了一些有关如何添加新游戏的代码。 我在GameService类以及GameServiceThread内部遍历_games。 首先,当新客户将远程游戏对象发送到服务时,我在同一个集合中进行迭代,以向每个游戏接收者发送一条消息。 在每个游戏中都有一个集合,我可以使用该集合来确定游戏是否已满-是否创建了新的Game()。

您可以在列表本身上进行同步。

    @Override
public void run() {

    this.running = true;
    while (this.running) 
    {
        synchronized (_games) {
            for (Game g: _games) {
             //do work
            }
        }
    }
 }

您可能需要synchronize_games列表的访问。

这是docs.oracle.com上的同步教程。

您可以使用线程安全列表开始,例如:

List list = Collections.synchronizedList(new ArrayList(...));

检查ArrayList javadoc以获取更多信息。

synchronized(_games) {            
    for (Game g: _games)
    {
        //do work
    }
}

要么

synchronized(getClass()) {            
    for (Game g: _games)
    {
        //do work
    }
}

都可以。

根据您在sendGameUpdate中执行的操作,您可能还必须在同一对象上同步处理。

暂无
暂无

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

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