import java.util.concurrent.Executors;
import java.util.concurrent.CountDownLatch;
/** 线程数 */
public final static int THREAD_TOTAL_NUM = 3;
private ExecutorService exec = Executors.newCachedThreadPool();//从线程池里获取线程
CountDownLatch latch = new CountDownLatch(THREAD_TOTAL_NUM );// //循环从mem里取值 每页数据都用一个线程去处理
for(int i =0;i<= THREAD_TOTAL_NUM;i++){
//从缓存取数据
value = MemcachedManagerSports.getMemcachedObject("fubo"+j, 0,"ISO-8859-1");
//处理数据
exec.execute(new CrownProcessor(value ,page.getDomain(),page,realManager,this,this.realUrl,latch));
}熟悉线程池的帮忙看下 这个会根据循环的次数产生多个线程么、还是只有一个线程??????在线等
不过线程池的使用就是象我刚才说的那样。
给你一段线程池的代码,代码最先从ibm网站上抄来并加以修改。
...{
private final int nThreads;
private final PoolWorker[] threads;
private final LinkedList queue; public WorkQueue(int nThreads)
...{
this.nThreads = nThreads;
queue = new LinkedList();
threads = new PoolWorker[nThreads]; for (int i=0; i<nThreads; i++) ...{
threads[i] = new PoolWorker();
threads[i].start();
}
} public void execute(Runnable r) ...{
synchronized(queue) ...{
queue.addLast(r);
queue.notify();
}
} private class PoolWorker extends Thread ...{
public void run() ...{
Runnable r; while (true) ...{
synchronized(queue) ...{
while (queue.isEmpty()) ...{
try
...{
queue.wait();
}
catch (InterruptedException ignored)
...{
}
} r = (Runnable) queue.removeFirst();
} // If we don't catch RuntimeException,
// the pool could leak threads
try ...{
r.run();
}
catch (RuntimeException e) ...{
// You might want to log something here
}
}
}
}
}
看来这段代码被很多地方引用了。
http://blog.csdn.net/linwenfu/archive/2007/07/25/1708071.aspx 这个家伙把整篇都收藏了。
当方法execute被调用时,传进来的任务加入队列queue,并发出通知notify(),
线程池中的线程得到通知被唤醒,把任务从队列中除去并执行该任务。执行完后再进入等待状态。如此周而复始。
如果你这样问我的话,那说明一个问题,你根本就没有去看这两个方法的 API 文档!newFixedThreadPool 方法需要调用者传一个数值进去的,这个数值就是线程池最大的大小。请看看看看看看看 ThreadPoolExecutorService 类的 API 文档,以及它的构造方法。
exec.execute(new CrownProcessor(value ,page.getDomain(),page,realManager,this,this.realUrl,la
肯定多个线程