一直是强势围观,从未发过贴。现在遇到一问题:同时用多个线程到处多个表的数据,各个表之间数据量未知,为了节省时间,使用多线程,由于本人属菜鸟级,所以对线程不是太了解,现用线程池解决这问题,不知这是否正确,或者是否有更优方法,或者此方法有哪些需要改进,忘各位大大不吝赐教,再此谢过。
代码如下
代码如下
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(1, 3, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),new ThreadPoolExecutor.CallerRunsPolicy());
for (String table : tables)
{
poolExecutor.execute(new ExportTask(entityName));
}
class ExportTask implements Runnable
{
ZipOutputStream zos;
String table;
public ExportTask(String table) throws FileNotFoundException
{
this.table =table;
zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(new File("packageDir", "abc.zip"))));
}
public void run()
{
// export the table data into file
export(table);
}
}