我们如何以循环方式使用多线程? [英] How can we use multi-thread in round robin manner?

查看:139
本文介绍了我们如何以循环方式使用多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以多线程方式读取10个邮件帐户的未读邮件.

I want to read the unread mails of 10 mail accounts in a multi-thread way.

但是,如果线程池大小为5,则将从线程池中使用5个线程.每个线程将读取一个邮件帐户.因此,一旦Thread_1读取了第一个邮箱,它就应该读取Mailbox_6.然后线程2将读取邮箱7.

But if the thread pool size is 5, then 5 threads will be used from the thread pool. Each thread will read one mail account. So once the Thread_1 has read the first mail box, it should read mailbox_6. Then thread 2 will read mailbox_7.

所有邮件帐户已被读取一次后,该循环将从第一个邮件帐户开始.

When all mail account have been read once, the cycle will start from 1st mail account.

我们如何在Java中做到这一点?

How can we do this in java?

推荐答案

这应该很容易.您创建具有5个线程的固定线程池,然后将10个作业提交给该池-每个用户电子邮件帐户1个作业:

This should be pretty easy. You create a fixed-thread pool with 5 threads and then you submit the 10 jobs to the pool -- 1 per each user email account:

// create a thread pool with 5 workers
ExecutorService threadPool = Executors.newFixedThreadPool(5);
// submit all 10 user email accounts to the pool to be processed in turn
for (UserEmail userEmail : userEmailsToProcess) {
    threadPool.submit(new EmailProcessor(userEmail));
}
// once we have submitted all jobs to the thread pool, it should be shutdown
threadPool.shutdown();
...
// here's our runnable class which does the actual work
public class EmailProcessor implements Runnable {
    private UserEmail userEmail;
    public MyJobProcessor(UserEmail userEmail) {
        this.userEmail = userEmail;
    }
    public void run() {
        // read the user email
        ...
    }
}

UserEmail类可以保存要读取"的电子邮件的文件名,或者可以包含帐户名或其他名称.这将由您决定如何表示邮件帐户和要阅读的邮件.

The UserEmail class could hold the filename of the email to "read" or maybe the account name or something. That will be up to you to figure out how to represent the mail account and mail to be read.

[[来自评论:]]

[[ From the comments: ]]

我有10个邮箱,例如..mailbox1 ... mailbox10,现在我从线程池中有5个线程,因此thread1将获取任何邮箱,因此假设它将选择邮箱1,然后线程2将选择邮箱2,线程3将选择邮箱3,线程4将选择邮箱4,线程5将选择邮箱5,现在,当线程1(已经定义了特定时间段)空闲时,它应该选择邮箱6-邮箱10,尚未读取的任何人都不应选择邮箱1-邮箱5中的任何邮箱,直到所有尚未读取的邮箱.

i have 10 mailboxes like.. mailbox1...mailbox10, now i have 5 thread from thread pool, so thread1 will fetch any mailbox so lets assume it will pick mailbox1 , then thread2 will pick mailbox2, thread3 will pick mailbox3, thread4 will pick mailbox4 and thread5 will pick mailbox5, now when thread1 (with specific time period which is already defined) will free then it should be pick mailbox6 - mailbox10, anyone which is yet not read it should not pick any mailbox from mailbox1 - mailbox5, up to all mailbox which yet not read.

哦,我明白了.一种解决方案是让调度线程每时每刻都在休眠和唤醒,以查看邮箱是否有任何邮件.如果他们这样做了,那么它将使该作业服从线程池,以便读取该邮箱.读取邮箱后,线程将返回并要求下一个邮箱进行处理.分派线程将继续向该线程池添加邮箱,直到被告知要停止.

Oh I see. One solution is to have a dispatch thread that is sleeping and waking up every so often to see if the mailbox's have any mail. If they do then it subjects a job to the thread-pool for that mailbox to be read. Once the mailbox has been read the thread goes back and asks for the next mailbox to process. The dispatch thread will continue to add mailboxes to the thread-pool until it is told to stop.

如果EmailProcessor中有很多上下文,那么您可以拥有线程池,但是它们可能会从BlockingQueue<File>中消耗,或者告诉他们哪些邮箱需要注意.

If there is a lot of context in an EmailProcessor then you could have your thread-pool but they could consume from a BlockingQueue<File> or something which tells them what mailbox needs attention.

这篇关于我们如何以循环方式使用多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆