public class Demo {
public static void main(String[] args) {
int threadNum=10;
StringBuffer sb = new StringBuffer();
ExecutorService servicePool = Executors.newFixedThreadPool(threadNum);
List
for(int i=0;i
Task task =new Task(1000);
//用10个线程并行执行 一个10*1000 次
Future
//最好合并结果
list.add(futrue);
}
for(Future
try {
String result=futrue.get();
sb.append(result);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
System.out.println(sb.toString());
servicePool.shutdown();
}
}
class Task implements Callable
private int num = 0;
public Task(int num) {
this.num = num;
}
@Override
public String call() throws Exception {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < num; i++) {
sb.append("里面是拼接的sql语句有点长!!");
}
return sb.toString();
}
}