Javet icon indicating copy to clipboard operation
Javet copied to clipboard

v8runtime中启动Thread执行start调用,线程阻塞

Open zhangxiaokai-chuxia opened this issue 1 month ago • 11 comments

你好,我这边有个场景,需要在js中调用java的thread start方法,发现是串行执行的,需要怎么做,实现并发执行

测试例子: Image

zhangxiaokai-chuxia avatar Nov 24 '25 07:11 zhangxiaokai-chuxia

You may use Module mode so that you can use Promise. In Promise, you can create new threads for async operations.

caoccao avatar Nov 24 '25 15:11 caoccao

收到!!

zhangxiaokai-chuxia avatar Nov 24 '25 15:11 zhangxiaokai-chuxia

能给我一个具体的例子吗,用的不太熟悉,非常感谢

zhangxiaokai-chuxia avatar Nov 25 '25 13:11 zhangxiaokai-chuxia

Please check my project Javenode out.

caoccao avatar Nov 25 '25 20:11 caoccao

非常感谢

zhangxiaokai-chuxia avatar Nov 26 '25 02:11 zhangxiaokai-chuxia

你好通过使用module 方式,可以实现异步了,但是还有一个比较急棘手的问题,我想在异步中实现多线程,测试用发现只使用了一个线程(vert.x-eventloop-thread-0), 本想通过VertxOptions设置一些属性,发现该类获取不到。 测试代码如下:

Image Image Image Image

zhangxiaokai-chuxia avatar Nov 27 '25 02:11 zhangxiaokai-chuxia

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.

caoccao avatar Nov 27 '25 08:11 caoccao

改成如下代码了,如果提交的任务比较多,会报线程阻塞,还有就是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);
                             }
                            } 

zhangxiaokai-chuxia avatar Nov 27 '25 13:11 zhangxiaokai-chuxia

Please read the code carefully. You need to clone.

caoccao avatar Nov 27 '25 20:11 caoccao

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){
  }

zhangxiaokai-chuxia avatar Nov 28 '25 07:11 zhangxiaokai-chuxia

  1. Let an agent scan Javenode.
  2. Ask that agent to write the code you want.
  3. If (1) and (2) doesn't work, let me know.

caoccao avatar Nov 28 '25 12:11 caoccao