I'm trying to do a script with PowerShell and I need to execute some Java code in a loop. So I do :
while(something){
     java my_program
}
But my_program takes time and I would like to set a Timeout. How can I do this? 
I'm trying to do a script with PowerShell and I need to execute some Java code in a loop. So I do :
while(something){
     java my_program
}
But my_program takes time and I would like to set a Timeout. How can I do this? 
You could try something like this:
while($something){
    $p = [diagnostics.process]::start("java my_program")
    if ( ! $p.WaitForExit(1000) ) 
    {
        $p.kill()
    }
}
I think this code will work fine for you, you can put while loop condition,
public class JavaTimeout {
    public void myMethod(){
        long startTime = System.currentTimeMillis(); // put the start time
        while((System.currentTimeMillis() - startTime ) < 5000) // if if 5 second end
        {
            // your method do something here 
        }
        System.out.println("5 sec end..."); // print the timeout
    } // end myMethod method
} // end JavaTimeout Class
I hope that helps..