gpt4 book ai didi

javascript - 我使用 Node.js 和回调函数的代码是否与 Java 线程一样工作?

转载 作者:行者123 更新时间:2023-11-30 00:06:00 24 4
gpt4 key购买 nike

我是 Node 并发模型的新手。

下面的代码显示了创建 java 线程并同时启动它。

package com.main;

class MyThread implements Runnable{

private int num = 0;

MyThread(int num){
this.num = num;
}

public void run() {
// TODO Auto-generated method stub
try{
System.out.println("Thread "+this.num);
for(int c = 0; c < 5; c++){
System.out.println(" Running thread "+(c+1));
Thread.sleep(2000);
}


}catch(Exception e){
e.printStackTrace();
}

}

}

public class Example01 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread[] ts = null;
try{
ts = new Thread[]{
new Thread(new MyThread(1)),
new Thread(new MyThread(2)),
new Thread(new MyThread(3))
};

for(int x = 0; x < ts.length; x++){
ts[x].start();
}
}catch(Exception e){
System.out.println(e);
}
}

}

从上面的代码可以看出,我启动了 3 个线程,每个线程打印 5 次,中间 hibernate 2 秒。

上面代码的输出是

Thread 1
Thread 2
Running thread 1
Thread 3
Running thread 1
Running thread 1
Running thread 2
Running thread 2
Running thread 2
Running thread 3
Running thread 3
Running thread 3
Running thread 4
Running thread 4
Running thread 4
Running thread 5
Running thread 5
Running thread 5

我用同样的方式写了一段javaScript代码,用Node运行,如下

function forEach(theArray,func){

if(Array.isArray(theArray) === true){
for(var x = 0; x < theArray.length; x++){
func(theArray[x],x);
}
}
}


forEach([1,2,3],function(num,index){

console.log("Thread "+num);

forEach([0,1,2,3,4],function(num,index){
setTimeout(function(){
console.log("Running Thread "+(num+1));
},2000);
});

});

上面代码的输出结果如下,

Thread 1
Thread 2
Thread 3
Running Thread 1
Running Thread 2
Running Thread 3
Running Thread 4
Running Thread 5
Running Thread 1
Running Thread 2
Running Thread 3
Running Thread 4
Running Thread 5
Running Thread 1
Running Thread 2
Running Thread 3
Running Thread 4
Running Thread 5

以上两个代码(javascript 和 java)是否以相同的方式运行?

我对Java的线程等价于javascript回调的理解是否正确?

如果我错了请解释我。

最佳答案

问:我使用 Node.js 和回调函数的代码是否与 Java 线程一样工作?
A:简短回答。

尽管您为 Java 和 NodeJS 获得的打印输出确实使后者看起来像是多线程的。

不幸的是,NodeJS 是使用 Javascript 编程的,这是一种单线程语言,这可以从 mdn 中明显观察到。 JS 文档。也就是说,您不会看到任何与 threads 相关的关键字。

但是请注意,您可以在 NodeJS 中生成子进程。

参见: https://nodejs.org/api/child_process.html

问:然后你可能会问。那么,为什么我会在我的 NodeJS 应用程序中看到这种类似线程的现象?
A: 用于运行应用程序代码 - NodeJS 只有一个线程,它有一个队列(想想 LinkedList)来跟踪事件(代码块) ).

当您启动您的应用程序时。

Node 会从上到下解析每一行代码,类似于 Java 应用程序的运行方式。当它看到 setTimeout 操作时,Node 会将 setTimeout 操作的代码(函数) block 推送到事件队列中并将其标记为(嘿,在最少 2 秒的时间内执行此代码块)

再次。 注意 Node 不保证您的代码在准确的 2 秒内运行,它只保证它会在至少 2 秒内执行。例如。如果主线程被阻塞(无限循环之类的),您会发现 setTimeout 操作的代码块将永远不会执行。

所以,继续前进。因此,在 setTimeout 代码块被插入事件队列后。主线程继续逐行解析 Javascript 代码,直到完成。完成后,队列的下一个代码块将被移动,事件队列将再次开始处理。

这种情况会一直持续下去。当事件队列中没有任何内容时,主线程什么都不做,直到有内容进入队列。

希望这能澄清您对为什么您的 NodeJS 应用程序像多线程一样运行的疑问。

关于javascript - 我使用 Node.js 和回调函数的代码是否与 Java 线程一样工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38540554/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com