0

Hi
How can I stop a class method execution ?

I have been reading and the most similar solution I found is use Threads....is there another solution ?

Im working with jasperreports, In order to generate report I just call single method to start the report print and another method to read database. How can I stop a job if time is more than 60 seconds?....

public class MiRunnable implements Runnable
{
    private boolean terminar = false;
    public void setTerminar (boolean terminar)
    {
        this.terminar=terminar;
    }
    public void run ()
    {
         while (!terminar)
         {
            // Alguna tarea a realizar
         }
    }
}
3
Contributors
10
Replies
13
Views
7 Years
Discussion Span
Last Post by ardn0001
-1

How can I stop a job if time is more than 60 seconds?

Use a Timer to call a method in 60 seconds.

0

Hmm noup usually that method take like 10min of execution time, I just want stop the method execution if it exceeds 5mins...something like that

0

usually that method take like 10min of execution time

No idea what "that method" is.

just want stop the method execution if it exceeds 5mins

Set the timer for whatever time you want.

0

The methods are:

gen.execute();  <-- usually takes 5 minutes

	reportIndex = gen.getExecutedReportIndex();

	ArrayList files = gen.export(exports); <-- there are cases where this method takes like 20mins!!

How can I interrupt or stop it?

0

In the execute() method, add code to test if anyone wants it to stop. This is usually done with a boolean.
In another thread set a Timer to run for 5 minutes. When the Timer awakens after 5 minutes have it set the boolean telling the execute() method it is to stop.

0

No man! tenes que usar Threads definitivamente mira que es bien facil, hace lo siguiente:

Thread myThread = new Thread(new Runnable(){
                             public void run(){
                         //Las acciones del metodo aqui
                  }
                });

//en otra seccion del programa
...
myThread.interrupt();

//seria mas facil si haces myThread accesible a todos los metodos de la clase.

espero que te haya ayudado, y espero que hables español xD

0

Lo sentimos, no hablo español. ¿Sabes sobre el traductor de Google?

Yes hmm interrupt I was testing with interrupt() but nothing happens... everything seems ok with stop() method, but it is deprecated... I dont'care if information is lost...so sleep and wait methods are not usefull for the project...


Gracias Ricardo si hablo español, crees que el pequeño codigo que tengo sea lo correcto? - Do you think the next code that I post is correct ?

ThanK yOU

import java.lang.Thread;
public class Tester implements Runnable{


   public void run() {
    	while(true){
    		System.out.println( " " + Thread.currentThread().getName());
    	}

	}//run()


	public static void main(String []params){
		Thread th = new Thread ( new Tester() , "Tarea1");
		th.start();

		long timer =0 ;
		long start = System.currentTimeMillis();

		while(timer < 300000){
			long current	=	System.currentTimeMillis();
			timer			=	current-start;
			System.out.println("Current: "+System.currentTimeMillis()+" Start: "+start+" Timer: "+timer/1000);
			if(timer >= 300000)
				th.stop();
		}

		System.out.println("Termina thread main");
	}//main()


}//class
0

Do you think the next code that I post is correct ?

The best way to find out is to compile and execute it and see what happens.

The Timer class will do what your code is trying to do.

Edited by NormR1: n/a

0

Ok Thank you! I think Its ok everything It will go to production TY

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.