Ending
Tip:由于文章篇幅有限制,下面还有20个关于MySQL的问题,我都复盘整理成一份pdf文档了,后面的内容我就把剩下的问题的目录展示给大家看一下
如果觉得有帮助不妨【转发+点赞+关注】支持我,后续会为大家带来更多的技术类文章以及学习类文章!(阿里对MySQL底层实现以及索引实现问的很多)
吃透后这份pdf,你同样可以跟面试官侃侃而谈MySQL。其实像阿里p7岗位的需求也没那么难(但也不简单),扎实的Java基础+无短板知识面+对某几个开源技术有深度学习+阅读过源码+算法刷题,这一套下来p7岗差不多没什么问题,还是希望大家都能拿到高薪offer吧。
NIO图解
AIO图解
NIO 服务端代码
提示
-
类NIOServer用于启动NIO服务器,传输参数只有PORT
-
类NIOServerHandle用于NIO服务器处理客户端的请求
NIOServer启动类
package com.ruider.customerNIO;
/**
- Created by mahede on 2018/11/20.
*/
public class NIOServer {
private static final int NIO_PORT = 1234;
private static NIOServerHandle nioServerHandle;
public static void start() { start(NIO_PORT); }
private synchronized static void start(int port) {
if(nioServerHandle != null) { nioServerHandle.stop(); }
nioServerHandle = new NIOServerHandle(NIO_PORT);
new Thread(nioServerHandle, “Server”).start();
}
}
NIO服务器处理请求类
package com.ruider.customerNIO;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
/**
- Created by mahede on 2018/11/20.
*/
public class NIOServerHandle implements Runnable{
private int port;
private ServerSocketChannel serverSocketChannel;
private Selector selector;
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);
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 selectionKeys = selector.selectedKeys();
Iterator 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;
最后
即使是面试跳槽,那也是一个学习的过程。只有全面的复习,才能让我们更好的充实自己,武装自己,为自己的面试之路不再坎坷!今天就给大家分享一个Github上全面的Java面试题大全,就是这份面试大全助我拿下大厂Offer,月薪提至30K!
我也是第一时间分享出来给大家,希望可以帮助大家都能去往自己心仪的大厂!为金三银四做准备!
一共有20个知识点专题,分别是:
Dubbo面试专题
JVM面试专题
Java并发面试专题
Kafka面试专题
MongDB面试专题
MyBatis面试专题
MySQL面试专题
Netty面试专题
RabbitMQ面试专题
Redis面试专题
Spring Cloud面试专题
SpringBoot面试专题
zookeeper面试专题
常见面试算法题汇总专题
计算机网络基础专题
设计模式专题
74)]
SpringBoot面试专题
[外链图片转存中…(img-PXaf9tRD-1715556452174)]
zookeeper面试专题
[外链图片转存中…(img-wmRaRJYu-1715556452174)]
常见面试算法题汇总专题
[外链图片转存中…(img-CRkdHEix-1715556452175)]
计算机网络基础专题
[外链图片转存中…(img-j3XWD59q-1715556452175)]
设计模式专题
[外链图片转存中…(img-78H88kuM-1715556452175)]