开发者

I need clarification on the ThreadpoolExecutor beforeExecute and afterExecute hooks

开发者 https://www.devze.com 2023-04-11 18:01 出处:网络
I am using threadpools in my application. I have subclassed the TreadPoolExecutor and overriden the methods beforeExecute, afterExecute and terminated for statistical purposes.

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 invokeAl开发者_开发技巧l 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,


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号