1

I am working on a Java EE Based application, where I need to PDF reports using itext API. Basically my requirement is that, if the report generation takes more than 1 minute then stop it.

So for this I am implementing this business requirement in this way

Thread t = new Thread(myPojoclass); 

long startTime = System.currentTimeMillis();
long endTime = startTime + 60000;

t.start();

while (System.currentTimeMillis() < endTime) 
{

}

t.interrupt();  // Tell the thread to stop

And inside my myPojoclass as mentioned above, this is a Thread which implements Runnable interface and inside its run method it is consisting of connecting to Database and getting the results and giving it to an ArrayList

Please let me know if this is correct ??

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Pawan
  • 31,545
  • 102
  • 256
  • 434
  • These posts has answer to your question: http://stackoverflow.com/q/2758612/948268 and http://stackoverflow.com/q/1247390/948268 – Kuldeep Jain Mar 29 '12 at 09:36

2 Answers2

2

there is a good solution here. Since your thread is connecting to a database I strongly recommend (if you haven't done it already) to catch the InterruptException exception and within its block close your connection and rollback if necessary.

Community
  • 1
  • 1
giorashc
  • 13,691
  • 3
  • 35
  • 71
0

you can use executor service

ScheduledExecutorService executor = Executors.newScheduledThreadPool(2); 
 final Future handler = executor.submit(new Callable(){ ... });
 executor.schedule(new Runnable(){
     public void run(){
         handler.cancel();
     }      
 }, 60000, TimeUnit.MILLISECONDS);

more details ExecutorService that interrupts tasks after a timeout

Community
  • 1
  • 1
Oscar Castiblanco
  • 1,626
  • 14
  • 31