Java BIO NIO AIO,java 常用技术框架
private volatile boolean start;
public NIOServerHandle(int port) {
this.port = port;
try {
selector = Selector.open();
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().bind(new InetSocketAddress(port), 1024);
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT)
《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
;
start = true;
System.out.println("NIOServer 启动服务,端口:" + port);
}catch (Exception e) {
System.out.println("封装 NIO 服务失败");
System.out.println(e);
}
}
@Override
public void run(){
while(start) {
try{
//没 1 秒唤醒一次,轮询一遍事件
selector.select(1000);
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> it = selectionKeys.iterator();
SelectionKey selectionKey = null;
while(it.hasNext()) {
selectionKey = it.next();
it.remove();
try{
handleInput(selectionKey);
}catch(Exception e){
System.out.println("服务获取信息失败");
System.out.println(e);
}
}
}
catch (Exception e) {
System.out.println(e);
}
}
if(selector != null)
try{
selector.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public void stop() { start = false;}
private void handleInput(SelectionKey key) throws Exception{
if(key.isValid()) {
if(key.isAcceptable()) {
ServerSocketChannel channel = (ServerSocketChannel) key.channel();
SocketChannel socketChannel = channel.accept();
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
}
if(key.isReadable()) {
SocketChannel socketChannel = (SocketChannel)key.channel();
//分配 1M 的 buffer
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
int size = socketChannel.read(byteBuffer);
if(size > 0) {
//将缓冲区当前的 limit 设置为 position=0,用于后续对缓冲区的读取操作
byteBuffer.flip();
byte[] bytes = new byte[byteBuffer.remaining()];
//复制到 bytes 中
byteBuffer.get(bytes);
String contents = new String(bytes, "UTF-8");
System.out.println("NIO 服务收到消息:"+contents);
String expression = new String(bytes,"UTF-8");
System.out.println("服务器收到消息:" + expression);
doWrite(socketChannel, "收到信息,你好,我是 NIO");
}
else if(size < 0) {
key.cancel();
socketChannel.close();
}
}
}
}
private void doWrite(SocketChannel channel, String msg) {
try {
byte[] bytes = msg.getBytes();
ByteBuffer writebuffer = ByteBuffer.allocate(bytes.length);
writebuffer.put(bytes);
//postion,limit 值的交换,用于输出 buffer
writebuffer.flip();
channel.write(writebuffer);
}catch (Exception e) {
System.out.print("[error]: NIOserver 发送信息失败");
e.printStackTrace();
}
}
}
NIO 客户端代码
提示
类 Client 用于启动 NIO 客户端,传输参数只有 IP、PORT
类 ClientHandle 用于 NIO 客户端请求服务端以及处理服务端的数返回数据
NIO 客户端启动类
package com.ruider.customerNIO;
/**
Created by mahede on 2018/11/21.
*/
public class Client {
private static final String IP_ADDRESS = "localhost";
private static final int PORT = 1234;
private static ClientHandle clientHandle;
public static void start(){ start(IP_ADDRESS, PORT);}
private synchronized static void start(String host, int port) {
if(clientHandle != null) {
clientHandle.stop();
}
clientHandle = new ClientHandle(IP_ADDRESS, PORT);
new Thread(clientHandle, "Client").start();
}
//向服务器发送消息
public static boolean sendMsg(String msg) throws Exception{
if(msg.equals("q")) return false;
clientHandle.sendMsg(msg);
return true;
}
}
客户端发送请求和处理返回数据类
package com.ruider.customerNIO;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
/**
Created by mahede on 2018/11/21.
*/
public class ClientHandle implements Runnable {
private String ipAddress;
private int port;
private SocketChannel socketChannel;
private Selector selector;
private static boolean start;
public ClientHandle(String host, int port) {
try{
this.ipAddress = host;
this.port = port;
selector = Selector.open();
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
//socketChannel.connect(new InetSocketAddress(ipAddress, port));
//socketChannel.register(selector, SelectionKey.OP_CONNECT);
start = true;
System.out.println("客户端启动");
}catch (Exception e) {
System.out.println("客户端启动失败");
e.printStackTrace();
}
}
public static void stop() { start = false;}
public void sendMsg(String msg) throws Exception{
socketChannel.register(selector, SelectionKey.OP_READ);
doWrite(socketChannel, msg);
}
@Override
public void run() {
try{
doConnect();
}catch (Exception e){
e.printStackTrace();
System.exit(1);
}
while (start) {
try {
selector.select(1000);
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> it = selectionKeys.iterator();
SelectionKey key = null;
while (it.hasNext()) {
key = it.next();
it.remove();
try {
handleInput(key);
}
catch (Exception e) {
if(key != null){
key.cancel();
if(key.channel() != null){
key.channel().close();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
try{
if(selector != null) {
selector.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
private void handleInput(SelectionKey key) {
if(key.isValid()) {
try{
SocketChannel socketChannel = (SocketChannel)key.channel();
if(key.isConnectable()){
if(socketChannel.finishConnect());
else System.exit(1);
}
if(key.isReadable()){
//创建 ByteBuffer,并开辟一个 1M 的缓冲区
ByteBuffer buffer = ByteBuffer.allocate(1024);
//读取请求码流,返回读取到的字节数
int readBytes = socketChannel.read(buffer);
//读取到字节,对字节进行编解码
if(readBytes>0){
//将缓冲区当前的 limit 设置为 position=0,用于后续对缓冲区的读取操作
buffer.flip();
//根据缓冲区可读字节数创建字节数组
byte[] bytes = new byte[buffer.remaining()];
//将缓冲区可读字节数组复制到新建的数组中
buffer.get(bytes);
String result = new String(bytes,"UTF-8");
总结
我个人认为,如果你想靠着背面试题来获得心仪的 offer,用癞蛤蟆想吃天鹅肉形容完全不过分。想必大家能感受到面试越来越难,想找到心仪的工作也是越来越难,高薪工作羡慕不来,却又对自己目前的薪资不太满意,工作几年甚至连一个应届生的薪资都比不上,终究是错付了,错付了自己没有去提升技术。
这些面试题分享给大家的目的,其实是希望大家通过大厂面试题分析自己的技术栈,给自己梳理一个更加明确的学习方向,当你准备好去面试大厂,你心里有底,大概知道面试官会问多广,多深,避免面试的时候一问三不知。
大家可以把 Java 基础,JVM,并发编程,MySQL,Redis,Spring,Spring cloud 等等做一个知识总结以及延伸,再去进行操作,不然光记是学不会的,这里我也提供一些脑图分享给大家:



希望你看完这篇文章后,不要犹豫,抓紧学习,复习知识,准备在明年的金三银四拿到心仪的 offer,加油,打工人!
评论