Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Multi threaded programs in Java

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


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


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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Firstly you’ll want to create a class the implements the interface Runnable. Your new object that implements the interface Runnable could take the ArrayList of products, then in the implemented method run() you could create a new instance of SalesReportGenerator and start generating your report.

So for each time a request comes in for a report you create a new thread passing in via the constructor an instance of your new object that implements Runnable and then start the thread.

This is the basics but with this and a little research on Threads you should be able to get this going.

Hope it helps.
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use Executor Framework for Threading. Search in google.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic