my dog learned polymorphism*
The moose likes Java in General and the fly likes Terminating a infinite loop from within the program itself Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Terminating a infinite loop from within the program itself " Watch "Terminating a infinite loop from within the program itself " New topic
Author

Terminating a infinite loop from within the program itself

V Bel
Greenhorn

Joined: Mar 24, 2009
Posts: 4
I am making a website which has java puzzles like "fill in the missing code".
I am compiling and executing the programs online and displaying the results with respect to some test cases.
My problem is when a user tries to implement a program which goes into a infinite loop.
eg,

I want to know how I can time out programs while they are executing so I can halt the execution of programs which exceed a certain timeout or my web server will become too slow if such infinite loops are allowed to be compiled.
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
Run the user code in a new thread. After a predetermined time period if the thread is still running call stop the user thread.

Have a look at this for an example:

http://www.acme.com/resources/classes/Acme/TimeKiller.java
V Bel
Greenhorn

Joined: Mar 24, 2009
Posts: 4
Great Worked for my case...
I am posting the code for further reference for other googlers. I modified your code for the infinite while loop.



Thanks a lot. This is really helpful.
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
I am glad it worked for you.

I modified your code for the infinite while loop


It was not my code, I found it using google.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

I sure hope you're installing a strict SecurityManager, or sooner rather than later, one of the "puzzle solutions" is going to pwn your server.


[Jess in Action][AskingGoodQuestions]
V Bel
Greenhorn

Joined: Mar 24, 2009
Posts: 4
Yes I am working on that too.
Any suggestions on that. ?
Any useful links ?
Thanks
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
one of the "puzzle solutions" is going to pwn your server


Oops I just tried System.exit(), I guess I will have wait until you restart the server to try running System.reformatHardDrive()
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Terminating a infinite loop from within the program itself