Search code examples
javarubymultithreadingjrubythread-sleep

JRuby: closing runScriptlet if it takes too long?


I want to use JRuby to run Ruby scripts.

However, I'd like it so that if a script takes longer than t seconds, it will automatically be closed.

Here's my attempt:

ScriptingContainer ruby = new ScriptingContainer();
int t = 3;

new Thread() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(t * 1000); // Timeout
                        System.out.println("Timeout passed.");
                        ruby.terminate(); // This has no effect?
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }.start();

Object output = ruby.runScriptlet(scriptWithAnInfiniteLoop);
ruby.terminate(); // Terminate without timeout, at the end of the script

Solution

  • Here's an answer using the deprecated Thread.stop():

            ScriptingContainer ruby = new ScriptingContainer();
            int t = 5;
    
            String script = content;
            Thread runner = new Thread() {
                @Override
                public void run() {
                    try {
                        ruby.runScriptlet(script);
                        ruby.terminate(); // Close normally.
                    } catch (Exception e) {
                        ruby.terminate(); // Close if JRuby crashes.
                    }
                }
            };
    
            Thread killer = new Thread() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(t * 1000);
                        runner.stop();
                        ruby.terminate(); // Close forcefully.
                    } catch (Exception e) {}
                }
            };
    
            runner.start();
            killer.start();
    

    See this article on why it is deprecated: https://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html

    Since I am using a deprecated method I won't mark this as an official answer.