我刚刚使用ArrayBlockingQueue进行多线程处理.但它似乎放慢了速度而不是加速.你能帮助我吗?我基本上是导入一个文件(大约300k行)并解析它们并将它们存储在数据库中
public class CellPool { private static class RejectedHandler implements RejectedExecutionHandler { @Override public void rejectedExecution(Runnable arg0, ThreadPoolExecutor arg1) { System.err.println(Thread.currentThread().getName() + " execution rejected: " + arg0); } } private static class Task implements Runnable { private JSONObject obj; public Task(JSONObject obj) { this.obj = obj; } @Override public void run() { try { Thread.sleep(1); runThis(obj); } catch (InterruptedException e) { e.printStackTrace(); } } public void runThis(JSONObject obj) { //where the rows are parsed and stored in the DB, etc } } public static void executeCellPool(String filename) throws InterruptedException { // fixed pool fixed queue BlockingQueuequeue = new ArrayBlockingQueue (300000, true); ThreadPoolExecutor executor = new ThreadPoolExecutor(90, 100, 1, TimeUnit.MINUTES, queue); DataSet ds = CommonDelimitedParser.getDataSet(filename); final String[] colNames = ds.getColumns(); while (ds.next()) { JSONObject obj = new JSONObject(); //some JSON object Task t = new Task(obj); executor.execute(t); } }
}