Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Best approach to Thread  RSS feed

 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am working on an application where i need to generate multiple reports in .txt format. Earlier i did it sequentially & it is working fine. Now i want to multithread it. What is the best method to go about that?
I assume i will create a Class which extends Thread ( say ReportThread). Within its run() i will call the generateReport() method. I will create as many instance of ReportThread class as many reports are to be genaretd & call the respective run method. From the main programs how do i determine that all the threads are exited??
Any code snippets for the same will be very helpful

Regards,
Saj
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's always good practice to use the most abstract thing that will work, so when you write your report generator implementing Runnable is preferable to extending Thread. You can use join to know when another thread is done:

If you have a reasonable number of reports / threads this should be fine as is. If you have too many the JVM will bog down managing threads and run slower and slower. I think I've read 200 threads will bring most JVMs to their knees, but I'd be inclined to keep the number under 20. In JDK 5 look into Executors for thread pooling. I'm not sure how you know when all the things you put in queue have finished. Look at the APIs ... I'm pretty sure you can query the outstanding job count. Maybe somebody will jump in with something more creative.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!