public synchronized void Shutdown() { this.m_IsStopped = true; for (PoolThread thread : m_Threads) { thread.doStop(); } }
/** * Constructor * * @param i_NoOfThreads * @param i_MaxNoOfTasks */ public ThreadPool(int i_NoOfThreads, int i_MaxNoOfTasks) { m_TaskQueue = new BlockingQueue(i_MaxNoOfTasks); for (int i = 0; i < i_NoOfThreads; i++) { m_Threads.add(new PoolThread(m_TaskQueue)); } for (PoolThread thread : m_Threads) { thread.start(); } }
/* ------------------------------------------------------------ */ protected void newThread() { synchronized (_threadsLock) { if (_threads.size() < _maxThreads) { PoolThread thread = new PoolThread(); _threads.add(thread); thread.setName(_name + "." + _id++ + "." + thread.getId()); thread.start(); } else if (!_warned) { _warned = true; if (Log.isDebugEnabled()) { Log.debug(String.format("Max threads for %s", this.getClass().getName())); } } } }
public static void main(String args[]) throws Exception { taskQueue = new BlockingQueueTasks(); switchParser = new SwitchParser(args); Task t = new Task(0, new ArrayList<String>(), switchParser.getSwitches().get("URL")); taskQueue.enqueue(t); int cantThreads = Integer.parseInt(switchParser.getSwitches().get("p")); for (int i = 0; i < cantThreads; i++) { threads.add(new PoolThread(taskQueue, switchParser)); } for (PoolThread thread : threads) { thread.start(); } }
/** * Run job. * * @return true */ public boolean dispatch(Runnable job) { if (!isRunning() || job == null) return false; PoolThread thread = null; boolean spawn = false; synchronized (_lock) { // Look for an idle thread int idle = _idle.size(); if (idle > 0) thread = (PoolThread) _idle.remove(idle - 1); else { // queue the job _queued++; if (_queued > _maxQueued) _maxQueued = _queued; _jobs[_nextJobSlot++] = job; if (_nextJobSlot == _jobs.length) _nextJobSlot = 0; if (_nextJobSlot == _nextJob) { // Grow the job queue Runnable[] jobs = new Runnable[_jobs.length + _maxThreads]; int split = _jobs.length - _nextJob; if (split > 0) System.arraycopy(_jobs, _nextJob, jobs, 0, split); if (_nextJob != 0) System.arraycopy(_jobs, 0, jobs, split, _nextJobSlot); _jobs = jobs; _nextJob = 0; _nextJobSlot = _queued; } spawn = _queued > _spawnOrShrinkAt; } } if (thread != null) { thread.dispatch(job); } else if (spawn) { newThread(); } return true; }