• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Multithreading scenario

 
Ranch Hand
Posts: 814
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?
 
Bartender
Posts: 3648
16
Mac OS X Firefox Browser Java
  • 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!
 
Saloon Keeper
Posts: 12154
258
  • 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: 814
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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • 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.
 
This one time, at bandcamp, I had relations with a 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
    Bookmark Topic Watch Topic
  • New Topic