Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: linkedlist concurrentmodificationexception

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default linkedlist concurrentmodificationexception

    I was using javolution before but my team decided to drop it. Since then we got a concurrentmodificationexception on
    "LinkedList<AbstractNodeLoc> path = new LinkedList<>();" if i revert it to fastlist it works great. What makes the difference?
    public class CellPathFinding extends PathFinding
    {
    	private static final Logger _log = Logger.getLogger(CellPathFinding.class.getName());
    	private BufferInfo[] _allBuffers;
    	private int _findSuccess = 0;
    	private int _findFails = 0;
    	private int _postFilterUses = 0;
    	private int _postFilterPlayableUses = 0;
    	private int _postFilterPasses = 0;
    	private long _postFilterElapsed = 0;
     
    	private List<L2ItemInstance> _debugItems = null;
     
    	public static CellPathFinding getInstance()
    	{
    		return SingletonHolder._instance;
    	}
     
    	protected CellPathFinding()
    	{
    		try
    		{
    			String[] array = Config.PATHFIND_BUFFERS.split(";");
     
    			_allBuffers = new BufferInfo[array.length];
     
    			String buf;
    			String[] args;
    			for (int i = 0; i < array.length; i++)
    			{
    				buf = array[i];
    				args = buf.split("x");
    				if (args.length != 2)
    					throw new Exception("Invalid buffer definition: " + buf);
     
    				_allBuffers[i] = new BufferInfo(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
    			}
    			_log.info("CellPathFinding: buffers have been loaded.");
    		}
    		catch (Exception e)
    		{
    			_log.log(Level.WARNING, "CellPathFinding: Problem during buffer init: " + e.getMessage(), e);
    			throw new Error("CellPathFinding: loading have been aborted.");
    		}
    	}
     
    	@Override
    	public boolean pathNodesExist(short regionoffset)
    	{
    		return false;
    	}
     
    	@Override
    	public List<AbstractNodeLoc> findPath(int x, int y, int z, int tx, int ty, int tz, boolean playable)
    	{
    		int gx = (x - L2World.MAP_MIN_X) >> 4;
    		int gy = (y - L2World.MAP_MIN_Y) >> 4;
    		if (!GeoData.getInstance().hasGeo(x, y))
    			return null;
    		short gz = GeoData.getInstance().getHeight(x, y, z);
    		int gtx = (tx - L2World.MAP_MIN_X) >> 4;
    		int gty = (ty - L2World.MAP_MIN_Y) >> 4;
    		if (!GeoData.getInstance().hasGeo(tx, ty))
    			return null;
    		short gtz = GeoData.getInstance().getHeight(tx, ty, tz);
    		CellNodeBuffer buffer = alloc(64 + 2 * Math.max(Math.abs(gx - gtx), Math.abs(gy - gty)), playable);
    		if (buffer == null)
    			return null;
     
    		boolean debug = playable && Config.DEBUG_PATH;
     
    		if (debug)
    		{
    			if (_debugItems == null)
    				_debugItems = new ArrayList<>();
    			else
    			{
    				for (L2ItemInstance item : _debugItems)
    				{
    					if (item == null)
    						continue;
    					item.decayMe();
    				}
     
    				_debugItems.clear();
    			}
    		}
     
    		List<AbstractNodeLoc> path = null;
    		try
    		{
    			CellNode result = buffer.findPath(gx, gy, gz, gtx, gty, gtz);
     
    			if (debug)
    			{
    				for (CellNode n : buffer.debugPath())
    				{
    					if (n.getCost() < 0) // calculated path
    						dropDebugItem(1831, (int) (-n.getCost() * 10), n.getLoc());
    					else
    						// known nodes
    						dropDebugItem(57, (int) (n.getCost() * 10), n.getLoc());
    				}
    			}
     
    			if (result == null)
    			{
    				_findFails++;
    				return null;
    			}
     
    			path = constructPath(result);
    		}
    		catch (Exception e)
    		{
    			_log.log(Level.WARNING, "", e);
    			return null;
    		}
    		finally
    		{
    			buffer.free();
    		}
     
    		if (path.size() < 3 || Config.MAX_POSTFILTER_PASSES <= 0)
    		{
    			_findSuccess++;
    			return path;
    		}
     
    		long timeStamp = System.currentTimeMillis();
    		_postFilterUses++;
    		if (playable)
    			_postFilterPlayableUses++;
     
    		int currentX, currentY, currentZ;
    		ListIterator<AbstractNodeLoc> middlePoint, endPoint;
    		AbstractNodeLoc locMiddle, locEnd;
    		boolean remove;
    		int pass = 0;
    		do
    		{
    			pass++;
    			_postFilterPasses++;
     
    			remove = false;
    			middlePoint = path.listIterator();
    			endPoint = path.listIterator(1);
    			locEnd = null;
    			currentX = x;
    			currentY = y;
    			currentZ = z;
     
    			while (endPoint.hasNext())
    			{
    				locEnd = endPoint.next();
    				locMiddle = middlePoint.next();
    				if (GeoData.getInstance().canMoveFromToTarget(currentX, currentY, currentZ, locEnd.getX(), locEnd.getY(), locEnd.getZ()))
    				{
    					middlePoint.remove();
    					remove = true;
    					if (debug)
    						dropDebugItem(735, 1, locMiddle);
    				}
    				else
    				{
    					currentX = locMiddle.getX();
    					currentY = locMiddle.getY();
    					currentZ = locMiddle.getZ();
    				}
    			}
    		}
    		// only one postfilter pass for AI
    		while (playable && remove && path.size() > 2 && pass < Config.MAX_POSTFILTER_PASSES);
     
    		if (debug)
    		{
    			middlePoint = path.listIterator();
    			while (middlePoint.hasNext())
    			{
    				locMiddle = middlePoint.next();
    				dropDebugItem(65, 1, locMiddle);
    			}
    		}
     
    		_findSuccess++;
    		_postFilterElapsed += System.currentTimeMillis() - timeStamp;
    		return path;
    	}
     
    	private static List<AbstractNodeLoc> constructPath(AbstractNode node)
    	{
    		LinkedList<AbstractNodeLoc> path = new LinkedList<>();
    		int previousDirectionX = Integer.MIN_VALUE;
    		int previousDirectionY = Integer.MIN_VALUE;
    		int directionX, directionY;
     
    		while (node.getParent() != null)
    		{
    			if (!Config.ADVANCED_DIAGONAL_STRATEGY && node.getParent().getParent() != null)
    			{
    				int tmpX = node.getLoc().getNodeX() - node.getParent().getParent().getLoc().getNodeX();
    				int tmpY = node.getLoc().getNodeY() - node.getParent().getParent().getLoc().getNodeY();
    				if (Math.abs(tmpX) == Math.abs(tmpY))
    				{
    					directionX = tmpX;
    					directionY = tmpY;
    				}
    				else
    				{
    					directionX = node.getLoc().getNodeX() - node.getParent().getLoc().getNodeX();
    					directionY = node.getLoc().getNodeY() - node.getParent().getLoc().getNodeY();
    				}
    			}
    			else
    			{
    				directionX = node.getLoc().getNodeX() - node.getParent().getLoc().getNodeX();
    				directionY = node.getLoc().getNodeY() - node.getParent().getLoc().getNodeY();
    			}
     
    			// only add a new route point if moving direction changes
    			if (directionX != previousDirectionX || directionY != previousDirectionY)
    			{
    				previousDirectionX = directionX;
    				previousDirectionY = directionY;
     
    				path.addFirst(node.getLoc());
    				node.setLoc(null);
    			}
     
    			node = node.getParent();
    		}
     
    		return path;
    	}
     
    	private final CellNodeBuffer alloc(int size, boolean playable)
    	{
    		CellNodeBuffer current = null;
    		for (BufferInfo i : _allBuffers)
    		{
    			if (i.mapSize >= size)
    			{
    				for (CellNodeBuffer buf : i.bufs)
    				{
    					if (buf.lock())
    					{
    						i.uses++;
    						if (playable)
    							i.playableUses++;
    						i.elapsed += buf.getElapsedTime();
    						current = buf;
    						break;
    					}
    				}
    				if (current != null)
    					break;
     
    				// not found, allocate temporary buffer
    				current = new CellNodeBuffer(i.mapSize);
    				current.lock();
    				if (i.bufs.size() < i.count)
    				{
    					i.bufs.add(current);
    					i.uses++;
    					if (playable)
    						i.playableUses++;
    					break;
    				}
     
    				i.overflows++;
    				if (playable)
    					i.playableOverflows++;
    			}
    		}
     
    		return current;
    	}
     
    	private final void dropDebugItem(int itemId, int num, AbstractNodeLoc loc)
    	{
    		final L2ItemInstance item = new L2ItemInstance(IdFactory.getInstance().getNextId(), itemId);
    		item.setCount(num);
    		item.spawnMe(loc.getX(), loc.getY(), loc.getZ());
    		_debugItems.add(item);
    	}
     
    	private static final class BufferInfo
    	{
    		final int mapSize;
    		final int count;
    		ArrayList<CellNodeBuffer> bufs;
    		int uses = 0;
    		int playableUses = 0;
    		int overflows = 0;
    		int playableOverflows = 0;
    		long elapsed = 0;
     
    		public BufferInfo(int size, int cnt)
    		{
    			mapSize = size;
    			count = cnt;
    			bufs = new ArrayList<>(count);
    		}
     
    		@Override
    		public String toString()
    		{
    			final StringBuilder stat = new StringBuilder(100);
    			StringUtil.append(stat, String.valueOf(mapSize), "x", String.valueOf(mapSize), " num:", String.valueOf(bufs.size()), "/", String.valueOf(count), " uses:", String.valueOf(uses), "/", String.valueOf(playableUses));
    			if (uses > 0)
    				StringUtil.append(stat, " total/avg(ms):", String.valueOf(elapsed), "/", String.format("%1.2f", (double) elapsed / uses));
     
    			StringUtil.append(stat, " ovf:", String.valueOf(overflows), "/", String.valueOf(playableOverflows));
     
    			return stat.toString();
    		}
    	}
     
    	@Override
    	public String[] getStat()
    	{
    		final String[] result = new String[_allBuffers.length + 1];
    		for (int i = 0; i < _allBuffers.length; i++)
    			result[i] = _allBuffers[i].toString();
     
    		final StringBuilder stat = new StringBuilder(100);
    		StringUtil.append(stat, "LOS postfilter uses:", String.valueOf(_postFilterUses), "/", String.valueOf(_postFilterPlayableUses));
    		if (_postFilterUses > 0)
    			StringUtil.append(stat, " total/avg(ms):", String.valueOf(_postFilterElapsed), "/", String.format("%1.2f", (double) _postFilterElapsed / _postFilterUses), " passes total/avg:", String.valueOf(_postFilterPasses), "/", String.format("%1.1f", (double) _postFilterPasses / _postFilterUses), "\r\n");
    		StringUtil.append(stat, "Pathfind success/fail:", String.valueOf(_findSuccess), "/", String.valueOf(_findFails));
    		result[result.length - 1] = stat.toString();
     
    		return result;
    	}
     
    	private static class SingletonHolder
    	{
    		protected static final CellPathFinding _instance = new CellPathFinding();
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: linkedlist concurrentmodificationexception

    Can you copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: linkedlist concurrentmodificationexception

    Quote Originally Posted by Norm View Post
    Can you copy the full text of the error message and paste it here.
    java.util.ConcurrentModificationException
    	at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
    	at java.util.LinkedList$ListItr.next(Unknown Source)
    	at net.sf.l2j.gameserver.pathfinding.cellnodes.CellPathFinding.findPath(CellPathFinding.java:191)
    	at net.sf.l2j.gameserver.model.actor.L2Character.moveToLocation(L2Character.java:3734)
    	at net.sf.l2j.gameserver.model.actor.L2Character$AIAccessor.moveTo(L2Character.java:2849)
    	at net.sf.l2j.gameserver.ai.AbstractAI.moveTo(AbstractAI.java:538)
    	at net.sf.l2j.gameserver.ai.AbstractAI.moveTo(AbstractAI.java:509)
    	at net.sf.l2j.gameserver.ai.L2PlayerAI.onIntentionMoveTo(L2PlayerAI.java:178)
    	at net.sf.l2j.gameserver.ai.AbstractAI.setIntention(AbstractAI.java:267)
    	at net.sf.l2j.gameserver.ai.AbstractAI.setIntention(AbstractAI.java:225)
    	at net.sf.l2j.gameserver.network.clientpackets.MoveBackwardToLocation.runImpl(MoveBackwardToLocation.java:111)
    	at net.sf.l2j.gameserver.network.clientpackets.L2GameClientPacket.run(L2GameClientPacket.java:64)
    	at net.sf.l2j.gameserver.network.L2GameClient.run(L2GameClient.java:772)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: linkedlist concurrentmodificationexception

    Is the list that is being iterated through being modified while it is iterating through the list?

    at net.sf.l2j.gameserver.pathfinding.cellnodes.CellPa thFinding.findPath(CellPathFinding.java:191)
    where is line 191 with the call to the next() method?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: linkedlist concurrentmodificationexception

    int currentX, currentY, currentZ;
    		ListIterator<AbstractNodeLoc> middlePoint, endPoint;
    		AbstractNodeLoc locMiddle, locEnd;
    		boolean remove;
    		int pass = 0;
    		do
    		{
    			pass++;
    			_postFilterPasses++;
     
    			remove = false;
    			middlePoint = path.listIterator();
    			endPoint = path.listIterator(1);
    			locEnd = null;
    			currentX = x;
    			currentY = y;
    			currentZ = z;
     
    			while (endPoint.hasNext())
    			{
    				locEnd = endPoint.next();
    				locMiddle = middlePoint.next();
    				if (GeoData.getInstance().canMoveFromToTarget(currentX, currentY, currentZ, locEnd.getX(), locEnd.getY(), locEnd.getZ()))
    				{
    					middlePoint.remove();
    					remove = true;
    					if (debug)
    						dropDebugItem(735, 1, locMiddle);
    				}
    				else
    				{
    					currentX = locMiddle.getX();
    					currentY = locMiddle.getY();
    					currentZ = locMiddle.getZ();
    				}
    			}
    		}

    here it is..

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: linkedlist concurrentmodificationexception

    IS the answer to my question: yes?
    The code is using an iterator when it removes an element from the list.

    Read the API doc for the error message for more details. The name in the error message is the name of a class:
    ConcurrentModificationException
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: linkedlist concurrentmodificationexception

    Okay i got it thank you

Similar Threads

  1. boolean linkedlist help
    By gspease839 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 11th, 2013, 04:32 PM
  2. Arraylist and LinkedList
    By vilkas123 in forum Collections and Generics
    Replies: 1
    Last Post: November 3rd, 2012, 02:20 AM
  3. cant get LinkedList to compile
    By chuy525 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 1st, 2012, 08:32 PM
  4. [SOLVED] need help implementing LinkedList
    By mia_tech in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 8th, 2012, 11:33 AM
  5. LinkedList of String
    By oxnume in forum Collections and Generics
    Replies: 3
    Last Post: April 6th, 2012, 12:12 AM