GeeCON Prague 2014*
The moose likes Java in General and the fly likes selecting frrom database in multithreaded environment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "selecting frrom database in multithreaded environment" Watch "selecting frrom database in multithreaded environment" New topic
Author

selecting frrom database in multithreaded environment

nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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.
 
GeeCON Prague 2014
 
subject: selecting frrom database in multithreaded environment