File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

running of code within a specified time limit

 
Ind Sans
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is there any API or options wherein lines of codes are executed within a specified time.

For example:

StartTimer<current time>
code line 1
code line 2
code line 3
code line 4 ---- The code execution stops here itself without proceeding since the given time limit has ended.
code line 5
EndTimer<Current time+10 seconds>


thanks in advance

bharat
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use Thread.sleep(ms) method. Here's a code snippet



but..perhaps i got it wrong..you want all the 5 lines to be executed within a time limit...
 
Rob Spoor
Sheriff
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Run the code in a different thread, then use join:
There is no real way of actually terminating the thread though, so the code will continue in the background. Maybe interrupting the thread can help but I'm not sure.

Alternatively, check out java.util.concurrent.Future. That's a class with a timed get() method that can be canceled.
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could just check the time manually, though this is very tedious:



Or you could use an interface with a List of anonymous classes so you can loop over all the lines:

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic