使用ArrayBlockingQueue会使进程变慢

Kar*_*ara 6 java multithreading blockingqueue threadpoolexecutor

我刚刚使用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
    BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(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);
    }
  }
Run Code Online (Sandbox Code Playgroud)

}

kos*_*tya 2

如果您希望尽快将文件中的记录持久保存到关系数据库中,您应该使用 JDBC 批量插入,而不是一条一条地插入记录。