计费类具有与国家计费相关的所有逻辑。它从数据库中获取结果,然后向用户收费。计费类实现 Runnable。我想根据国家参数并行执行计费,以便快速计费大量用户(500 万+)。现在需要几个小时才能完成。
我正在尝试实现 ThreadPoolExecutor 来执行 Billing 类,但我很困惑如何?以下有什么区别或者我做错了什么?请推荐!!总共有 20 个国家,但我在这里只粘贴 5 个。
//for 20 countries ThreadPoolExecutor (20,20,20.......)????
ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5, 5, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(10), new ThreadPoolExecutor.CallerRunsPolicy());
executor.execute(new Billing("UK"));
executor.execute(new Billing("USA"));
executor.execute(new Billing("Germany"));
executor.execute(new Billing("Spain"));
executor.execute(new Billing("Italy"));
或者
ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5, 5, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(10), new ThreadPoolExecutor.CallerRunsPolicy());
for(int i=0;i<5;i++) // for 20 countries i<20??
{
executor.execute(new Billing("UK"));
executor.execute(new Billing("USA"));
executor.execute(new Billing("Germany"));
executor.execute(new Billing("Spain"));
executor.execute(new Billing("Italy"));
}
while (! executor.isTerminated()) {
try{
executor.awaitTermination(100, TimeUnit.SECONDS);
}catch(InterruptedException iE)
{
iE.printStackTrace();
System.out.println("Executor Exception: "+ iE);
}
提前致谢!!