我需要澄清 ThreadpoolExecutor beforeExecute 和 afterExecute 挂钩
我在我的应用程序中使用线程池。我已经对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望在每次调用之前和之后发生事情,而不是覆盖
beforeExecute
和afterExecute
,那么对DoTask
进行超类会更容易。编辑:此外,如果您使用可调用对象,您可能需要使用
ExecutorService
。It would be easier to superclass
DoTask
if you want things to happen before and after each call instead of overridingbeforeExecute
andafterExecute
.Edit: Also, if you're using callables, you might want to use an
ExecutorService
.