Jump to content

Recommended Posts

Posted

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);
	}
	

Posted (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 by _dev_
Posted (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 by Tryskell
Posted

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();
	}
}

 

 

Posted (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 by xxdem
Posted (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 by Tryskell
Posted (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 by xxdem
Posted

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...

Posted (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 by RedHoT

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Posts

    • L2 Kings    Stage 1 – The Awakening Dynasty and Moirai Level Cap: 83 Gear: Dynasty -Moirai & Weapons (Shop for Adena + Drop from mobs/instances ) Masterwork System: Available (Neolithics S required with neolithics u can do armor parts foundation aswell) Class Cloaks: Level 1 - Masterwork sets such us moirai/dynasty stats are boosted also vesper(stage 2) Olf T-Shirt: +6 (fails don’t reset) safe is +2 Dolls: Level 1 Belts: Low & Medium Enchant: Safe +3 / Max +8 / Attribution Easy in Moirai-Dynasty . Main Zones: Varka Outpost: Easy farm, Adena, EXP for new players = > 80- 100kk hour Dragon Valley: Main farm zone — , 100–120kk/hour Weapon Weakness System active (all classes can farm efficiently) Archers get vampiric auto-hits vs mobs Dragon Valley Center: Main Party Zone — boosted drops (Blessed enchants, Neolithics chance) => farm like 150-200kk per hour. Dragon Valley North: Spoil Zone (Asofe + crafting materials for MW) Primeval Isle: Safe autofarm zone (low adena for casual players) ==> 50kk per hour Forge of the Gods & Imperial Tomb: Available from Stage 1 (lower Adena reward in compare with Dragon Valley) Hellbound also avaliable from stage 1 In few words all zones opened but MAIN farm zone with boosted adena and drops is Dragon valley also has more mobs Instances: Zaken (24h Reuse) → Instead of Vespers drop Moirai , 100% chance to drop 1 of 9 dolls lvl 1, Zaken 7-Day Jewelry Raid Bosses (7 RBs): Drop Moirai Parts + Neolithic S grade instead of Vespers parts that has 7 Rb Quest give Icarus Weapons Special Feature 7rb bosses level up soul crystals aswell. Closed Areas : Monaster of SIlence, LOA, ( It wont have mobs) / Mahum Quest/Lizardmen off) Grand Epics: Unlocked on Day 4 of Stage 1 → Antharas, Valakas, Baium, AQ, etc ================================================================================= Stage 2 – Rise of Vespers Level Cap: 85 Gear: Moirai Armors (Adena GM SHOP / Craft/ Drop) Weapons: Icarus Cloaks: Level 2 Olf: +8 Dolls: Level 2 Belts: High & Top Enchant: Safe +3 / Max +8 Masterwork can be with Neolithics S84 aswell but higher so craft will be usefull aswell. 7 Raid Boss Quest Updated: Now works retail give vesper weapons 7rb Bosses Drops : Vespers Instances: Zaken : Drops to retail vespers + the dolls and the extra items that we added on stage 1 New Freya Instance: Added — drops vespers and instead of mid s84 weapons will drop vespers . Extra drops Blessed Bottle of Freya - drops 100% chance 1 of 9 dolls. Farm Areas Dragon Valley remains main farm New Zone : Lair of Antharas (mobs nerfed and added drop Noble stone so solo players can farm too) New Party Zone : LOA Circle   ============================================================================   Stage 3 – The Vorpal ERA Gear: Vorpal Unclock Cloaks: Level 3 Olf: +10 (max cap) Dolls: Level 3 Enchant: Safe +3 / Max +12 Farm Zones : Dragon Valley Center Scorpions becomes a normal solo zone (no longer party zone) Drops:   LOA & Knorik → Mid Weapons avaliable in drop New Party Zone Kariks Instances: Easy Freya Drops Mid Weapons Frintezza Release =================================================================================     Stage 4 – Elegia Era (Final Stage) Elegia Unlock Gear: Elegia Weapons: Elegia TOP s84 ( farmed via H-Freya/ Drops ) Cloaks: Level 5 Dolls: Level 3 (final bonuses) Enchant: Safe +6 / Max +16 Instances: Hard Freya → Drops Elegia Weapons + => The Instance will drop 2-3 parts for sure and also will be able to Join with 7 people . Party Zone will have also drop chances for elegia armor parts and weapons but small   Events (Hourly): Win: 50 Event Medals + 3 GCM + morewards Lose: 25 Medals + 1 GCM + more rewards Tie: 30 Medals + 2 GCM + more rewards   ================================================================================ Epic Fragments Currency Participating in Daily Bosses mass rewarding all players Participating in Instances (zaken freya frintezza etc) all players get reward ================================================================================ Adena - Main server currency (all items in gm shop require adena ) Event Medals (Festival Adena) - Event shop currency Donation coins you can buy with them dressme,cosmetics and premium account Epic Fragments you can buy with them fake epic jewels Olympiad Tokens you can buy many items from olympiad shop (Hero Coin even items that are on next stages) Olympiad Win = 1000 Tokens / Lose = 500 Tokens ================================================================================= Offline Autofarm Allows limited Offline farming requires offline autofarm ticket that you get by voting etc ================================================================================= Grand Epics have Specific Custom NPC that can spawn Epics EU/LATIN TIME ZONE ================================================================================= First Olympiad Day 19 December First Heroes 22 December ( 21 December Last day of 1st Period) After that olympiad will be weekly. ================================================================================= Item price and economy Since adena is main coin of server and NOT donation coins we will always add new items in gm shop with adena in order to burn the adena of server and not be inflation . =================================================================================        
    • Hello, I'd like to change a title color for custom npc.  I created custom NPC, cloned existing. I put unique id for it in npcname-e, npcgrp and database. I have "0" to serverSideName in db, so that it would use npcname-e, but instead it has "NoNameNPC"and no title color change.
    • Trusted Guy 100% ,  I asked him for some work and he did it right away.
  • Topics

×
×
  • Create New...

AdBlock Extension Detected!

Our website is made possible by displaying online advertisements to our members.

Please disable AdBlock browser extension first, to be able to use our community.

I've Disabled AdBlock