Timing a Task in Java – Find Out How!

  • Context: Java 
  • Thread starter Thread starter frankfjf
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
SUMMARY

This discussion focuses on implementing a Java program that utilizes threading to manage long-running calculations within a for loop. The solution involves creating a new thread for each calculation using the ExecutorService interface, allowing the program to interrupt calculations that exceed a 20-second execution time. The provided code demonstrates how to use Future to handle timeouts and manage the execution of lengthy calculations effectively.

PREREQUISITES
  • Java programming language proficiency
  • Understanding of threading concepts in Java
  • Familiarity with the ExecutorService interface
  • Knowledge of handling exceptions in Java, particularly TimeoutException
NEXT STEPS
  • Explore Java's ExecutorService for advanced threading techniques
  • Learn about Java's Future and how to handle asynchronous tasks
  • Investigate the TimerTask class for scheduling tasks in Java
  • Study best practices for managing thread lifecycles and resource cleanup
USEFUL FOR

Java developers, software engineers, and anyone interested in optimizing task execution and performance in multithreaded applications.

frankfjf
Messages
166
Reaction score
0
Hi PF,

I am trying to write a program that utilizes a for loop that skips the current iteration (via continue) if it takes too long (for instance, longer than 20 seconds, else it finishes the current iteration as per normal), but am not sure how to best do this.

Does anyone know of a way to do this?
 
Technology news on Phys.org
Basically your problem boils down to needing the ability to interrupt a set of calculations at will. In this case, you want to interrupt a set of calculations inside a for loop after 20 seconds has passed on a given loop iteration, and then skip to the next iteration. To do this you need to use threading.

The general idea is to perform the calulations in a new Thread, which you will kill after 20 seconds if it is still running, start the thread inside your for loop and wait for it to finish before going to the next iteration. You can either do so directly, by creating a Runnable for your set of calculations, and a Thread instance for your Runnable, and using Thread.join to wait for the thread to die, and killing it internally after 20 seconds using either a Timer or System.currentTimeMillis(), or take advantage of the TimerTask class or ExecutorService interface (if you are using a newer version of java). Here is an example using ExecutorService:

Code:
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import static java.lang.Math.random;

public class TestLoopInterrupt {

	public static void main(String[] args) {
		
		// Create an array with 10 calculation objects
		CalculationObject[] calcObjects = new CalculationObject[10];
		
		/* This is our for loop where we will do lengthy calculations 
		on a bunch calculation objects in an array. Each call to 
		performCalculation() will last a maximum of 20 seconds*/
		for(CalculationObject o : calcObjects) {
			/*Create a calculation object with some random number as an
			 attribute and then perform some lenghty calculations on it
			 and output the result */	
			o = new CalculationObject((double)(random()*10));
			System.out.println("Number was " + o.getNum());
			o = performCalculation(o);
			System.out.println("Number is " + o.getNum());
		}		
	}
	
	private static CalculationObject performCalculation(CalculationObject o) {
		/*Create an Executor that uses a single worker thread operating
		off an unbounded queue*/
		final ExecutorService service = Executors.newSingleThreadExecutor();
		
		/*Create instance of our LengthyCalculation class, passing our
		Calculation Object as a parameter to be used in the calculation*/
		LengthyCalculation lengthyCalculation = new LengthyCalculation(o);

        try {
        	//Use a Future to represent the result of our calculations
            final Future<CalculationObject> f = service.submit(lengthyCalculation);
            /*Get the result, waiting up to 20 seconds. 
            A TimeoutException is thrown if the calculation has not been
            completed in 20 seconds.*/
            o = f.get(20, TimeUnit.SECONDS);
        } catch (final TimeoutException e) {
            System.err.println("Calculation took to long");
        } catch (final Exception e) {
            throw new RuntimeException(e);
        } finally {
            service.shutdown();
			service.shutdownNow();
        }
        
        return o;
	}

}

Code:
//Some object that contains attributes we will perform calculations on
public class CalculationObject {
	private double num;
		
	public CalculationObject(double num) {
		this.num = num;
	}
		
	public double getNum() {
		return num;
	}
}

Code:
import java.util.concurrent.Callable;

public class LengthyCalculation 
			 implements Callable<CalculationObject> {
			 
	private CalculationObject o;
		
	public LengthyCalculation(CalculationObject o) {
		this.o = o;
	}
	
	@Override
	public CalculationObject call() throws Exception {
		double num = o.getNum();
		while(num<5) {
			num += 0.0000000004;
		}
		return new CalculationObject(num);
	}
		
}

This performs some lengthy calculation on some calculation objects in an array, using a for loop. If any of the lentghy calculations take longer than 20 seconds to execute, the calculation is topped and the next ieration of the for loop is processed.
 
Oh yeah, I figured I would need threading but wasn't sure how to go about getting started on it. This could work well, thank you.
 

Similar threads

  • · Replies 14 ·
Replies
14
Views
6K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
2K
Replies
1
Views
2K