• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this way of creating a multithreaded application correct?  RSS feed

 
Monica Shiralkar
Ranch Hand
Posts: 919
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to create a multithreaded application which reads lakhs of files from a folder, does some alterations in the content and write the output in separate output files in another folder. I did the below steps:

1) Created a thread pool, set the maximum number of threads it should run at a moment.

2) Read the files using a single thread and called run method for each file while reading each file one by one in a loop..From run method I am calling the method which does the alterations in file and writes to separate output file.


Is this method of creating multi threaded application correct?

thanks
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't tell exactly how multi-threading works from your description. If you're saying that you're spawning a thread for each input file (which will also cause an output file to be created), that sounds like an OK approach. Note that the concurrency is limited by the number of write heads on the disk - that number is likely to be < 10.
 
Monica Shiralkar
Ranch Hand
Posts: 919
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks. Yes I am doing like that. So in that case how can I measure the total time taken? I know that the way to measure total time taken by a program is to put timer at the beginning and end but since here many threads are running including the main thread so who knows which thread will be the last to exit as the end timer should be in that thread.If I put end timer at the end of main method will I get the total time?Does that mean main thread will exit at the end?
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monica. Shiralkar wrote:So in that case how can I measure the total time taken? I know that the way to measure total time taken by a program is to put timer at the beginning and end but since here many threads are running including the main thread so who knows which thread will be the last to exit as the end timer should be in that thread.If I put end timer at the end of main method will I get the total time?Does that mean main thread will exit at the end?


If you put the end timer at the end of the main method (and assuming you put the start timer at the beginning), all you will be measuring is the time it takes the main method to puts those lakhs of jobs into the thread pool. You need to make sure that the thread pool is finished with all the jobs first.

Henry
 
Monica Shiralkar
Ranch Hand
Posts: 919
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you put the end timer at the end of the main method (and assuming you put the start timer at the beginning), all you will be measuring is the time it takes the main method to puts those lakhs of jobs into the thread pool. You need to make sure that the thread pool is finished with all the jobs first.

Henry



In that case how to measure the total time taken for the application?
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monica. Shiralkar wrote:
In that case how to measure the total time taken for the application?


You need a way to determine that all the tasks in the thread pool has been completed. For example, the executor service class provided by Java provides a way to determine the completion of an orderly shutdown -- so a quick and dirty option, after adding all the tasks to the executor would be to (orderly) shutdown the executor and await its termination. When all the tasks completes, the thread pool will shutdown and the main thread will wake up.

Now, if you are thinking that shutting down executor is extreme, I kinda agree... so, alternatively, you can track the amount of files processed. For example, at the end of the task, you simply increment a thread safe variable. And when the count reaches the expected value, the main thread can determine the stop time.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!