我即将编写我的第一个多线程Java应用程序。任务基本上是为一组产品创建销售报告;产品和报表生成器类的输入是完全独立的。
我相信我们可以创建多个线程(我想通过基于目标机器中CPU数量的属性文件来控制要创建的线程数量)。使用它们可以异步生成报告。目前这是我在单线程程序中所做的。
我希望Java大师给我一些关于设计的意见。提前致谢。
报告生成器类的结构
public class SalesReportGenerator
{
//Variables
public Report prepareReport(Product prod){
//Implementation
}
//Helper methods used by SalesReportGenerator.prepareReport
}我目前的实现,我想以多线程方式执行。
public class ReportCreater
{
public static void main(String args[]){
ListProducts listProd = new ListProducts(); //Getting product list
ArrayList products = listProd.getProductAsArrayList(); //To store list of products
ArrayList reports = new ArrayList(); //To store reports
SalesReportGenerator salesGen = new SalesReportGenerator();
Report tempReport = null;
for (int i;i
{
tempReport = salesGen.prepareReport(products.get(i));
reports.add(tempReport);
}
//At this point I will have reports for all the producst
//which I can use for processing, further reporting/saving.
}
}我想知道什么是首先创建一组线程并让他们选择产品并生成报告的最佳方法。
谢谢。