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
         }
    }
}

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

Use a Timer to call a method in 60 seconds.

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

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.

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?

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.

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

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

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.