当前位置:网站首页>NiO service multithreaded version
NiO service multithreaded version
2022-06-22 16:28:00 【The force is with you】
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 Create fixed 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());
// relation
log.info("before register...{}",sc.getRemoteAddress());
// Blocking
worker.register(sc);
// Registration is required before ,select Method post execution
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(); // Wake up the
sc.register(worker, SelectionKey.OP_READ,null);
}
@Override
public void run() {
while (true){
try {
// Blocking
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){
}
}
}
}
边栏推荐
- NiO uses writable events to handle the situation of one-time write incompleteness
- sql语法检测
- 解决mysql远程登录报权限问题
- Runtime -- explore the nature of classes, objects, and classifications
- Static assertion static_ assert
- Odoo local document function development record
- 4.字符串(倒序且大小写转换)
- wallys/WiFi6 MiniPCIe Module 2T2R 2×2.4GHz 2x5GHz
- ABAP query tutorial in sap: sq01, sq02, sq03-017
- 毕业季·本科毕业感想——机械er的自救之路
猜你喜欢

ABAP query tutorial in sap: sq01, sq02, sq03-017

How to embody the value of knowledge management in business

odoo系统对原有模型单独开发的视图设置优先级

SAP abap 数据类型,操作符和编辑器-02

浙江创投圈的“半壁江山”,还得是国资

SAP ABAP dialog programming tutorial: module pool in -09

SAP 中的 ABAP 查询教程:SQ01、SQ02、SQ03-017

Unity game optimization (version 2) learning record 8

CUMT学习日记——数字图像处理考试速成笔记

Unity游戏优化(第2版)学习记录8
随机推荐
shell学习
5. reading and writing of documents (students)
webDriver以及Selenium使用总结
Machine learning notes - Hagrid - Introduction to gesture recognition image data set
GD32F4xx MCU 驱动mcp2515扩展CAN接口
十九、Xv6上下文切换(上下文切换的实现;状态机的封装与恢复)
SAP ABAP 中 OpenSQL和Native SQL-05 本教程的目的不是教您 SQL 或数据库概念
NiO file and folder operation examples
让pycharm项目里面的文本模板支持jinjia2语法
[Shanda conference] private chat channel webrtc tools
SAP ABAP report programming-08
SAP ABAP table control and example-07
SAP web service 无法使用 SOAMANAGER 登陆到SOA管理页面
买网红雪糕的我,成了大冤种
安全信得过!天翼云数据安全管理平台通过评测
Conversion between numeric types and strings
The odoo system sets priorities for the views independently developed by the original model
论催收系统的任务调度设计
SAP教程中的ALV报告 - ABAP列表查看器-012
SAP ABAP 内部表:创建、读取、填充、复制和删除-06