java - How to access thread objects inside custom threadpool executor's methods? -
i want access data inside runnable objects have in custom threadpool executor. if tried access in before/after execute methods getting class cast exception. how resolve scenario.
public class mythread implements runnable { string key; public void run(){ /* */} } public class myexecutor extends threadpoolexecutor { @override protected void beforeexecute(thread paramthread, runnable paramrunnable) { mythread mt = (mt)paramrunnable; } @override protected void afterexecute(runnable paramrunnable, throwable paramthrowable) { mythread mt = (mt)paramrunnable; /* need access "key" inside mythread */ }
if classcastexception
means pass in thread implementations not mythread
or subclass of mythread
myexecutor
. in order fix instanceof
check before casting.
public class myexecutor extends threadpoolexecutor { @override protected void beforeexecute(thread paramthread, runnable paramrunnable) { if(paramrunnable instanceof mythread) { mythread mt = (mythread)paramrunnable; } } @override protected void afterexecute(runnable paramrunnable, throwable paramthrowable) { if(paramrunnable instanceof mythread) { mythread mt = (mythread)paramrunnable; } /* need access "key" inside mythread */ }
Comments
Post a Comment