File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes One file should be processed by one thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "One file should be processed by one thread" Watch "One file should be processed by one thread" New topic

One file should be processed by one thread

aresh babu
Ranch Hand

Joined: Aug 31, 2008
Posts: 65

i have a requirement that is..

I have 2 text files and 2 threads in my porgram. My task is i need to allow only one thread to execute a one file. If thread1 executed the first text file then thread2 is not supposed to execute the first text file because it is already processed by first thread but at the same time thread2 is supposed to execute the second text file if the file is not processed by first thread. In the same way if second text file is already proccessed by thread2 then thread1 is not supposed to process the second text file.

Finally what i want to do is,
i want to process a one text file with one thread, if the text file is already processed by the first thread then second thread is not supposed to process the file, whatever the files are left out by the thread1 that files are only supposed to process by the thread2.

Can any one please let me know how to achieve it???

Thanks in advance
Aresh Babu
Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
A general solution would be to set up a queue for the file names, and a thread pool to process the files, but it may be an overkill for your problem. It'd allow you to process any number of files with any number of threads.
Sachin Chandra

Joined: Jun 19, 2009
Posts: 7

Use a thread pool to allocate unique files for each thread.

Sachin CR
I agree. Here's the link:
subject: One file should be processed by one thread
It's not a secret anymore!