Comments (8)
感謝提供範例,我試試看。
from quickjs-wrapper.
這個 Timer 可行,太棒了,謝謝。
from quickjs-wrapper.
哦 看來不適合我的專案
我的 ctx 是在一個 newSingleThreadExecutor 下建立的
無法使用 new Handler 再去跑 js func
from quickjs-wrapper.
newSingleThreadExecutor 里的线程你改成 HandlerThread 的话,就可以使用 Handler.post 来实现 setTimeout 了
from quickjs-wrapper.
from quickjs-wrapper.
測試好像不行
可能是因為前面 thread.start(); 啟動過了
下面這個如果有 submit runnable 進來
Executors.newSingleThreadExecutor(r -> thread);
thread 會在 strart() 一次
會出現 java.lang.IllegalThreadStateException
from quickjs-wrapper.
估计 Executors 对 Thread 有一些特殊处理,那你把前面的 thread.start 去掉试试呢?如果不行的话,建议下面两个方案:
- 直接使用 HandlerThread 替代 Executors.newSingleThreadExecutor
- 使用 TimerTask 来实现延迟执行,或者其他可以实现延迟功能的 api
类似代码如下:
private final Timer timer = new Timer("timer");
public Spider() {
}
public Spider(String api) {
this.executor = Executors.newSingleThreadExecutor();
this.key = "__" + UUID.randomUUID().toString().replace("-", "") + "__";
this.api = api;
}
public void setTimeout() {
this.executor.submit(() -> {
// In Javascript test: setTimeout(1000, () => { console.log('executed'); })
ctx.getGlobalObject().setProperty("setTimeout", args -> {
int delayTime = (int) args[0];
JSFunction callback = (JSFunction) args[1];
callback.hold();
timer.schedule(new TimerTask() {
@Override
public void run() {
// 这里需要切回到原线程执行
executor.submit(() -> {
callback.call();
});
}
}, delayTime);
return null;
});
});
}
from quickjs-wrapper.
好的,感謝,我在調整看看。
from quickjs-wrapper.
Related Issues (20)
- How to addJavascriptInterface?
- 大神您好,有個問題想請教 HOT 11
- 請問更新至 0.19.1 有需要調整什麼嗎? HOT 22
- Todo: JVM 下不同平台的动态链接库支持
- java中什么获取 js Promise 和 async function 的结果呢? HOT 2
- https://github.com/bellard/quickjs/issues/187
- 能否支持JSRuntime创建多个JSContext上下文? HOT 4
- Console 沒有顯示 JS 錯誤訊息 HOT 5
- 問題詢問 HOT 9
- 能否支持Java byte写入js的ArrayBuffer? HOT 5
- 是否支持多线程? HOT 2
- 是否支持多线程?
- com.whl.quickjs.wrapper.QuickJSException: not a function HOT 9
- ModuleLoader 加载ByteCode后的模块那么路径就会有问题 HOT 8
- Quickjs优化版 HOT 3
- 是否支持创建多个的JSRunTime
- NewStringUTF doesn't support four byte unicode in JVM platform
- Update quickjs HOT 2
- [Feature Request] Make QuickJsContext implement Closeable HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from quickjs-wrapper.