示例#1
0
  public synchronized void Shutdown() {
    this.m_IsStopped = true;

    for (PoolThread thread : m_Threads) {
      thread.doStop();
    }
  }
示例#2
0
  /**
   * 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();
    }
  }
示例#3
0
 /* ------------------------------------------------------------ */
 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()));
       }
     }
   }
 }
示例#4
0
  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();
    }
  }
示例#5
0
  /**
   * 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;
  }