当前位置:网站首页>nio服务多线程版本
nio服务多线程版本
2022-06-22 15:10:00 【原力与你同在】
public static void main(String[] args) throws IOException {
Thread.currentThread().setName("boss");
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
Selector boss = Selector.open();
SelectionKey bossKey = ssc.register(boss, 0, null);
bossKey.interestOps(SelectionKey.OP_ACCEPT);
ssc.bind(new InetSocketAddress(8888));
// worker 创建固定worker
Worker worker = new Worker("worker-0");
while (true){
boss.select();
Iterator<SelectionKey> iterator = boss.selectedKeys().iterator();
while (iterator.hasNext()){
SelectionKey key = iterator.next();
iterator.remove();
if(key.isAcceptable()){
SocketChannel sc = ssc.accept();
sc.configureBlocking(false);
log.info("connected...{}",sc.getRemoteAddress());
// 关联
log.info("before register...{}",sc.getRemoteAddress());
// 阻塞
worker.register(sc);
// 注册要在前,select方法后执行
log.info("after register...{}",sc.getRemoteAddress());
}
}
}
}
static class Worker implements Runnable{
private Thread thread;
private Selector worker;
private String name;
private volatile boolean start = false;
public Worker(String name){
this.name = name;}
public void register(SocketChannel sc) throws IOException {
if(!start){
thread = new Thread(this,name);
worker = Selector.open();
thread.start();
start = true;
}
worker.wakeup(); // 唤醒
sc.register(worker, SelectionKey.OP_READ,null);
}
@Override
public void run() {
while (true){
try {
// 阻塞
worker.select();
Iterator<SelectionKey> iterator = worker.selectedKeys().iterator();
while (iterator.hasNext()){
SelectionKey key = iterator.next();
iterator.remove();
if(key.isReadable()){
ByteBuffer buffer = ByteBuffer.allocate(16);
SocketChannel channel = (SocketChannel) key.channel();
log.info("read:{}",channel.getRemoteAddress());
channel.read(buffer);
buffer.flip();
log.info("buffer is:{}",Charset.defaultCharset().decode(buffer).toString());
}
}
}catch (IOException e){
}
}
}
}
边栏推荐
- LeetCode_回溯_动态规划_中等_131.分割回文串
- B树和B+树
- Luogu p2466 [sdoi2008] Sue's small ball solution
- [Shanda conference] acquisition of user media based on webrtc
- CMake教程系列-00-简介
- Uni develops wechat applet to customize automatic camera detection (portrait + ID card)
- Rosbag use command
- Rosbag use command
- SAP ABAP 数据字典教程 SE11:表、锁定对象、视图和结构 -03
- Cmake tutorial series-00-introduction
猜你喜欢
随机推荐
Huawei cloud hcdez special session and Distributed Technology Summit: Huawei cloud distributed cloud native technology and Practice
【山大会议】WebRTC基础之用户媒体的获取
Deploy odoo to the server and configure it as a service
[Shanda conference] project initialization
【山大会议】一些基本工具类定义
C语言贪吃蛇
十九、Xv6上下文切换(上下文切换的实现;状态机的封装与恢复)
Navicat premium connecting to Oracle database (Graphic tutorial)
SAP 中的 ABAP 查询教程:SQ01、SQ02、SQ03-017
1.类的继承(point)
使用 zipfile、openpyxl、flask 批量导出excel zip
默认函数控制 =default 与 =delete
二叉树练习第二弹
[译文] 弥合开源数据库和数据库业务之间的鸿沟
【LeetCode】9、回文数
SLAM十四讲之第6讲--非线性优化
Applet development - Custom expiration cache
Uni develops wechat applet to customize automatic camera detection (portrait + ID card)
Focus on creating a net red product. The xinjietu x70s is newly launched, starting from 87900
19、 Xv6 context switching (implementation of context switching; encapsulation and recovery of state machine)


![[Shanda conference] application setting module](/img/1e/1665234715b365614a753355274ced.png)





