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

    • ⚡ Weekend Stock Refresh! Fast order fulfillment is active. Get Gemini Pro Advanced + Google One cloud storage activated on your own personal email within minutes.
    • New Release World & atmosphere A complete dynamic weather system has arrived, with Clear, Light Rain, Rain, Heavy Rain, Storm, Snow, and Windy presets. Rain and snow react to roofs, wind moves foliage, lightning illuminates storms, exposed ground becomes wet and develops puddles, and snow builds up and melts over time. Weather includes Low to Ultra quality levels and advanced controls for precipitation, wind, cloud cover, lightning, surface wetness, puddles, and snow. The game server can also select or restore the current weather while local quality settings continue to control rendering cost. The weather can be controlled from the server with custom packets. For more details use Shift+Alt+P. Enhanced lighting now allows authored lamps, fires, and other light sources to illuminate towns and interiors. Lights can pulse, flicker, blink, and strobe, flames produce warm dynamic light, and lamps display depth-aware halos. Remastered emissive surfaces are now supported, beginning with illuminated windows in Giran that glow and cast light into their surroundings. Broadcasting Towers now use their correct original scale and animated rings and glow effects. Map, quests & navigation A complete quest journal has been added, with active and completed quests, expandable stages, descriptions, requirements, item progress, quest inventory integration, location controls, and quest cancellation. Quest objectives now appear through flashing minimap and world-map markers, an overhead directional guide, a notification button beside chat, and rotating quest symbols above eligible NPCs. Server-directed radar markers now work across the live minimap and world map, including the original overhead guidance effect and arrival behavior. The original tutorial system now works, including scrollable tutorial pages, animated illustrations, the flashing question button, tutorial links, and movement, camera, pickup, and sit/rest progression events. Server-opened town maps now display their original artwork and the supplied player marker. Shops, storage & items Personal, clan, castle, and freight warehouses now work, including deposits, withdrawals, stack quantities, fees, weight and capacity previews, tooltips, and the original warning and empty-storage dialogs. Account freight now works through the original recipient picker and delivery window, allowing items to be sent to another character on the same account. Multisell exchanges now work, with paged recipe lists, product details, required materials, quantity selection, warnings, and server-backed completion. Manor seed purchasing is now supported through the shop interface, including availability and pricing. Inventory items can now be dragged to the trash button and destroyed after the appropriate warning or quantity prompt. The original in-game calculator now works with mouse and keyboard input. Community The Community Board is now available from the system menu or Alt+B, with Home, Favorites, Region, Clan, Memo, Mail, and Friends sections, retained navigation, forms, scrolling, minimization, and server-provided pages. Characters, cameras & events Striders and Wyverns now render as fully animated mounts with the rider attached correctly. Striders remain ground-based, while Wyverns support three-dimensional flight. Characters now enter a proper falling state after high drops or airborne dismounts, with falling animations and server-authoritative landing and fall-damage handling. Players can now sit on authored world chairs using the correct sit, wait, and stand animations. Server-driven special camera sequences now work, including target tracking, interpolation, duration, widescreen bars, and restoration to the normal camera. GM camera mode and server-authorized ///fly movement are now supported. The original 102-second Kamael teaser sequence has been restored with its complete slideshow, transitions, panning artwork, localized subtitles, and narration. Hats, masks, circlets, and other hair accessories now use their authored head placement and correctly adjust the character's hairstyle. Timed server confirmation dialogs, short-duration status effects, regeneration previews, and the seasonal Christmas Seal presentation are now supported. Interface & performance Character creation has been refined with corrected description wrapping, clipping, layout, and name-entry presentation. Tutorial and NPC dialog windows now use polished proportional scrollbars, improved HTML layout, and correctly sized illustrations and controls. A new independent 3D render-resolution setting offers Native, 720p, 900p, 1080p, 1440p, and 4K choices while keeping the window, HUD, text, and FermaUI at native resolution. Major rendering optimizations reduce unnecessary work in local lighting, shadows, visibility, exposure, bloom, post-processing, and GPU diagnostics. Download from the launcher you have installed, or at https://updates.fermata.gg/ if you don't have the launcher. Fermata Light Engine Preview All lights can be baked in the client, shipped to the users, and exported and imported as .femlight files.
  • Topics

×
×
  • Create New...

Important Information

This community uses essential cookies to function properly. Non-essential cookies and third-party services are used only with your consent. Read our Privacy Policy and We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue..