本文介绍了“路由"是指threadpoolexecutor内部的任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有实现Runnable的类

I have class which implements Runnable

public class MyRunnable implements Runnable {
    private long RID = <SOME_LONG_ID_VALUE>;
    public long getRID() { return this.RID; }
    public void run {
        System.out.println("HAI!");
    }
}

是否有一种方法可以通过RID字段路由" ThreadPoolExecutor内部的任务(我的意思是,如果线程N 3使用RID = 1运行Runnable,则必须通过线程N 3执行下一次使用RID = 1的品尝(如果还活着)谢谢.

Is there a way to "route" tasks inside ThreadPoolExecutor by RID field ( i mean, if thread N 3 runs Runnable with RID = 1, then next tast with RID = 1 must be executed by thread N 3 (if it's alive))Thank you.

推荐答案

据我了解,对于每个RID,您都有一个执行上下文,并且希望针对该上下文依次执行所有MyRunnable对象.

As I understand, for each RID you have an execution context and you want to execute all MyRunnable objects serially against that context.

您的错误是您尝试将上下文链接到线程.更好地由演员来表示这种情况.您可以使用Akka等广为人知的Actor框架,但出于您的目的,请简单地演员实现会起作用.

Your mistake is that you try to link that context to a thread. Better represent that context by an Actor. You can use widely known Actor frameworks like Akka, but for your purpose, simple Actor implementation would work.

另一观点:由于Actor实现具有唯一的接口方法execute,因此可以将其视为串行执行器,以串行方式运行提交的任务.而且,由于它没有自己的线程池,而是使用所有Actor之间共享的另一个执行程序,因此可以称为辅助执行程序.

Another point of view: since the Actor implementation has the only interface method execute, it can be considered as a serial executor, running submitted tasks serially. And since it has no own threadpool but uses another executor, shared between all Actors, it can be called a Secondary Executor.

这篇关于“路由"是指threadpoolexecutor内部的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 15:04