开发者

Keeping track of files being uploaded by different threads

In java, I'm trying to write a FileManager that holds a directory of files and uploads them to other FileManagers on request. Every time an upload request comes in (through a Socket), a new thread is created to handle the upload of the requested file.

I was thinking of using an ExecutorServic开发者_开发百科e and a FileUploader implements Runnable to deal with this.

i.e. if sessions is the ExecutorService and mySocket is the socket the FileManager is listening for requests on, the creation of each thread would be something like:

sessions.execute(new FileUploader(mySocket.accept()));

The problem is that if the FileManager gets a command to rename a file in it's database, it needs to wait for all the threads that are currently uploading the file to finish before renaming the file.

So the question is, how can I keep track of how many/which threads are uploading a given file?


You can use a CountDownLatch associated with each filename that you want to transfer. Your mean thread should try to await() on the latch and start processing only when it gets the latch and your children should use countDown() when they are done

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜