---
title: "关于mina的StreamIoHandler"
author: "wwty"
date: "2010-06-19T22:12:01Z"
tags: ["Apache", "Mina", "thread"]
categories: ["编程语言"]
slug: "mina-streamiohandler"
draft: false
lastmod: "2010-06-19T22:12:01Z"
id: 694771
url: "https://wwty.iteye.com/blog/694771"
---

在网上看到这样一段讨论：

I was looking at the 2 HTTP examples in Mina1.1, and have always wondered  
this:  Why, if at line 50 of  
org.apache.mina.example.httpserver.stream.HttpProtocolHandler a comment says  
"You \*MUST* execute stream I/O logic in a separate thread." there is a  
stream based HTTP server example?  Is this just for illustrative purposes  
and not intended for production level systems?  Or does this comment not  
hold true anymore?

问题说的是针对mina提供的一个http协议的例子说的，提供的例子中HttpProtocolHandler继承自StreamIoHandler。

 

As you know, InputStream.read() is a blocking operation.  If you block  
the current I/O processor thread (or the thread pool thread) by  
calling read(), it's simply a dead lock because the I/O processor (or  
the event dispatcher) can't perform any I/O operation when you wait  
for data.  That's why you need to spawn a new thread or use a  
different thread pool.

回答的大致意思是说InputStream.read() 是一个阻塞的方法，为了应付阻塞，需要用线程来解决问题，所以建议不使用StreamIoHandler