selecting frrom database in multithreaded environment
posted 4 years ago
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.
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.