Tôi vừa mới sử dụng ArrayBlockingQueue cho quy trình đa luồng của mình. Nhưng có vẻ như nó đã chậm lại hơn là tăng tốc. Các bạn có thể giúp tôi không? Tôi cơ bản nhập khẩu một tập tin (khoảng 300k hàng) và phân tích chúng và lưu trữ chúng trong DBSử dụng ArrayBlockingQueue làm cho quy trình chậm hơn
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);
}
}
}
Bị chậm lại so với cái gì? Sử dụng một loại 'BlockingQueue' khác? – Andreas
Hãy thoát khỏi giấc ngủ. –
Tại sao? Tôi đề nghị bạn thoát khỏi hàng đợi, các chủ đề, người thi hành, tất cả điều đó và làm tất cả trong một luồng đơn lẻ, dưới dạng một lô. Bạn không cần hàng đợi 30.000 mục và 90-100 chủ đề cho việc này. – EJP