Timer gets stuck

H

hagarwal

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
 
G

Gordon Beaton

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.

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
 
I

iksrazal

(e-mail address removed) escreveu:
Has anyone experienced this proble of the Timer getting stuck.I am
using JDK 1.4.2.

Any help will be appreciated.

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/
 
R

Roedy Green

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.
 
R

Roedy Green

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.
 
M

Mike Schilling

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top