工作中遇到的并行处理问题# Java - 爪哇娇娃
T*B
1 楼
在工作中要解决一个scale相关的问题。现有的code简化后大约是这样:
class Predictor {
public Predictor() {
// time consuming stuff.
}
String synchronized predict(String input) {
String result = null;
// step 1
// step 2
// step 3
return result;
}
}
class PredictSvc {
private Predictor predictor = new Predictor();
public List handleRequest() {
List results = new ArrayList();
int count = getCount();
for (int i = 0; i < count; ++i) {
String input = getInput();
String result = predictor.predict(input);
results.add(result);
}
return results;
}
}
现在的问题是这个count有时候很大,导致handleRequest() 执行时间太长。我现在的
想法是做一个thread pool,每一个thread own一个Predictor object. 这样的话在
handleRequest()里就可以同时递交多个predict请求。我学习了一下
ThreadPoolExecutor以及Runnable/Callable, 注意到这些办法好像不能直接take
input parameter.
想问问各位高手这个问题用什么方法并行处理比较好? 谢谢。
class Predictor {
public Predictor() {
// time consuming stuff.
}
String synchronized predict(String input) {
String result = null;
// step 1
// step 2
// step 3
return result;
}
}
class PredictSvc {
private Predictor predictor = new Predictor();
public List
List
int count = getCount();
for (int i = 0; i < count; ++i) {
String input = getInput();
String result = predictor.predict(input);
results.add(result);
}
return results;
}
}
现在的问题是这个count有时候很大,导致handleRequest() 执行时间太长。我现在的
想法是做一个thread pool,每一个thread own一个Predictor object. 这样的话在
handleRequest()里就可以同时递交多个predict请求。我学习了一下
ThreadPoolExecutor以及Runnable/Callable, 注意到这些办法好像不能直接take
input parameter.
想问问各位高手这个问题用什么方法并行处理比较好? 谢谢。