当前位置:网站首页>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){
}
}
}
}
边栏推荐
- 数睿数据受邀参与南通企业数字化转型研讨会
- [Shanda conference] application setting module
- Program substitution function
- 3.抽象類(shape)
- 数睿数据荣获第二届ISIG中国产业智能大会两项年度大奖
- Reddit对LaMDA模型的探讨:并非无状态,采用双重过程,相比它编辑维基百科的方式,有没有感情并不重要
- Consumption monitoring of Prometheus monitoring [consult exporter]
- 期货怎么开户?网上期货开户安全吗?
- SAP ABAP table control and example-07
- Solve the problem of MySQL remote login permission
猜你喜欢

SAP ABAP 数据字典教程 SE11:表、锁定对象、视图和结构 -03

Ironsource Luna offers a limited time discount for Apple search ads and enjoys 3 months of free service upon registration
![Prometheus监控之Consul监控 [consul-exporter]](/img/9e/8547b2c38143ab0e051c1cf0b04986.png)
Prometheus监控之Consul监控 [consul-exporter]

SAP script tutorial: se71, se78, SCC1, vf03, so10-013

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

jmeter关联登录302类型的接口

Pod type

什么是RESTful,REST api设计时应该遵守什么样的规则?

数睿数据深度 | 关于软件自主可控,源代码向左,无代码向右
![Consumption monitoring of Prometheus monitoring [consult exporter]](/img/9e/8547b2c38143ab0e051c1cf0b04986.png)
Consumption monitoring of Prometheus monitoring [consult exporter]
随机推荐
SAP ABAP table control and example-07
NiO programming service
SAP ABAP data dictionary tutorial se11: tables, locked objects, views, and structures-03
Lecture 6 of slam Lecture 14 -- nonlinear optimization
SAP ABAP BAPI-016
Conversion between numeric types and strings
3.抽象類(shape)
音视频基础知识|ANS 噪声抑制原理解析
Pod type
谷歌浏览器的小细节
6.gui (graphics, filling)
nio服务多线程版本
Summary of Changan chain usage skills
SAP ABAP dialog programming tutorial: module pool in -09
What is SAP ABAP? Type, ABAP full form and meaning
Google Chrome small details
wallys/WiFi6 MiniPCIe Module 2T2R 2×2.4GHz 2x5GHz
【C语言】库函数qsort的使用
Deploy odoo to the server and configure it as a service
过气剧本杀,被露营“复活”