• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronization while file reading  RSS feed

 
Sweta Chauhan
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • 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.

 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • 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: 7993
143
  • Mark post as helpful
  • send pies
  • 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: 56
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!