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();
}}我这样可是还是没有控制线程数,
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();
}}我这样可是还是没有控制线程数,
解决方案 »
- 大家帮我看看这个是什么错误,怎么修改?一下子就出现这个错误,所有的网页都打不开!!
- 我想用org.apache.tools.zip实现压缩,但找不到该包
- JXplorer连接ApacheDS时遇到的问题,求助高手
- 我的tomcat一启动,CSRSS进程就占用大量内存,怎么回事啊?
- 多表连接查询的速度问题
- 我想学习java,谁能告诉我哪儿有这方面的免费电子书
- 我从一个JSP页面action到另一个JSP里,为什么前一个JSP页面中的表单值传不到后一个JSP页面中?
- 请问哪里有JSP 运行环境 所需的各种软件下载(做好有IIS 的插件)?
- 关于frameset框架中jsp页面通过<s:iterator/>标签遍历内容不显示
- 使用SpringMvc跨服务器上传图片,Ajax异步刷新图片框显示图片功能请求不到后台,onChange事件没有激活,这是为什麽?
- 头疼 求帮助
- sturts2中怎么实现urlrewrite
这个不就是构造了固定个数的线程池吗
ExecutorService pool = Executors.newCachedThreadPool();
吗?反正我是用的这个!
你了解多线程吗?去看我的贴子:“******多线程下载的问题****** ”。
这问题很急,谢谢
threadPool.execute(t);线程池会自己执行的
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().否则内存不能释放的。