Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 螺纹不';即使循环中不再满足条件,也不要停止_Java_Multithreading - Fatal编程技术网

Java 螺纹不';即使循环中不再满足条件,也不要停止

Java 螺纹不';即使循环中不再满足条件,也不要停止,java,multithreading,Java,Multithreading,作为标题,我创建了一个线程,并从下面的方法开始 该线程具有while条件while(OpponentsMap.get(type)!=null&&isPlayerAlive&&game.getFuel()>0.0) 我知道threadOpponentsMap.get(type)被更改为null,所以现在条件不应该满足,它仍然会永远循环。我错过了什么 public Thread newOpponent(Type type){ boolean valueSet = true; Poi

作为标题,我创建了一个线程,并从下面的方法开始

该线程具有while条件
while(OpponentsMap.get(type)!=null&&isPlayerAlive&&game.getFuel()>0.0)
我知道thread
OpponentsMap.get(type)
被更改为null,所以现在条件不应该满足,它仍然会永远循环。我错过了什么

public Thread newOpponent(Type type){
    boolean valueSet = true;
    Poi opponent = randomLocationOpponent(type);
    OpponentsMap.replace(type, opponent);

    SwingUtilities.invokeLater(new Runnable() { //Inform EDT about GUI changes
            @Override
            public void run() {
                if(OpponentsMap.get(type)!=null){
                    radar.addPoi(OpponentsMap.get(type));
                }
            }
    });

    Thread thread = new Thread(){
        @Override
        public void run(){
            double[] XYSpeed = getXYSpeedTowardsPlayer(type);

            while(OpponentsMap.get(type)!=null&&isPlayerAlive&&game.getFuel()>0.0){
                moveOpponent(type, XYSpeed[0], XYSpeed[1]);
                System.out.println(OpponentsMap.get(type).getName()+" distance: "+distanceToPlayer(OpponentsMap.get(type)));

                if(distanceToPlayer(OpponentsMap.get(type))>2) {
                    System.out.println("destroy");
                    destroyOpponent(type);
                }
                System.out.println("Is null: "+(OpponentsMap.get(type)==null)+" IsPlayerAlive: "+isPlayerAlive+" isFuelAbove0: "+(game.getFuel()>0.0) );
                try {
                    sleep(300);
                } catch (InterruptedException ex) {
                    Logger.getLogger(GameOpponents.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    };
    return thread;
}

private void destroyOpponent(Type type){
    OpponentsMap.replace(type,null);

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            if(OpponentsMap.get(type)!=null){
                radar.removePoi(OpponentsMap.get(type));

            }
        }
    });
}
更新
我尝试过使hashmap易失性,并将其更改为concurrenthashmap,但它仍然没有停止循环。

尝试使
OpponentsMap
易失性。@shmosel我做了,不幸的是,循环一直在进行,OpponentsMap是一个hashmap。首先,如果OpponentsMap.get(type)==null,循环肯定会中断。这里有几个重要因素1)类型不可变?(如果不是沿着行的某个地方,值正在更改,哈希将返回不同的bucket,其中值不为null可能是)2)可能是您的代码卡在了“double[]XYSpeed=getXYSpeedTowardsPlayer(type)”并且没有继续。这就是我现在所能想到的,然而,要得出任何结论,这都是不完整的信息。