import org.jruby.CompatVersion;
import org.jruby.RubyInstanceConfig.CompileMode;
import org.jruby.embed.EmbedEvalUnit;
import org.jruby.embed.LocalContextScope;
import org.jruby.embed.ScriptingContainer;
public class ThreadRegexTest2 implements Runnable {
private static ScriptingContainer makeRuntime() {
ScriptingContainer runtime = new ScriptingContainer(LocalContextScope.CONCURRENT);
runtime.setCompileMode(CompileMode.JIT);
runtime.setCompatVersion(CompatVersion.RUBY1_9);
runtime.runScriptlet(
"module RegexTest\n" +
" CHECK = {\n" +
" :email => lambda{|str|\n" +
" raise 'bad email' unless str =~ /@/\n" +
" str.downcase\n" +
" },\n" +
" :username => lambda{|str|\n" +
" str[/\\A(\\w+)\\z/, 1] or raise 'bad username'\n" +
" }\n" +
" }\n" +
"\n" +
" def self.check(type, str)\n" +
" raise 'is nil!' if str.nil?\n" +
" CHECK[type].call(str)\n" +
" end\n" +
"end\n"
);
return runtime;
}
private final ScriptingContainer runtime;
public ThreadRegexTest2(ScriptingContainer runtime) {
this.runtime = runtime;
}
public void run() {
System.out.println("running thread " + Thread.currentThread().getId());
EmbedEvalUnit eval = runtime.parse(
"RegexTest.check(:email, 'f...@example.com')\n" +
"RegexTest.check(:username, 'foo')\n"
);
while (true) eval.run();
}
public static void main(String[] args) {
ScriptingContainer runtime = makeRuntime();
new Thread(new ThreadRegexTest2(runtime)).start();
new Thread(new ThreadRegexTest2(runtime)).start();
}
}