0

I have a Java client (client server app) that does two main things: (1) listen to server to receive files and save them to a local folder and (2) watch that folder for changes and send changes to the server. I want to run each in its own thread. First, is it a good idea to run each task on a separate thread. Second, how do I lock the folder when it's used by either task to avoid interference?

6
  • 2
    Can you edit your post to show some of your work? What have you tried? What documentation have you read? Commented Oct 1, 2012 at 0:20
  • @Gray I'm not asking for code corrections; I'm seeking advice. How to lock a resource (folder in my case) and unlock it. Commented Oct 1, 2012 at 0:21
  • Small observation, if you want to lock the folder for single thread what is the point of having two threads ? Commented Oct 1, 2012 at 0:23
  • @Vash One thread can change files while they are being sent by the other or any kind of interference you can think of. Commented Oct 1, 2012 at 0:27
  • Is writing received file a change that must be send to a server? The server to load files from, and the server to send changes to - are they the same or different servers? Commented Oct 1, 2012 at 4:18

1 Answer 1

4

is it a good idea to run each task on a separate thread

It sounds like a good idea to split your program into threads since the 2 tasks can work asynchronously and concurrently. The 1st thread could be downloading at the same time the 2nd one is uploading.

how do I lock the folder when it's used by either task to avoid interference?

I wouldn't do a lock at all. I'd have your 1st thread read a file from the server, write it into the folder, and then add a FileToSend object (or maybe just a File object) to a BlockingQueue. So instead of looking at the directory, your 2nd thread would just be waiting on the BlockingQueue for files to be sent to the server. The LinkedBlockingQueue class should work well for this. The BlockingQueue takes care of the locking for you.

If you do need to share a lock then you could just inject a lock object into your two threads:

private final Object folderLock = new Object();
...
new Thread(new Downloader(folderLock)).start();
new Thread(new Uploader(folderLock)).start();
...

A good pattern would be to define an addFileToUpload(File fileToUpload) method on your Uploader class. Then your Uploader can decide what to do with it. The BlockingQueue could then be local to the Uploader

Uploader uploader = new Uploader();
// pass the uploader into the downloader
new Thread(new Downloader(uploader)).start();
new Thread(uploader).start();
Sign up to request clarification or add additional context in comments.

4 Comments

Great! Is BlockingQueue a Java class? How do I share it between the two threads?
Yes. BlockingQueue is a Java interface. LinkedBlockingQueue is one of the implementations. You can define it outside and pass it into each thread Runnable. See my lock example above.
Okay, so the downloader sends a addFileToUpload msg to the uploader but then the uploader is a member in the downloader, right? The downloader delegates uploading files to it. Or should I make that method a class method?
Yeah, I'd make it a member @saadtaame. The class method would work but it's bad form IMHO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.