RedHoT Posted September 22, 2015 Posted September 22, 2015 When to use HashMap and ConcurrentHashMap Basic Example: private Map<String, Map<Integer, Integer>> _aviableBuffs; private FastMap<Integer, Map<String, List<Integer>>> _buffs; private Map<Integer, String> _activeSchemes; private Map<Integer, String> _activePetSchemes; private Map<Integer, List<String>> _modified; @SuppressWarnings("unused") private DataUpdater _dataUpdater; public EventBuffer() { _aviableBuffs = new FastMap<>(); _buffs = new FastMap<>(); _modified = new FastMap<>(); _activeSchemes = new FastMap<>(); _activePetSchemes = new FastMap<>(); _dataUpdater = new DataUpdater(); loadAviableBuffs(true); } Quote
*real*savormix Posted September 22, 2015 Posted September 22, 2015 (edited) This code snippet is missing critical information about the usage context. However, as it is NOW (considering it is coded correctly), you can safely change all FastMaps (which are thread-unsafe as they are initialized here) to HashMaps. You will lose the gradual memory increase and insertion-order iterations provided by FastMap, though. EDIT: if the current implementation fits the usage context (which you have decided to hide), you have no use of CHM in this snippet. Edited September 22, 2015 by _dev_ Quote
Tryskell Posted September 22, 2015 Posted September 22, 2015 (edited) FastMap is part of Javolution library, which is a Linked implementation of ConcurrentHashMap. Concurrency was reached using .shared(true) normally, but sometimes it needs, sometimes it doesn't need (still a mystery for me). ConcurrentHashMap are HashMap with the possibility of concurrent edition. E.g, if you add and remove in same time into a HashMap, you will end with a ConcurrentException. If you add and add, if you remove and remove, if you for loop and .remove( inside,... All those cases will throw an exception. The easiest is to run through HashMap, and if you got an issue, switch to ConcurrentHashMap. Without the use, it's hard to answer. Some maps which are supposed to be concurrent don't need concurrency due to how feature handles itself, how you coded it, or because it's processed too fast to be concurrent. Finally in your own scenario, you maybe will have to use LinkedHashMap / ConcurrentSkipListMap to keep sorting. Edited September 22, 2015 by Tryskell Quote
RedHoT Posted September 22, 2015 Author Posted September 22, 2015 This code snippet is missing critical information about the usage context. /* * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see <http://www.gnu.org/licenses/>. */ package cz.nxs.events.engine; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.logging.Level; import cz.nxs.events.NexusLoader; import cz.nxs.interf.PlayerEventInfo; import cz.nxs.interf.delegate.SkillData; import cz.nxs.l2j.CallBack; import javolution.text.TextBuilder; import javolution.util.FastList; import javolution.util.FastMap; import javolution.util.FastSet; /** * @author hNoke */ public class EventBuffer { private Map<String, Map<Integer, Integer>> _aviableBuffs; private FastMap<Integer, Map<String, List<Integer>>> _buffs; private Map<Integer, String> _activeSchemes; private Map<Integer, String> _activePetSchemes; private Map<Integer, List<String>> _modified; @SuppressWarnings("unused") private DataUpdater _dataUpdater; public EventBuffer() { _aviableBuffs = new FastMap<>(); _buffs = new FastMap<>(); _modified = new FastMap<>(); _activeSchemes = new FastMap<>(); _activePetSchemes = new FastMap<>(); _dataUpdater = new DataUpdater(); loadAviableBuffs(true); } public void reloadBuffer() { loadAviableBuffs(false); } public void loadPlayer(PlayerEventInfo player) { loadData(player.getPlayersId()); } public void buffPlayer(PlayerEventInfo player, boolean heal) { for (int buffId : getBuffs(player)) { player.getSkillEffects(buffId, getLevelFor(buffId)); } if (heal) { player.setCurrentHp(player.getMaxHp()); player.setCurrentMp(player.getMaxMp()); player.setCurrentCp(player.getMaxCp()); } } public void buffPlayer(PlayerEventInfo player) { for (int buffId : getBuffs(player)) { player.getSkillEffects(buffId, getLevelFor(buffId)); } if (EventConfig.getInstance().getGlobalConfigBoolean("bufferHealsPlayer")) { player.setCurrentHp(player.getMaxHp()); player.setCurrentMp(player.getMaxMp()); player.setCurrentCp(player.getMaxCp()); } } public void buffPet(PlayerEventInfo player) { if (player.hasPet()) { if (getPlayersCurrentPetScheme(player.getPlayersId()) == null) return; for (int buffId : getBuffs(player, getPlayersCurrentPetScheme(player.getPlayersId()))) { player.getPetSkillEffects(buffId, getLevelFor(buffId)); } } } public void addModifiedBuffs(PlayerEventInfo player, String schemeName) { if (!_modified.containsKey(player.getPlayersId())) _modified.put(player.getPlayersId(), new FastList<String>()); if (!_modified.get(player.getPlayersId()).contains(schemeName)) _modified.get(player.getPlayersId()).add(schemeName); } public void addModifiedBuffs(int player, String schemeName) { if (!_modified.containsKey(player)) _modified.put(player, new FastList<String>()); if (!_modified.get(player).contains(schemeName)) _modified.get(player).add(schemeName); } public boolean hasBuffs(PlayerEventInfo player) { try { return !_buffs.get(player.getPlayersId()).isEmpty(); } catch (Exception e) { return false; } } public boolean addScheme(PlayerEventInfo player, String schemeName) { if (player == null) return false; if (_buffs.get(player.getPlayersId()).containsKey(schemeName)) return false; if (_buffs.get(player.getPlayersId()).size() >= 6) { player.sendMessage("You can't have more than 6 schemes."); return false; } _buffs.get(player.getPlayersId()).put(schemeName, new FastList<Integer>()); setPlayersCurrentScheme(player.getPlayersId(), schemeName); addModifiedBuffs(player, schemeName); return true; } public boolean removeScheme(PlayerEventInfo player, String schemeName) { if (!_buffs.get(player.getPlayersId()).containsKey(schemeName)) return false; _buffs.get(player.getPlayersId()).remove(schemeName); if (schemeName.equals(getPlayersCurrentScheme(player.getPlayersId()))) setPlayersCurrentScheme(player.getPlayersId(), getFirstScheme(player.getPlayersId())); addModifiedBuffs(player, schemeName); return true; } public String getPlayersCurrentScheme(int player) { String current = _activeSchemes.get(player); if (current == null) current = setPlayersCurrentScheme(player, getFirstScheme(player)); return current; } public String getPlayersCurrentPetScheme(int player) { return _activePetSchemes.get(player); } public String setPlayersCurrentScheme(int player, String schemeName) { return setPlayersCurrentScheme(player, schemeName, true); } public String setPlayersCurrentScheme(int player, String schemeName, boolean updateInDb) { if (schemeName == null) { _activeSchemes.remove(player); return null; } if (!_buffs.get(player).containsKey(schemeName)) _buffs.get(player).put(schemeName, new FastList<Integer>()); if (updateInDb) { if (_activeSchemes.containsKey(player)) addModifiedBuffs(player, _activeSchemes.get(player)); addModifiedBuffs(player, schemeName); } _activeSchemes.put(player, schemeName); return schemeName; } public String setPlayersCurrentPetScheme(int player, String schemeName) { if (schemeName == null) { _activePetSchemes.remove(player); return null; } if (!_buffs.get(player).containsKey(schemeName)) _buffs.get(player).put(schemeName, new FastList<Integer>()); _activePetSchemes.put(player, schemeName); return schemeName; } public String getFirstScheme(int player) { if (_buffs.containsKey(player)) { for (Entry<String, List<Integer>> e : _buffs.get(player).entrySet()) { return e.getKey(); } } return null; } public Set<Entry<String, List<Integer>>> getSchemes(PlayerEventInfo player) { if (_buffs.containsKey(player.getPlayersId())) { return _buffs.get(player.getPlayersId()).entrySet(); } return new FastSet<>(); } public boolean addBuff(int buffId, PlayerEventInfo player) { String scheme = getPlayersCurrentScheme(player.getPlayersId()); if (scheme == null) return false; if (!_buffs.get(player.getPlayersId()).containsKey(scheme)) return false; if (_buffs.get(player.getPlayersId()).get(scheme).contains(buffId)) return false; _buffs.get(player.getPlayersId()).get(scheme).add(buffId); addModifiedBuffs(player, scheme); return true; } public void removeBuff(int buffId, PlayerEventInfo player) { String scheme = getPlayersCurrentScheme(player.getPlayersId()); if (scheme == null) return; _buffs.get(player.getPlayersId()).get(scheme).remove(new Integer(buffId)); addModifiedBuffs(player, scheme); } public boolean containsSkill(int buffId, PlayerEventInfo player) { String scheme = getPlayersCurrentScheme(player.getPlayersId()); if (scheme == null) return false; return _buffs.get(player.getPlayersId()).get(scheme).contains(buffId); } public List<Integer> getBuffs(PlayerEventInfo player) { String scheme = getPlayersCurrentScheme(player.getPlayersId()); if (scheme == null) return new FastList<>(); return _buffs.get(player.getPlayersId()).get(scheme); } public List<Integer> getBuffs(PlayerEventInfo player, String scheme) { return _buffs.get(player.getPlayersId()).get(scheme); } private void loadData(int playerId) { synchronized (_buffs) { _buffs.put(playerId, new FastMap<String, List<Integer>>()); PreparedStatement statement = null; try (Connection con = CallBack.getInstance().getOut().getConnection()) { statement = con.prepareStatement("SELECT * FROM nexus_playerbuffs WHERE playerId = " + playerId); ResultSet rset = statement.executeQuery(); while (rset.next()) { String scheme = rset.getString("scheme"); int active = rset.getInt("active"); _buffs.get(playerId).put(scheme, new FastList<Integer>()); for (String buffId : rset.getString("buffs").split(",")) { try { _buffs.get(playerId).get(scheme).add(Integer.parseInt(buffId)); } catch (Exception e) { continue; } } if (active == 1) setPlayersCurrentScheme(playerId, scheme, false); } rset.close(); statement.close(); } catch (Exception e) { e.printStackTrace(); } } } protected synchronized void storeData() { if (_modified.isEmpty()) return; PreparedStatement statement = null; String buffs; try (Connection con = CallBack.getInstance().getOut().getConnection()) { TextBuilder tb; for (Entry<Integer, List<String>> modified : _modified.entrySet()) { for (String modifiedScheme : modified.getValue()) { statement = con.prepareStatement("DELETE FROM nexus_playerbuffs WHERE playerId = " + modified.getKey() + " AND scheme = '" + modifiedScheme + "'"); statement.execute(); if (_buffs.get(modified.getKey()).containsKey(modifiedScheme)) { tb = new TextBuilder(); for (int buffId : _buffs.get(modified.getKey()).get(modifiedScheme)) { tb.append(buffId + ","); } buffs = tb.toString(); if (buffs.length() > 0) buffs = buffs.substring(0, buffs.length() - 1); statement = con.prepareStatement("REPLACE INTO nexus_playerbuffs VALUES (?,?,?,?)"); statement.setInt(1, modified.getKey()); statement.setString(2, modifiedScheme); statement.setString(3, buffs); statement.setInt(4, modifiedScheme.equals(getPlayersCurrentScheme(modified.getKey())) ? 1 : 0); statement.executeUpdate(); statement.close(); } } } } catch (Exception e) { e.printStackTrace(); } _modified.clear(); } public Map<String, Map<Integer, Integer>> getAviableBuffs() { return _aviableBuffs; } public int getLevelFor(int skillId) { for (Map<Integer, Integer> e : _aviableBuffs.values()) { for (Entry<Integer, Integer> entry : e.entrySet()) { if (entry.getKey() == skillId) return entry.getValue(); } } return -1; } private void loadAviableBuffs(boolean test) { if (!_aviableBuffs.isEmpty()) _aviableBuffs.clear(); String category; int buffId, level; int count = 0; String name; PreparedStatement statement = null; try (Connection con = CallBack.getInstance().getOut().getConnection()) { statement = con.prepareStatement("SELECT * FROM nexus_buffs"); ResultSet rset = statement.executeQuery(); while (rset.next()) { category = rset.getString("category"); buffId = rset.getInt("buffId"); level = rset.getInt("level"); if (test) { name = rset.getString("name"); if (name == null || name.length() == 0) { try { name = new SkillData(buffId, level).getName(); if (name != null) { PreparedStatement statement2 = con.prepareStatement("UPDATE nexus_buffs SET name = '" + name + "' WHERE buffId = " + buffId + " AND level = " + level + ""); statement2.execute(); statement2.close(); } } catch (Exception e) { } } } if (!_aviableBuffs.containsKey(category)) _aviableBuffs.put(category, new FastMap<Integer, Integer>()); _aviableBuffs.get(category).put(buffId, level); count++; } rset.close(); statement.close(); } catch (Exception e) { e.printStackTrace(); } NexusLoader.debug("Loaded " + count + " buffs for Event Buffer.", Level.INFO); } private class DataUpdater implements Runnable { protected DataUpdater() { // CallBack.getInstance().getOut().scheduleGeneralAtFixedRate(this, 600000, 600000); CallBack.getInstance().getOut().scheduleGeneralAtFixedRate(this, 10000, 10000); } @Override public void run() { storeData(); } } public static EventBuffer getInstance() { return SingletonHolder._instance; } private static class SingletonHolder { protected static final EventBuffer _instance = new EventBuffer(); } } Quote
xxdem Posted September 23, 2015 Posted September 23, 2015 (edited) You don't need concurrent hashmaps on this case scenario, except if the coder was lazy enough to fix the concurrent exception he was getting with a ConcurrentHashmap hotfix, bad practice and bad code You should use ConcurrentHashMap when you want to populate data while iterating the map which essentially is considered a bad practice Edited September 23, 2015 by xxdem Quote
Tryskell Posted September 23, 2015 Posted September 23, 2015 (edited) http://stackoverflow.com/questions/12628600/java-synchronized-block-vs-concurrenthashmap-vs-collections-synchronizedmap Unlike xxdem says, ConcurrentHashMap is useful on numerous places, and since java8 CHM has been improved by A LOT, so even Javolution is out of business (I guess they introduced that librarie only for performance case, when Java 1.6 sucked). I give a simple example : a Map storing players registering to an event. It has to be concurrent because 2 players can add/remove themselves in same time (until you force a unique player to access the NPC at a time, that's the type of scenario I described higher). You can play with synchronized keyword, creating blocks on add/remove methods, but it's just a waste of time since CHM makes the exact same work, except you're sure to don't forget a location. Anyway, as a resume : HashMap / ConcurrentHashMap -------------------> basic / concurrent version. LinkedHashMap / ConcurrentSkipListMap -------> basic sorted / sorted concurrent concurrent > thread-safe, different threads can access it in same time without affecting general output. sorted > automatic sorting based on key or a Comparator (points based, name based sorting, buffs, skills). If you want to move from Javolution to Java, I would say to use basic first, and concurrent version if you got a ConcurrentException. Sorted is up to your use, avoid it if you got no needs. Edited September 23, 2015 by Tryskell Quote
xxdem Posted September 23, 2015 Posted September 23, 2015 (edited) not true, no way possible a player can register/remove from an event map/array simutaniously so you don't need concurrency here, concurrenthashmap is used for multithreaded population of data, not a case in l2j unless you're shitcoding Edited September 23, 2015 by xxdem Quote
*real*savormix Posted September 23, 2015 Posted September 23, 2015 (edited) Disregard this, I cba to check how Nexus EE works. Edited September 23, 2015 by _dev_ Quote
Tryskell Posted September 23, 2015 Posted September 23, 2015 not true, no way possible a player can register/remove from an event map/array simutaniously so you don't need concurrency here, concurrenthashmap is used for multithreaded population of data, not a case in l2j unless you're shitcoding I speak about 2 players. Not one. It has to be concurrent because 2 players can add/remove themselves in same time PS for topic creator : aviable > available... Quote
RedHoT Posted September 23, 2015 Author Posted September 23, 2015 (edited) Thanks for all. PS for topic creator : aviable > available... This code is part of Nexus Event Engine. Anyway, I will to fix this "error" =) Edited September 23, 2015 by RedHoT Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.