62,614
社区成员
发帖
与我相关
我的任务
分享
public class OldThreadPools {
public Worker[] workers;
public Vector taskList = new Vector();
public OldThreadPools(int poolNumber) {
workers =new Worker[poolNumber];
for (int i = 0; i < poolNumber; i++) {
Worker t = new Worker(taskList);
workers[i] = t;
t.run();
}
}
public void addTaks(Object o) {
taskList.add(o);
getIdleWorker().run();
}
public Worker getIdleWorker() {
// find ide thread
return workers[0];
}
public static void main(String args[]) throws InterruptedException {
OldThreadPools pool = new OldThreadPools(2);
pool.addTaks("just a test");
}
}
class Worker implements Runnable {
List taskList;
public Worker(List taskList) {
this.taskList = taskList;
}
public void run() {
if (this.taskList!= null && this.taskList.size() > 0) {
Object o = taskList.get(0);
dbOperate(o);
}
}
private void dbOperate(Object o) {
System.out.println("I am db operate thread from pool");
}
}
public class ExecutorSample {
public static void main(String args[]) throws InterruptedException {
ExecutorService exec = Executors.newFixedThreadPool(2);
exec.submit(new FileThread());
exec.submit(new DbThread());
exec.shutdown();
}
}
class FileThread implements Runnable{
public void run(){
System.out.println("I am search file thread");
}
}
class DbThread implements Runnable{
public void run(){
System.out.println("I am db operate thread");
}
}