Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

selecting frrom database in multithreaded environment

 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
need some advise on multi-threading, i have a java class which extends Thread. Inside its run () method i want to go to the database and fetch some records and write them out to a flat file. I am not sure how to synchronise the code so when multiple threads are running at the same time the data that gets returned from the database doesnt get corrupted.
i don't have any sample code yet so apologies if the above is vague.

thanks in advance.
 
Paul Clapham
Sheriff
Pie
Posts: 20764
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your output is going to just one flat file? Then you'll want to prevent threads from trying to write to that file at the same time. You could have your working threads synchronize on something to prevent that, or you could use a producer-consumer model in which your "producers" are your working threads (the ones accessing the database) and the "consumer" is something which writes to that file.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic