我需要澄清 ThreadpoolExecutor beforeExecute 和 afterExecute 挂钩

发布于 2024-12-08 04:59:07 字数 1102 浏览 1 评论 0原文

我在我的应用程序中使用线程池。我已经对 TreadPoolExecutor 进行了子类化,并重写了 beforeExecute、afterExecute 和终止方法以用于统计目的。
我还实现了自己的 ThreadFactory 并重写了 newThread 方法。

据我所知,线程池包装类创建了十几个“可调用”任务并调用 invokeAll 方法来获取结果。每个任务都有一个接口对象。基本 X 对象实现该接口并已被子类化多次。因此,当执行线程池时,它会启动对象 X 的子对象。

从代码角度来看,它看起来有点像:

the wrapper threapool class:

List<DoTask> tasks;     

tasks.add(new DoTask(new A("A"));
tasks.add(new DoTask(new B("B"));
tasks.add(new DoTask(new C("C"));

results = threadpool.invokeAll(tasks, 60, TimeUnit.Seconds);

in my DoTask class: public DoTask implements Callable

constructor: DoTask(ifaceX x) 

im my Base class X: public class X implements ifaceX
In my child class A, B, C: public A extends X

我的问题是,当我调用 before 和 afterexecute 挂钩时,如何检索可调用任务中保存的信息?或者我想我想做的是为线程池的每个线程指定一个特定的名称,这可能吗?

我可以清楚地看到隐藏在 Runnable 对象/FutureTask/callable 中的调试模式下 Eclipse 变量视图中的信息。

我不明白为什么我只需要使用 Runnable 对象覆盖方法(beforeExecute、afterExecute),而无需使用 Callable 对象(因为我需要检索结果)。我缺少或不明白什么?也许我需要子类化 FutureTask 不确定?

感谢您的帮助,

I am using threadpools in my application. I have subclassed the TreadPoolExecutor and overriden the methods beforeExecute, afterExecute and terminated for statistical purposes.
I also have implemented my own ThreadFactory and overriden the newThread method.

I understand that the threadpool wrapper class creates a dozen of "callable" tasks and calls the invokeAll method to get the results. In each task there is an interface object. The base X object implements the interface and has been subclassed a number of times. So when the threadpool is executed it launches childs of the object X.

From a code perspective it looks a bit like:

the wrapper threapool class:

List<DoTask> tasks;     

tasks.add(new DoTask(new A("A"));
tasks.add(new DoTask(new B("B"));
tasks.add(new DoTask(new C("C"));

results = threadpool.invokeAll(tasks, 60, TimeUnit.Seconds);

in my DoTask class: public DoTask implements Callable

constructor: DoTask(ifaceX x) 

im my Base class X: public class X implements ifaceX
In my child class A, B, C: public A extends X

My questions is, how to retrieve information kept in the callable task when I call the before and after execute hooks? or I guess what I am trying to do is give a specific name to each thread of the threadpool is that possible?

I can clearly see the information is there in the Eclipse variables view in debug mode hidden in the Runnable object/FutureTask/callable.

I do not understand why I only have to override methods (beforeExecute, afterExecute) with Runnable objects and no Callable ones (since I need to retrieve results). Something I am missing or do not understand? Maybe I need to subclass FutureTask not sure?

Thanks for your help,

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

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

发布评论

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

评论(1

兮子 2024-12-15 04:59:07

如果您希望在每次调用之前和之后发生事情,而不是覆盖 beforeExecuteafterExecute,那么对 DoTask 进行超类会更容易。

编辑:此外,如果您使用可调用对象,您可能需要使用 ExecutorService

It would be easier to superclass DoTask if you want things to happen before and after each call instead of overriding beforeExecute and afterExecute.

Edit: Also, if you're using callables, you might want to use an ExecutorService.

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