• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Synchronization while file reading

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have worked on java but not in multithreading issue. Currently in my organization there is requirement where file will be read by multiple thread and need to ensure that first thread t3,t2 and t1 will read the file and after that rest of thread could start reading.

I am able to achieve the first scenario with join() method but when thread t4,t5 and t6 start reading file result in not what is expected. for example T4 is not finished with synchronized readFile() method and t5 start executing synchronized readFile() method.

I am sharing my code, could someone help where I am missing.

 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should use high-level concurrency tools, like Lock, Condition, CyclicBarrier or CountDownLatch.

In this case, I would let the slave threads await a CountDownLatch, and have the master threads count down.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This seems like a really weird requirement though.

Why should the threads wait for each other? They're not bothering each other by reading concurrently. And if they have to read one at a time, why is this multi-threaded in the first place?
 
Sweta Chauhan
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:This seems like a really weird requirement though.

Why should the threads wait for each other? They're not bothering each other by reading concurrently. And if they have to read one at a time, why is this multi-threaded in the first place?



Thanks Stephan for your response.

You are right I think we need to reconsider the requirement.
 
reply
    Bookmark Topic Watch Topic
  • New Topic