Java 中的多线程程序

发布于 2024-10-26 23:08:37 字数 1266 浏览 1 评论 0原文

我即将编写我的第一个多线程 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<products.size() ;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.
    }
}

我想知道首先创建一组线程并让它们选择产品并生成报告的最佳方法是什么。

谢谢。

I am about to write my first multi threaded Java application. The task basically is to create sales report for a set of products; the products and the inputs to the report generator class are completely independent.

I believe we can create multiple threads (I would like to control the number of threads to create from a properties files based on number of CPUs in the target machine). to use them generate reports asynchronously. Currently this is what I do in my single threaded program.

I wish Java gurus give me some inputs on the design. Thanks in advance.

Structure of Report Generator class

public class SalesReportGenerator
{
    //Variables

    public Report prepareReport(Product prod){
        //Implementation
    }

    //Helper methods used by SalesReportGenerator.prepareReport
}

My current implementation which I would like to perform in a multi threaded approach.

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<products.size() ;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.
    }
}

I would like to know what is the best way to first create a set of threads and make them pick a product and generate report.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢欲 2024-11-02 23:08:37

您需要的是一个创建线程池并在其中分配报告生成任务的类。 Java已经提供了这样的类。它称为 ExecutorService。您将任务定义为 Runnable 并将其传递给 ExecutorService 的对象。当你创建这个对象时,你应该指定线程池的大小。如果任务是 CPU 密集型任务,您可能希望将此大小限制为 NumberOfProcessors + 1。 (请参阅此示例了解如何确定 NumberOfProcessors 的值)。您可以向该对象提交任意数量的任务,它将管理可用线程如何执行这些任务。网上有许多教程向您展示如何使用此类

What you need is a class that creates a thread pool and distribute the report generation tasks among them. Java already provides such a class. It is called ExecutorService. You define a task as a Runnable and pass it to an object of ExecutorService. When you create this object, you should specify the size of the thread pool. If the tasks are CPU intensive, you might want to limit this size to NumberOfProcessors + 1. (See this example on how to determine the value of NumberOfProcessors). You submit as many tasks as you want to this object and it will manage how they get executed by the available threads. There are many tutorials on the web that shows you how to use this class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文