startDate = new Date();
StringBuffer countHql = new StringBuffer(
"select count(*) from TlcLogStddeliver t where 1=1");
countHql.append(" and t.proStatus='0'");
List countList = getDao().find(countHql.toString());
long count = (Long) countList.get(0);
totalPage = (int) (count % pageSize == 0 ? count / pageSize : count
/ pageSize + 1);
// 构造一个线程池
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 10, 3,
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
new ThreadPoolExecutor.DiscardOldestPolicy());
// 循环传输数据的总次数
for (int i = 1; i <= totalPage; i++) {
System.out.println("循环了第" + i + "次");
// 查表状态为0的所有数据,表示未传
StringBuffer hql = new StringBuffer(
"from TlcLogStddeliver t where 1=1 ");
hql.append(" and t.proStatus=?");
Query query = getDao().getHibernateTemplate().getSessionFactory()
.getCurrentSession().createQuery(hql.toString());
query.setParameter(0, "0");
List<TlcLogStddeliver> listTable = query
.setFirstResult((i - 1) * pageSize).setMaxResults(pageSize)
.list();
if (null != listTable && listTable.size() > 0) {
Thread t = this.createTask(listTable);
threadPool.execute(t);
t.start();
}}我这样可是还是没有控制线程数, 
 

解决方案 »

  1.   

    ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 10, 3,
    这个不就是构造了固定个数的线程池吗
      

  2.   

    我最近在做多线程下载,对它也不太了解!线程池不是用:
    ExecutorService pool = Executors.newCachedThreadPool();
    吗?反正我是用的这个!
    你了解多线程吗?去看我的贴子:“******多线程下载的问题****** ”。
    这问题很急,谢谢
      

  3.   

    t.start();去掉,这样的话每次都start线程,怎么控制?既然交由线程池了,就由线程池去控制
    threadPool.execute(t);线程池会自己执行的
      

  4.   

    其实不明白你想要什么,但是对于你当前使用线程的方式有两点不妥:
    1.如果你只是想得到一个线程池的话建议你通过如下方式来得到一个线程池//不限制大小的一个缓冲的池,如果线程足够多会内存不足的
    ExecutorService cachedPoolService = Executors.newCachedThreadPool();
    //固定大小的池,下例为10
    ExecutorService fixPoolService = Executors.newFixedThreadPool(10);2.如果你想要更多的功能可以继承ThreadPoolExecutor ,实现其它的检测import java.io.*;
    import java.util.concurrent.*;
    import java.util.*;class MyThreadPoolExecutor extends ThreadPoolExecutor{
    private boolean hasFinish = false; public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) 
    {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
        // TODO Auto-generated constructor stub
        
    }public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) 
    {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
                threadFactory, handler);
        // TODO Auto-generated constructor stub
        
    }
    public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
                threadFactory);
        // TODO Auto-generated constructor stub
        
    }    public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
            super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
            // TODO Auto-generated constructor stub
            
        }    /* (non-Javadoc)
         * @see java.util.concurrent.ThreadPoolExecutor#afterExecute(java.lang.Runnable, java.lang.Throwable)
         */
        @Override
        protected void afterExecute(Runnable r, Throwable t) {
            // TODO Auto-generated method stub
            super.afterExecute(r, t);    
            synchronized(this){
            System.out.println("自动调用了....afterEx 此时getActiveCount()值:"+this.getActiveCount()); 
            if(this.getActiveCount() == 1)//已执行完任务之后的最后一个线程
            {
             this.hasFinish=true;
             this.notify(); 
            }//if
          }// synchronized
        }
        
         public void isEndTask() { 
              synchronized(this){      
                while (this.hasFinish==false) { 
                   System.out.println("等待线程池所有任务结束: wait...");          
              try { 
              this.wait(); 
              } 
              catch (InterruptedException e) { 
    //           TODO Auto-generated catch block 
              e.printStackTrace(); 
              }
             }
          }
      }}3.建议你在运行了所有的线程后把那个service关掉,调用shutDown().否则内存不能释放的。