All pastes #954482 Raw Edit

nbali

public javascript v1 · immutable
#954482 ·published 2008-03-23 22:41 UTC
rendered paste body
/* 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 2, 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, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. * * http://www.gnu.org/copyleft/gpl.html */package net.sf.l2j.gameserver.taskmanager;import javolution.util.FastList;import net.sf.l2j.gameserver.ThreadPoolManager;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class AiTaskManager implements Runnable{	private final static Log _log = LogFactory.getLog(AiTaskManager.class.getName());	private FastList<Runnable> _aiTasks = new FastList<Runnable>();		private static AiTaskManager _instance;		private AiTaskManager()	{		ThreadPoolManager.getInstance().scheduleAiAtFixedRate(this, 1000, 1000);		_log.info("AiTaskManager: Initialized.");	}		public static AiTaskManager getInstance()	{		if (_instance == null)			_instance = new AiTaskManager();				return _instance;	}		public synchronized Runnable addAiTask(Runnable runnable)	{		if (!_aiTasks.contains(runnable))			_aiTasks.add(runnable);				return runnable;	}		public synchronized void removeAiTask(Runnable runnable)	{		_aiTasks.remove(runnable);	}		public synchronized boolean getAiTask(Runnable runnable)	{		return _aiTasks.contains(runnable);	}		private synchronized Runnable[] getRunnables()	{		return _aiTasks.toArray(new Runnable[_aiTasks.size()]);	}		public void run()	{		for (Runnable runnable : getRunnables())		{			try			{				if (runnable != null)					runnable.run();			}			catch (Exception e)			{				_log.warn("", e);			}		}	}}