通过 executorservice 进行顺序事件处理

发布于 2024-09-30 22:35:59 字数 907 浏览 9 评论 0原文

我有一个事件队列要处理。线程将事件添加到队列中。
我创建了一个可运行的任务,它在 run 方法中执行处理事件所需的所有操作。
我声明了一个 Executors.newCachedThreadPool(); 并执行每个任务。

        public class EventHandler {

            private static final ExecutorService handlers = Executors.newCachedThreadPool();

            public void handleNextEvent(AnEvent event){

                  handlers.execute(new Task(evt)); 

            }   


        public class Task implements Runnable{

        @Override
            public void run() {
            //Event processing
            }
        }

public AnotherClass{    
    public void passEvent(AnEvent evt)//This is called by another thread
    {
      EventHandler.handleNextEvent(evt);

    }
}

我的问题是,如果我调用执行器的execute,我的代码将获取下一个事件并通过执行器运行下一个可运行程序。 我的目的是仅在上一个任务结束后才处理队列中的下一个事件。
我如何知道上一个任务是否已完成,以便我知道我可以再次调用handleNextEvent?
由任务更新某些状态字段是个好主意吗?

谢谢

I have an event queue to process. A thread adds events to the queue.
I have created a runnable Task that in the run method does all which is necessary to process the event.
I have declared an Executors.newCachedThreadPool(); and I execute each Task.

        public class EventHandler {

            private static final ExecutorService handlers = Executors.newCachedThreadPool();

            public void handleNextEvent(AnEvent event){

                  handlers.execute(new Task(evt)); 

            }   


        public class Task implements Runnable{

        @Override
            public void run() {
            //Event processing
            }
        }

public AnotherClass{    
    public void passEvent(AnEvent evt)//This is called by another thread
    {
      EventHandler.handleNextEvent(evt);

    }
}

My problem is that if I call execute of the executor, my code will get the next event and run next runnable via the executor.
My purpose is to process next event from queue only after previous task has ended.
How would I know that the previous task has finished or not so that I know I can call handleNextEvent again?
Is having some status field updated by the Task a good idea?

Thanks

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

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

发布评论

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

评论(2

暗地喜欢 2024-10-07 22:35:59

Executors.newCachedThreadPool() 将根据需要创建新线程,因此这不是您想要的。您需要像 Executors.newSingleThreadExecutor() 这样的东西,它将一次处理一个事件,并将其余事件排队。

请参阅 javadoc

创建一个执行器,该执行器使用在无界队列上运行的单个工作线程。 (但请注意,如果该单个线程由于关闭前执行过程中的失败而终止,则如果需要执行后续任务,一个新线程将取代它。)保证任务按顺序执行,并且不会有超过一个任务处于活动状态在任何给定时间。

Executors.newCachedThreadPool() will create new threads on demand, so it's not what you want. You want something like Executors.newSingleThreadExecutor(), which will process the events one at a time, and queue up the rest.

See javadoc:

Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time.

予囚 2024-10-07 22:35:59

我认为 Executors.newSingleThreadExecutor()submit() 方法可以解决您的问题:http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent /ExecutorService.html

I think Executors.newSingleThreadExecutor() and the submit() Method are the solution to your problem: http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ExecutorService.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文