简体   繁体   English

中断驱动与事件驱动的I / O模型

[英]Interrupt driven vs Event Driven I/O model

While reading Node JS , the definition says that the I/O model is event Driven. 在阅读Node JS时,定义说I / O模型是事件驱动的。 How is this different than interrupts in multi threading environment ? 这与多线程环境中的中断有何不同? In a multithreading environment, when I/O operation is completed , an interrupt is generated and waiting thread is now pushed in read to run state. 在多线程环境中,当I / O操作完成时,会生成一个中断,并且等待线程现在被推入读取运行状态。 In Node Js, based on the event thrown after the I/O is completed, the callback handler is pushed to the event Queue. 在Node Js中,基于I / O完成后抛出的事件,回调处理程序被推送到事件Queue。

Why are the two different? 为什么两者不同?

I'm not sure that I understand the question but it is my understanding that... 我不确定我是否理解这个问题,但我理解......

The two concepts are different in that they apply to processing at a different level. 这两个概念的不同之处在于它们适用于不同级别的处理。 Events are employed at a higher level (system/code) than interrupts, which happen at lower level (CPU). 事件的使用级别(系统/代码)高于中断,后者发生在较低级别(CPU)。 So even though the interrupts do happen at the CPU level, nodejs abstracts them (using a separate thread for I/O) that will execute a callback if defined. 因此,即使中断确实发生在CPU级别,nodejs也会抽象它们(使用单独的I / O线程),如果已定义,它将执行回调。 What a multi-threaded environment, such as Java, does is suspend and resume threads. 多线程环境(例如Java)的作用是暂停和恢复线程。 This is not the same as the CPU-level Interrupts. 这与CPU级中断不同。 Java does let the programmer implement interrupts like this: Java确实让程序员实现了这样的中断:

for (int i = 0; i < importantInfo.length; i++) {
    // Pause for 4 seconds
    try {
        Thread.sleep(4000);
    } catch (InterruptedException e) {
        // We've been interrupted: no more messages.
        return;
    }
    // Print a message
    System.out.println(importantInfo[i]);
}

but this is not at all like a CPU interrupt or a nodejs Event/Callback. 但这根本不像CPU中断或nodejs事件/回调。

Even though nodejs is said to be single threaded, it does use multiple threads, however it only uses one main thread that is designed to spend little time on the CPU and pass on other IO or CPU intensive tasks to other tasks, so that is may be ready for handling the next (typically http) request 即使nodejs被称为单线程,它确实使用多个线程,但是它只使用一个主线程,该线程旨在花费很少的时间在CPU上并将其他IO或CPU密集型任务传递给其他任务,因此可能准备好处理下一个(通常是http)请求

Some reference links: 一些参考链接:

http://www3.ntu.edu.sg/home/ehchua/programming/java/J5e_multithreading.html http://www3.ntu.edu.sg/home/ehchua/programming/java/J5e_multithreading.html

http://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html http://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html

Difference between interrupt and event 中断和事件之间的区别

https://softwareengineering.stackexchange.com/questions/298493/nodejs-like-interrupt-handlers https://softwareengineering.stackexchange.com/questions/298493/nodejs-like-interrupt-handlers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM