class ThreadPoolExecutor {}
class ScheduledThreadPoolExecutor extends ThreadPoolExecutor {} // derived class
interface Enhance {
default Runnable enhanceTask(Runnable command) {
// wrap runnable
...
}
}
class CommonThreadPoolExecutor extends ThreadPoolExecutor implements Enhance {}
class CommonScheduledThreadPoolExecutor extends ScheduledThreadPoolExecutor implements Enhance {}
If I want to enhance the execute
method in ThreadPoolExecutor
, I must write same @Override
twice:
class CommonThreadPoolExecutor extends ThreadPoolExecutor implements Enhance {
@Override
public void execute(@NonNull Runnable command) {
Runnable runnable = enhanceTask(command);
super.execute(runnable);
}
}
class CommonScheduledThreadPoolExecutor extends ScheduledThreadPoolExecutor implements Enhance {
@Override
public void execute(@NonNull Runnable command) {
Runnable runnable = enhanceTask(command);
super.execute(runnable);
}
}
Must keep instances is assignable to ThreadPoolExecutor
or ScheduledThreadPoolExecutor
.
New contributor
Manjiz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.