如果您正在使用NIO,我还建议您使用框架.我一直在使用

Apache Mina,我会推荐它.

至于阻塞IO,基本上你需要一个监听器线程来接受传入的连接并产生将处理每个连接的其他线程.下面是一个这样的Listener代码的示例,最初是为Apache Felix Project提供的.

如果您查找完整但已修改的版本,则可以使用browse the source here.

例如

/*

* Licensed to the Apache Software Foundation (ASF) under one or more

* contributor license agreements. See the NOTICE file distributed with

* this work for additional information regarding copyright ownership.

* The ASF licenses this file to You under the Apache License, Version 2.0

* (the "License"); you may not use this file except in compliance with

* the License. You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package org.apache.felix.shell.remote;

import java.io.IOException;

import java.io.PrintStream;

import java.net.ServerSocket;

import java.net.Socket;

import java.net.SocketException;

/**

* Implements a simple listener that will accept a single connection.

*

*

* @author Dieter Wimberger (wimpi)

*/

class Listener

{

private int m_Port;

private Thread m_ListenerThread;

private boolean m_Stop = false;

private ServerSocket m_ServerSocket;

private AtomicInteger m_UseCounter;

private int m_MaxConnections;

/**

* Activates this listener on a listener thread (telnetconsole.Listener).

*/

public void activate()

{

//configure from system property

try

{

m_Port = Integer.parseInt( System.getProperty( "osgi.shell.telnet.port", "6666" ) );

}

catch ( NumberFormatException ex )

{

Activator.getServices().error( "Listener::activate()", ex );

}

try

{

m_MaxConnections = Integer.parseInt( System.getProperty( "osgi.shell.telnet.maxconn", "2" ) );

}

catch ( NumberFormatException ex )

{

Activator.getServices().error( "Listener::activate()", ex );

}

m_UseCounter = new AtomicInteger( 0 );

m_ListenerThread = new Thread( new Acceptor(), "telnetconsole.Listener" );

m_ListenerThread.start();

}//activate

/**

* Deactivates this listener.

*

* The listener's socket will be closed, which should cause an interrupt in the

* listener thread and allow for it to return. The calling thread joins the listener

* thread until it returns (to ensure a clean stop).

*/

public void deactivate()

{

try

{

m_Stop = true;

//wait for the listener thread

m_ServerSocket.close();

m_ListenerThread.join();

}

catch ( Exception ex )

{

Activator.getServices().error( "Listener::deactivate()", ex );

}

}//deactivate

/**

* Class that implements the listener's accept logic as a Runnable.

*/

private class Acceptor implements Runnable

{

/**

* Listens constantly to a server socket and handles incoming connections.

* One connection will be accepted and routed into the shell, all others will

* be notified and closed.

*

* The mechanism that should allow the thread to unblock from the ServerSocket.accept() call

* is currently closing the ServerSocket from another thread. When the stop flag is set,

* this should cause the thread to return and stop.

*/

public void run()

{

try

{

/*

A server socket is opened with a connectivity queue of a size specified

in int floodProtection. Concurrent login handling under normal circumstances

should be handled properly, but denial of service attacks via massive parallel

program logins should be prevented with this.

*/

m_ServerSocket = new ServerSocket( m_Port, 1 );

do

{

try

{

Socket s = m_ServerSocket.accept();

if ( m_UseCounter.get() >= m_MaxConnections )

{

//reject with message

PrintStream out = new PrintStream( s.getOutputStream() );

out.print( INUSE_MESSAGE );

out.flush();

//close

out.close();

s.close();

}

else

{

m_UseCounter.increment();

//run on the connection thread

Thread connectionThread = new Thread( new Shell( s, m_UseCounter ) );

connectionThread.start();

}

}

catch ( SocketException ex )

{

}

}

while ( !m_Stop );

}

catch ( IOException e )

{

Activator.getServices().error( "Listener.Acceptor::activate()", e );

}

}//run

}//inner class Acceptor

private static final String INUSE_MESSAGE = "Connection refused.\r\n"

+ "All possible connections are currently being used.\r\n";

}//class Listener

您可以找到其他示例here和here.

请注意,当您有更多负载时,NIO优于阻塞模型的优势就会发挥作用.从某一点来看,线程创建,管理和上下文切换的额外工作量将限制您的系统性能.

java实现io阻塞的代码,Java:完整的每个连接线程阻塞IO与NIO的代码示例?相关推荐

  1. Java阶段性测试--知识点:数组,面向对象,集合、线程,IO流

    #Java基础测试 涉及知识点:数组,面向对象,重载,重写,继承,集合,排序,线程,文件流 一.多项选择题(可能是单选,也可能是多选) 1.下列标识符命名不合法的是(   D  ). A.$_Name ...

  2. 【JAVA核心知识】6.1: JAVA IO基础

    IO基础 1 输入与输出 2 流 2.1 字节流 2.2 字符流 3 阻塞IO与非阻塞IO 3.1 阻塞IO 3.2 非阻塞IO 3.3 生活举例 4 同步IO与异步IO 4.1 同步IO 4.2 异 ...

  3. java Vector 过时了_为什么Java Vector(和Stack)类被认为已过时或已弃用?

    为什么Java向量被认为是遗留的类,过时的或被弃用的? 使用并发时,它的使用是否有效? 如果我不想手动同步对象,只想使用线程安全集合而不需要对底层数组进行新的复制(如CopyOnWriteArrayL ...

  4. Qt使用JNI调用Java代码—————附带完整示例

    文章目录 0 背景 1 调用准备 2 调用 2.1 调用方法 2.2 方法签名 2.3 调用示例 2.4 注意事项 3 完整示例代码 3.1 部分代码 3.2 附赠示例 0 背景 在开发Android ...

  5. 从同步阻塞聊到Java三种IO方式

    对于刚刚成为程序猿不久的人,可能常常会被以下几个概念所混淆: 同步,异步,阻塞,非阻塞?以及从这几个概念中衍生出的几个概念,同步阻塞,同步非阻塞,异步阻塞,异步非阻塞? 小编从网上查了一些资料,发现对 ...

  6. java 编码 正弦计算器_计算器完整代码(java).doc

    计算器完整代码(java) 1. Calculator 类 import java.applet.*; import java.awt.*; import java.awt.event.*; impo ...

  7. java线程内存溢出_Java常见问题分析(内存溢出、内存泄露、线程阻塞等)

    Java垃圾回收机制(GC) 1.1 GC机制作用 1.2 堆内存3代分布(年轻代.老年代.持久代) 1.3 GC分类 1.4 GC过程 Java应用内存问题分析 2.1 Java内存划分 2.2 J ...

  8. 记录《疯狂Java讲义精粹》划线内容及理解(第十章-输入输出流(IO))

    第十章-输入输出流(IO) java的IO主要通过java.io包下的类和接口来实现,主要分为输入,输出两大类 每类都包含字节流(以字节为单位)和字符流(以字符为单位) java的IO流使用了装饰器设 ...

  9. Java基础知识第二讲:Java开发手册/JVM/集合框架/异常体系/Java反射/语法知识/Java IO

    Java基础知识第二讲(Java编程规范/JVM/集合框架/异常体系/Java反射/语法知识/Java IO/码出高效) 分享在java学习及工作中,常使用的一些基础知识,本文从JVM出发,讲解了JV ...

  10. 架构设计:系统间通信(4)——IO通信模型和JAVA实践 中篇

    接上篇<架构设计:系统间通信(3)--IO通信模型和JAVA实践 上篇> 4.多路复用IO模型 在"上篇"文章中,我们已经提到了使用多线程解决高并发场景的问题所在,这篇 ...

最新文章

  1. linux kernel内存映射实例分析
  2. 前端资源构建-Grunt环境搭建
  3. JavaSE(十三)——Swing
  4. 腾讯Groupon合资团购网站 高朋网悄然上线
  5. Redis进阶之内存模型
  6. 研究死锁–第5部分:使用显式锁定
  7. Gerrit的用法及与gitlab的区别
  8. python slice函数怎么取列表的最后一个数_python slice函数_python中slice函数如何实现?...
  9. ASP.NET程序中常用的三十三种代码一
  10. @entity 不限字节长度的类型_面试常考,项目易错,长文详解C/C++中的字节对齐...
  11. linux内核编程4部曲之二:增加linux内核系统调用
  12. 【千本樱】mmd镜头+动作打包下载
  13. 向工程腐化开炮|资源治理
  14. jsp简介lamitry_[提拉米苏] 找人一起玩,今晚刚开的号
  15. html直线动画,HTML5 Canvas流动线条动画特效
  16. html用ul li制作导航条
  17. 2019年1-4月份雅思口语题库素材(原创)describe an interesting persion you would like to meet
  18. 微信小程序商品详情页底部弹出框(点击加入购物车或立即购买弹出)
  19. 分布式事务 spring 两阶段提交 tcc
  20. B+树算法在mysql中能存多少行数据?

热门文章

  1. 点击按钮弹出Outlook(新建邮件状态)
  2. Ubuntu打包发布qt程序
  3. c语言利用索引数组排序,根据C中的索引数组对数组排序C
  4. mac推箱子c语言,c语言写的推箱子源码,非常适合新手学习
  5. 18awg线材最大电流_USB4来了,最大的变化是这个
  6. dbeaver 修改数据_GitHub 上 5 款超好用的数据库 GUI 带你玩转 MongoDB、Redis、SQL 数据库...
  7. LintCode—合并两个排序链表(165)
  8. 克隆虚拟主机后的主机如何联网!!!!
  9. ElasticSearch开发问题汇总(不断更新中)
  10. ASP.NET MVC 3和Razor中的@helper