This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Multithreading scenario  RSS feed

 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have following scenario with respect to Multithreading environment

Suppose I want to process a file of 1000 records and I have created threads T1, T2, T3 and T4. Each thread will process 250 records. All records should process in sequence.

To implement above scenario if I use CyclicBarrier then is that correct approach or I need to provide better solution in above scenario.

Can anybody let me know what will be best solution on above scenario?
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seem to be a ideal task using the Java 7 fork/join framework. Are you using Java 7?

You mentioned that "all records should process in sequence". So if you do have 4 threads 250 records each, but will these 4 sets of records have dependency? If so then it's no different from processing each record one by one!
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ninad Kulkarni wrote:All records should process in sequence.


Yeah. Use 1 thread.
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:So if you do have 4 threads 250 records each, but will these 4 sets of records have dependency?


Yes theses 4 sets of records have dependency with each other.

Using Java 6 version.

Stephan van Hulst wrote:Yeah. Use 1 thread.


Yes you are absolutely correct but if we want to do it in parallel processing way, I mean if records are in millions then we need to process it parallel so want to know more on the approach we can use.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ninad Kulkarni wrote:
K. Tsang wrote:So if you do have 4 threads 250 records each, but will these 4 sets of records have dependency?


Yes theses 4 sets of records have dependency with each other.

Using Java 6 version.

Stephan van Hulst wrote:Yeah. Use 1 thread.


Yes you are absolutely correct but if we want to do it in parallel processing way, I mean if records are in millions then we need to process it parallel so want to know more on the approach we can use.


It doesn't matter if there are 10 or 10 million. If they have to be processed in sequence, each dependent on the previous, then you gain nothing with parallelism except the requirements to be thread safe and costs of synchronization. Parallelism only works if the tasks can be done in parallel, independently. In your scenario, at most you would use two threads in a producer/consumer mechanism. One producer which reads from the file and puts the record in a queue to be processed, and one consumer which reads from the queue to process the record - so at least the reading the next record from disk and processing the record can be done in parallel.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!