簡體   English   中英

迭代HashMap一次,沒有重復

[英]Iterate HashMap one full time, with no repeats

我的目標:

  • 讓它更加規范化。 (更相似的等待時間 - 線性增長)
  • 擴展到XXX甚至可能是XXXX的怪物數量和npcs。
  • 讓你們所有的Java大師都給我一些想法:D

我的問題如下:

  • 它從未達到第二次迭代循環(npc)
  • “浪費”時間過於隨意; 將有數百(如果不是更多)的mobs / npcs迭代,這個解決方案根本不會擴展
  • 我的服務器將在主循環中執行許多其他“事件”,其中一些使用相同的HashMaps,因此使用ConcurrentHashMap(計算命中損壞/等)

代碼:我希望這是足夠的SSCCE。 我試圖盡可能地削減脂肪......

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

public class WaitTime {

    static ConcurrentHashMap<String, Integer> mobInstanceMap = new ConcurrentHashMap<String, Integer>();
    static ConcurrentHashMap<String, Integer> npcInstanceMap = new ConcurrentHashMap<String, Integer>();

    public static void main(String[] args){
        mobInstanceMap.put("mob1", 0);
        mobInstanceMap.put("mob2", 0);
        mobInstanceMap.put("mob3", 0);
        npcInstanceMap.put("npc1", 0);
        npcInstanceMap.put("npc2", 0);
        npcInstanceMap.put("npc3", 0);
        while(true){
            updateEntityLocations();
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private static void updateEntityLocations() {
        long entityMovementLoopStartTime = System.nanoTime();
        Iterator<Entry<String, Integer>> it = mobInstanceMap.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, Integer> mobEntity = it.next();
            String mobName = mobEntity.getKey();
            int lastWalkTime = mobEntity.getValue();
            int mobWalkSpeed = 4000;
            long walkWaitTime = lastWalkTime;
            long elapsedTime = (long) ((System.nanoTime() - entityMovementLoopStartTime) / 100.0);
            walkWaitTime += elapsedTime;

            if (walkWaitTime >= mobWalkSpeed){
                System.out.println("Wasted time(walking)(" + mobName + "): " + (walkWaitTime - mobWalkSpeed));

                //mobInstanceMap.put(mobName, 0);
                mobInstanceMap.replace(mobName, 0);
            } else {  //!(walkWaitTime >= walkSpeed)
                //mobInstanceMap.put(mobName, (int) walkWaitTime);
                mobInstanceMap.replace(mobName, (int) walkWaitTime);
            }
        }

        Iterator<Entry<String, Integer>> it1 = npcInstanceMap.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, Integer> npcEntity = it1.next();
            String npcCoords = npcEntity.getKey();
            int lastWalkTime = npcEntity.getValue();
            int npcWalkSpeed = 4000;
            long walkWaitTime = lastWalkTime;
            long elapsedTime = (long) ((System.nanoTime() - entityMovementLoopStartTime) / 100.0);
            walkWaitTime += elapsedTime;

            if (walkWaitTime >= npcWalkSpeed){
                System.out.println("Wasted time(walking)(" + npcCoords + "): " + (walkWaitTime - npcWalkSpeed));

                npcInstanceMap.put(npcCoords, 0);
            } else {  //!(walkWaitTime >= walkSpeed)
                npcInstanceMap.put(npcCoords, (int) walkWaitTime);
            }
        }   
    }
}

安慰:

Wasted time(walking)(mob2): 58
Wasted time(walking)(mob1): 1983
Wasted time(walking)(mob3): 2288
Wasted time(walking)(mob3): 266
Wasted time(walking)(mob1): 122
Wasted time(walking)(mob3): 232
Wasted time(walking)(mob2): 23
Wasted time(walking)(mob1): 674
Wasted time(walking)(mob3): 27
Wasted time(walking)(mob1): 159
Wasted time(walking)(mob3): 1723
Wasted time(walking)(mob2): 119
Wasted time(walking)(mob1): 676
Wasted time(walking)(mob3): 1698
Wasted time(walking)(mob3): 3983
Wasted time(walking)(mob1): 182

正如你所看到的,如果用put(注釋掉)來運行它而不是替換它運行得稍微慢一些,而且更不穩定。

你會踢自己,但第二個循環引用第一個迭代器。

Iterator<Entry<String, Integer>> it1 = npcInstanceMap.entrySet().iterator();
while (it.hasNext()) {
    ...
}

另外,正如這個答案所暗示的那樣,我建議不要使用迭代器。

for(String mob : mobInstanceMap.keySet()){
    String mobName = mob;
    int lastWalkTime = mobInstanceMap.get(mob);
    int mobWalkSpeed = 4000;
    long walkWaitTime = lastWalkTime;
    long elapsedTime = (long) ((System.nanoTime() - entityMovementLoopStartTime) / 100.0);
    walkWaitTime += elapsedTime;

    if (walkWaitTime >= mobWalkSpeed){
        System.out.println("Wasted time(walking)(" + mobName + "): " + (walkWaitTime - mobWalkSpeed));

        mobInstanceMap.put(mobName, 0);
    } else {  //!(walkWaitTime >= walkSpeed)
        mobInstanceMap.put(mobName, (int) walkWaitTime);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM