跟踪不同线程上传的文件
在java中,我试图编写一个FileManager,它保存文件目录并根据请求将它们上传到其他FileManager。每次上传请求(通过套接字)传入时,都会创建一个新线程来处理所请求文件的上传。
我正在考虑使用 ExecutorService 和 FileUploader 实现 Runnable 来处理这个问题。
即,如果 session 是 ExecutorService 并且 mySocket 是 FileManager 正在侦听请求的套接字,则每个线程的创建将类似于:
sessions.execute(new FileUploader(mySocket.accept()));
问题是,如果 FileManager 收到重命名其数据库中的文件的命令,它需要重命名文件之前,等待当前正在上传文件的所有线程完成。
所以问题是,我如何跟踪有多少/哪些线程正在上传给定文件?
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 ExecutorService
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 CountDownLatch与您要传输的每个文件名关联。您的平均线程应该尝试在闩锁上等待()并仅在获得闩锁时才开始处理,并且您的孩子应该在完成后使用 countDown()
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