v8runtime中启动Thread执行start调用,线程阻塞
你好,我这边有个场景,需要在js中调用java的thread start方法,发现是串行执行的,需要怎么做,实现并发执行
测试例子:
You may use Module mode so that you can use Promise. In Promise, you can create new threads for async operations.
收到!!
能给我一个具体的例子吗,用的不太熟悉,非常感谢
Please check my project Javenode out.
非常感谢
你好通过使用module 方式,可以实现异步了,但是还有一个比较急棘手的问题,我想在异步中实现多线程,测试用发现只使用了一个线程(vert.x-eventloop-thread-0), 本想通过VertxOptions设置一些属性,发现该类获取不到。 测试代码如下:
You need to sends tasks in the callbacks, let the thread pool process the tasks. In the task callback, resolve the Promise. You need to use Promise. There are samples in Javenode.
改成如下代码了,如果提交的任务比较多,会报线程阻塞,还有就是promise异步处理时,会出现runtime close,事例代码
import { setImmediate } from 'timers/promises';
const a = []
function adkPromise(value) {
try{
var pro= setImmediate(value);
a.push(pro);
}catch(err){
pp.print("callback-1 (adkPromise) ================ error "+err);
}
}
Please read the code carefully. You need to clone.
My test code looks like this, but it still can't execute concurrently. When I write an infinite loop in the JavaScript, TestPromise doesn't execute. If possible, could you provide a simple example that can execute asynchronously for reference? Thank you.
function TestPromise(call) {
setImmediate(()=>{
var ps= new Promise((resolve, reject) => {
try {
resolve(call);
} catch(err) {
reject(err);
}
});
threadpool.submit(()=>{
ps.then((result) => {
result();
}).catch((err) => {
});
});
});
//js test example:
TestPromise(() => {
while(true) {
pp.print("callback-2 (test) ================ execute");
}
});
//just for test
while(true){
}
- Let an agent scan Javenode.
- Ask that agent to write the code you want.
- If (1) and (2) doesn't work, let me know.