eclipsesource / eclipsesource/J2V8

Invalid V8 thread access: current thread is Thread[Timer-0,5,main] while the locker has thread Thread[main,5,main]

Open
#509 13 comments 1 reaction 0 assignees View on GitHub
Dominant language
Java
Stars
2.6k
Forks
387
PR merge metrics
No merged PRs in 30d

Description

I got an error when running the code below
```
class TestV8 {
public static void main() {

V8 runtime = V8.createV8Runtime("global");
Console console = new Console();
V8Object v8Console = new V8Object(runtime);
runtime.add("console", v8Console);
v8Console.registerJavaMethod(console, "log", "log", new Class[] { String.class });
v8Console.registerJavaMethod(console, "err", "err", new Class[] { String.class });

JavaVoidCallback callback = (receiver, parameters) -> {
Timer timer = new Timer();
System.out.println("first param:"+parameters.getObject(0).toString());
System.out.println("second param:"+parameters.getInteger(1));
timer.schedule(new TimerTask() {
@Override
public void run() {
V8Function v8Function = (V8Function)parameters.getObject(0);
v8Function.call(v8Function, null);
v8Function.close();
}
}, parameters.getInteger(1));

};

runtime.registerJavaMethod(callback, "setTimeout");
runtime.executeScript("setTimeout(function() {console.log(123123)}, 10000);");
}
}
```
Maybe i did something wrong...?? how to fix it ?
thanks for your help

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.