我想问下大家Executor框架中,如何判断线程池的目前的任务量,
  for (int i=0;i<=500000;i++)
       {
                
         Task SendTask =new Task();//实现了Callable接口.
        
        
   
      
         Future<Integer> future=exec.submit(SendTask);//如果目前任务已经饱和,我这里添加会阻塞吗?                                                       
                                                            
 }

解决方案 »

  1.   

    饱和是什么意思?如果系统能创建的线程数已满,会抛出OutOfMemory异常。
      

  2.   

    个人理解:如果是使用的默认队列SynchronousQueue,是不会阻塞的。
      

  3.   

    那java 可以创建的最大的线程池的容量是多少个线程啊?
      

  4.   

    这个应该跟系统有关系了。内存大了可以创建的就多,而且有一种说法是虚拟机分配内存的时候给系统剩的内存越多能创建的就越多,因为java创建线程是用的系统资源(没有验证过)。
    我的2G WindowsXP系统可以创建4000到5000。
      

  5.   

        /**
         * Creates a new <tt>ThreadPoolExecutor</tt> with the given initial
         * parameters.
         *
         * @param corePoolSize the number of threads to keep in the
         * pool, even if they are idle.
         * @param maximumPoolSize the maximum number of threads to allow in the
         * pool.
         * @param keepAliveTime when the number of threads is greater than
         * the core, this is the maximum time that excess idle threads
         * will wait for new tasks before terminating.
         * @param unit the time unit for the keepAliveTime
         * argument.
         * @param workQueue the queue to use for holding tasks before they
         * are executed. This queue will hold only the <tt>Runnable</tt>
         * tasks submitted by the <tt>execute</tt> method.
         * @param threadFactory the factory to use when the executor
         * creates a new thread.
         * @param handler the handler to use when execution is blocked
         * because the thread bounds and queue capacities are reached.
         * @throws IllegalArgumentException if corePoolSize, or
         * keepAliveTime less than zero, or if maximumPoolSize less than or
         * equal to zero, or if corePoolSize greater than maximumPoolSize.
         * @throws NullPointerException if <tt>workQueue</tt>
         * or <tt>threadFactory</tt> or <tt>handler</tt> are null.
         */
        public ThreadPoolExecutor(int corePoolSize,
                                  int maximumPoolSize,
                                  long keepAliveTime,
                                  TimeUnit unit,
                                  BlockingQueue<Runnable> workQueue,
                                  ThreadFactory threadFactory,
                                  RejectedExecutionHandler handler) {
            if (corePoolSize < 0 ||
                maximumPoolSize <= 0 ||
                maximumPoolSize < corePoolSize ||
                keepAliveTime < 0)
                throw new IllegalArgumentException();
            if (workQueue == null || threadFactory == null || handler == null)
                throw new NullPointerException();
            this.corePoolSize = corePoolSize;
            this.maximumPoolSize = maximumPoolSize;
            this.workQueue = workQueue;
            this.keepAliveTime = unit.toNanos(keepAliveTime);
            this.threadFactory = threadFactory;
            this.handler = handler;
        }BlockingQueue<Runnable> workQueue,使用ArrayBlockingQueue的时候构造的时候有个等待队列的大小, 如果任务多于这个大小,那么默认好像是会抛弃策略.
    如果使用LinkedBlockQueue的时候应该就不用怕了, 设置一下RejectedExecutionHandler handler为CallerRunsPolicy,默认是AbortPolicy
      

  6.   

    弱弱的问一句 submit是线程安全的吗?  多个线程同时向一个线程池提交任务