Blogger

Delete comment from: Javarevisited

Anonymous said...

...As said, it is useful for short tasks and it's effective for more purpose but the presence of classic synchronization can be a problem in some stress condition. java.util.Timer it's also used in some old JBoss Server or JDBC Driver. It useful to have some standard classes (TimeTask is an Abstract Class to Extends) that define common Task and inside we can put simple logic to know what goes on and when (maybe we will find that over some level it's better another approach)
We need to put attention to have short task, when we use it, we need to cancel unused tasks, we need to purge cancelled tasks. Sometimes, we need to force some clean, playing on internal field with reflection: for example, knowning TimeThread, you can clean and shutdown a leaked Timer...

try {
final Field newTasksMayBeScheduled = ReflectionUtil.findField(thread.getClass(),"newTasksMayBeScheduled");
final Object queue = ReflectionUtil.findField(thread.getClass(), "queue").get(thread);
final Method clear = ReflectionUtil.getDeclaredMethod(queue,"clear");
clear.setAccessible(true);
synchronized (queue) {
newTasksMayBeScheduled.set(thread,false);
clear.invoke(queue);
queue.notify(); }}catch (Exception ex) {...}

Jul 14, 2015, 8:08:33 AM


Posted to What is Timer and TimerTask in Java – Tutorial Example

Google apps
Main menu