I have a TimerTask that downloads e-mails from a POP service, and
processes them. I am using a Timer class with initial delay and period
of 5 seconds.
I have observed that at times, the Timer gets "stuck", as in it fails
to execute the task and then I have to restart the application for the
Timer to be working again.
I initially thought that this was because I did not specify the timeout
in the my connection to the POP service, but aren't the subsequent
executions supposed to be independant of each other, so even if one
execution is halted because of a bad POP service, the next execution
should work just fine.
Has anyone experienced this proble of the Timer getting stuck.I am
using JDK 1.4.2.
Any help will be appreciated.
Thanks
Harsh
Gordon Beaton - 07 Feb 2006 17:22 GMT
> I have a TimerTask that downloads e-mails from a POP service, and
> processes them. I am using a Timer class with initial delay and period
[quoted text clipped - 9 lines]
> even if one execution is halted because of a bad POP service, the
> next execution should work just fine.
Actually no. A single thread is used to run all tasks, each of which
is expected to finish "quickly". If you have tasks that may take
"significant" time to execute, you should spawn threads for them to
run in.
Read the documentation for java.util.Timer, where this is made clear.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
iksrazal@gmail.com - 07 Feb 2006 20:22 GMT
> Has anyone experienced this proble of the Timer getting stuck.I am
> using JDK 1.4.2.
[quoted text clipped - 3 lines]
> Thanks
> Harsh
No, but here's a simple example that shows how to create a thread for
the timer and time it out if the thread doesn't complete in a timely
manner.
import java.util.*;
import java.io.*;
import EDU.oswego.cs.dl.util.concurrent.*;
public class PoolTest
{
class TimeOutTask extends TimerTask
{
Thread t;
TimeOutTask(Thread t)
{
this.t = t;
}
public void run()
{
if(t!= null && t.isAlive())
{
t.interrupt();
}
}
}
class MyRunnable implements Runnable
{
//set as true to be a daemon thread and therefore exit on
interrupt
Timer timer = new Timer(true);
public void run()
{
timer.schedule(new TimeOutTask(Thread.currentThread()), 1000);
try
{
System.out.println("MyRunnable...");
Thread.sleep(10000);
}
catch (InterruptedException ie)
{
System.out.println("MyRunnable error...");
ie.printStackTrace();
}
}
}
public static void main(String args[])
{
new PoolTest();
}
public PoolTest()
{
try
{
PooledExecutor pe = new PooledExecutor(3);
pe.execute(new MyRunnable());
pe.shutdownAfterProcessingCurrentlyQueuedTasks();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
You could of course use now use java.util.concurrent instead of
dl.util.concurrent .
HTH,
iksrazal
http://www.braziloutsource.com/
Roedy Green - 08 Feb 2006 08:59 GMT
>I have a TimerTask that downloads e-mails from a POP service, and
>processes them. I am using a Timer class with initial delay and period
>of 5 seconds.
I think you are hammering rather too often. Try every 60 or 600
seconds. the problem could be at the other end getting pissed with or
confused by two simultaneous logins from the same user.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 08 Feb 2006 09:00 GMT
>I initially thought that this was because I did not specify the timeout
>in the my connection to the POP service, but aren't the subsequent
>executions supposed to be independant of each other,
see http://mindprod.com/jgloss/timer.html
the tasks have to run sequentially. The whole point of a Timer is to
share a single expensive thread object.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Mike Schilling - 09 Feb 2006 01:52 GMT
>I have a TimerTask that downloads e-mails from a POP service, and
> processes them. I am using a Timer class with initial delay and period
[quoted text clipped - 9 lines]
> execution is halted because of a bad POP service, the next execution
> should work just fine.
No, a java.util.Timer runs all of its tasks on a single thread. If one task
hangs, no others will run. Also, if an exception escapes the task, that
will kill the thread and no more tasks will run on it, ever.