Java:如何存储处理不同文件的多个线程

发布于 2024-11-08 04:10:18 字数 284 浏览 0 评论 0原文

我有一个服务器,它根据请求发送不同的文件(每个文件打开一个线程)。 每个线程都不知道他在创建时处理哪个文件,但只有在收到带有其名称的消息后,所以当时我需要以某种方式将其与文件关联起来。

我需要等待某个文件的所有线程结束的选项,当然 服务器中的所有线程。

整个线程的事情是通过线程组完成的, 至于文件,我想向每个文件添加一个线程列表(我已经有一个包装类),并添加等待某个文件的选项。 但我认为这不是一个好的解决方案,而且我需要使用一些并发集合,而且我找不到任何只是并发链表的东西。

有什么建议如何实施吗?

I have a server, that sends different files (each opening a thread) on request.
each thread doesn't know which file hes handling on creation but only once a message with its name is received, so at that time I need to associate it with the file somehow.

I need the option to wait for all the threads of a certain file to end, and of course
all the threads in the server.

The whole threads thing is done with a thread group,
as for the files, I wanted to add a list of threads to each file (I have a wrapper class for it already), and add the option to wait for a certain file.
but I don't think this is a good solution, plus I need to use some concurrent collection, and I cant find anything that is just a concurrent linked list.

Any suggestion how to implement this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

递刀给你 2024-11-15 04:10:18

我需要等待所有的选项
某个文件的线程结束

我没有清楚地理解你的要求,但我认为这样的东西适合你:

// FileSender is the wrapper class you mentioned - where you keep track
// of file->threads association
public class FileSender {
    private final File file;

    private List<Thread> threads = new ArrayList<Thread>();

    public FileSender(File file) {
        this.file = file;
    }

    public void addThread(Thread thread) {
        if (thread != null) {
            this.threads.add(thread);
        }
        for (Iterator<Thread> itr = threads.listIterator(); itr.hasNext();) {
            Thread t = itr.next();
            if (! t.isAlive()) {
                itr.remove();
            }
        }
    }

    // this method blocks until all threads associated with
    // this file has completed execution
    public void waitForCompletion() throws InterruptedException {

        for (Iterator<Thread> itr = threads.listIterator(); itr.hasNext();) {
            Thread thread = itr.next();
            thread.join();
            itr.remove();
        }
    }
}

I need the option to wait for all the
threads of a certain file to end

I did not understand your requirement clearly, but I think something like this will work for you:

// FileSender is the wrapper class you mentioned - where you keep track
// of file->threads association
public class FileSender {
    private final File file;

    private List<Thread> threads = new ArrayList<Thread>();

    public FileSender(File file) {
        this.file = file;
    }

    public void addThread(Thread thread) {
        if (thread != null) {
            this.threads.add(thread);
        }
        for (Iterator<Thread> itr = threads.listIterator(); itr.hasNext();) {
            Thread t = itr.next();
            if (! t.isAlive()) {
                itr.remove();
            }
        }
    }

    // this method blocks until all threads associated with
    // this file has completed execution
    public void waitForCompletion() throws InterruptedException {

        for (Iterator<Thread> itr = threads.listIterator(); itr.hasNext();) {
            Thread thread = itr.next();
            thread.join();
            itr.remove();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文