如何控制java线程并发数

sdfsdfds324234234234 2011-05-17 09:40:51
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();
}

}我这样可是还是没有控制线程数,

...全文
618 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hh3755 2011-06-01
  • 打赏
  • 举报
回复
其实不明白你想要什么,但是对于你当前使用线程的方式有两点不妥:
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().否则内存不能释放的。
qingyuan18 2011-05-17
  • 打赏
  • 举报
回复
楼主你要控制并发线程,这个是线程执行中的加锁和同步策略决定的,线程池只是控制线程数量和相应的生命周期
qybao 2011-05-17
  • 打赏
  • 举报
回复
t.start();去掉,这样的话每次都start线程,怎么控制?既然交由线程池了,就由线程池去控制
threadPool.execute(t);线程池会自己执行的
Spring89 2011-05-17
  • 打赏
  • 举报
回复
我最近在做多线程下载,对它也不太了解!线程池不是用:
ExecutorService pool = Executors.newCachedThreadPool();
吗?反正我是用的这个!
你了解多线程吗?去看我的贴子:“******多线程下载的问题****** ”。
这问题很急,谢谢。。。
lemon5366 2011-05-17
  • 打赏
  • 举报
回复
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 10, 3,
这个不就是构造了固定个数的线程池吗
Inhibitory 2011-05-17
  • 打赏
  • 举报
回复
使用线程池

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧