首页 \ 问答 \ Java IO vs NIO vs Tasks队列(Java IO vs NIO vs Tasks queue)

Java IO vs NIO vs Tasks队列(Java IO vs NIO vs Tasks queue)

我已经看到很多主题将较旧的Java io模型与较新的java nio模型进行比较,前者是同步/阻塞,后者是异步/非阻塞。 因为nio是非阻塞的,所以当需要同时处理大量并发连接而不分配由于上下文切换/内存使用而无法很好地扩展的大量线程时,它比java io更合适。

我对这个论点的问题是IO与NIO的比较总是给出每个连接使用一个线程的IO的例子。 使用java IO的开发人员是否可以简单地分配有限数量的线程(线程池)来阻止IO操作(即文件读取或数据库查询)并对它们进行排队? 假设我正在使用java的ServerSocket类从头开始创建一个http服务器。 假设我收到客户端的请求,要求我进行数据库查询,这是由于JDBC规范而导致的阻塞操作。 难道我不能简单地将数据库查询排队到ThreadPool,并在池完成处理时将回调交给作业运行吗? 当然,我正在分配线程来处理io-bounds请求,但线程的数量是有限的。 由于IO操作通常是同步的(在某种程度上),尝试为每个db查询分配一个线程,或者每个文件的读/写线程都是没有意义的。

这样,您可以获得线程和异步编程的好处,而不会分配太多的线程。

我在这个模型中看到的唯一弱点是,如果所有io-bound操作都被卡住(可能是由于编程错误),将来的排队请求将被暂停,直到它们被解除或超时。 主逻辑仍然是并发的,但IO不是。

那么,问题:NIO是否解决了我上面描述的模型无法解决的问题(我刚才提到的潜在弱点)?


I've seen a lot of topics comparing the older Java io model to the newer java nio model, the former being synchronous/blocking and the latter being asynchronous/non-blocking. Because nio is non-blocking, it is more suitable than java io when a large number of concurrent connections need to be processed concurrently without allocating a large number of threads which does not scale well due to context switching/memory usage.

The problem I have with this argument is that comparisons of IO to NIO always give examples of IO using a thread per connection. Could a developer using java IO simply allocate a finite number of threads (thread pool) for blocking IO operations (ie file reading or database queries) and queue them? Let's say I'm making an http server from the ground up using java's ServerSocket class. Let's say I get a request from a client that requires me to make a database query which is a blocking operation due to the JDBC spec. Could I not simply queue the database query to a ThreadPool and hand the job a callback to run when the pool finishes handling it? Sure, I'm allocating threads to handle io-bounds requests, but the number of Threads are finite. Since IO operations are typically synchronous (to a degree), it would be pointless to try to allocate a thread per db query, or thread per file read/write.

This way you can get the benefits of Threads and asynchronous programming without the consequence of allocating too many Threads.

The only weakness I can see in this model is if all io-bound operations get stuck (possibly due to programming error) future queued requests will be put on hold until they either get unstuck, or time out. The main logic is still concurrent, but the IO is not.

So, question: Does NIO solve any problems that just can't be solved with the model I've described above (bar the potential weakness I just mentioned)?


原文:https://stackoverflow.com/questions/42702951
更新时间:2023-11-16 06:11

最满意答案

请尝试下面的代码:

 $(function(){
    $('#dataTableId').on('click','.cmpny_id',function(){
         alert("hi"); var isChecked = $('.cmpny_id').is(':checked'); 
    });
 });

根据您的表ID更改dataTableId


$(document.body).on('click', '.cmpny_id', function(event) {

var isChecked = $('.cmpny_id').is(':checked'); });

this worked for me:)

相关问答

更多