更新 招聘信息
This commit is contained in:
@ -4,7 +4,7 @@ import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* .
|
||||
* 题目描述: .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-05-20:03
|
||||
|
11
code/src/main/java/com/raorao/java/io/Main.java
Normal file
11
code/src/main/java/com/raorao/java/io/Main.java
Normal file
@ -0,0 +1,11 @@
|
||||
package com.raorao.java.io;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-07-9:58
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
}
|
45
code/src/main/java/com/raorao/java/io/aio/SimpleClient.java
Normal file
45
code/src/main/java/com/raorao/java/io/aio/SimpleClient.java
Normal file
@ -0,0 +1,45 @@
|
||||
package com.raorao.java.io.aio;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.AsynchronousSocketChannel;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* 异步非阻塞IO (AIO) 的客户端.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-07-14:38
|
||||
*/
|
||||
public class SimpleClient {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 用于读取数据的ByteBuffer。
|
||||
ByteBuffer buff = ByteBuffer.allocate(1024);
|
||||
Charset utf = Charset.forName("utf-8");
|
||||
try (
|
||||
// ①创建AsynchronousSocketChannel对象
|
||||
AsynchronousSocketChannel clientChannel
|
||||
= AsynchronousSocketChannel.open()) {
|
||||
// ②连接远程服务器
|
||||
clientChannel.connect(new InetSocketAddress("127.0.0.1"
|
||||
, 8888)).get(); //④
|
||||
buff.clear();
|
||||
// ③从clientChannel中读取数据
|
||||
clientChannel.read(buff).get(); //⑤
|
||||
buff.flip();
|
||||
// 将buff中内容转换为字符串
|
||||
String content = utf.decode(buff).toString();
|
||||
System.out.println("服务器信息:" + content);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
43
code/src/main/java/com/raorao/java/io/aio/SimpleServer.java
Normal file
43
code/src/main/java/com/raorao/java/io/aio/SimpleServer.java
Normal file
@ -0,0 +1,43 @@
|
||||
package com.raorao.java.io.aio;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.AsynchronousServerSocketChannel;
|
||||
import java.nio.channels.AsynchronousSocketChannel;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
* 使用AIO 实现异步非阻塞通信.
|
||||
*
|
||||
* 步骤:AsynchronousServerSocketChannel用于服务器端,只要三步
|
||||
*
|
||||
* 1.调用open()静态方法创建AsynchronousServerSocketChannel。
|
||||
*
|
||||
* 2.调用AsynchronousServerSocketChannel的bind()方法让它在指定的IP地址,指定端口监听。
|
||||
*
|
||||
* 3.调用AsynchronousServerSocketChannel的accept()方法接受请求。
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-07-14:30
|
||||
*/
|
||||
public class SimpleServer {
|
||||
|
||||
public static void main(String[] args)
|
||||
throws IOException, ExecutionException, InterruptedException {
|
||||
AsynchronousServerSocketChannel serverChannel = AsynchronousServerSocketChannel.open();
|
||||
serverChannel.bind(new InetSocketAddress("127.0.0.1", 8888));
|
||||
while (true) {
|
||||
// 采用循环接受来自客户端的连接
|
||||
Future<AsynchronousSocketChannel> future
|
||||
= serverChannel.accept();
|
||||
// 获取连接完成后返回的AsynchronousSocketChannel
|
||||
AsynchronousSocketChannel socketChannel = future.get();
|
||||
// 执行输出。
|
||||
socketChannel.write(ByteBuffer.wrap("欢迎你来自AIO的世界!"
|
||||
.getBytes("UTF-8"))).get();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
53
code/src/main/java/com/raorao/java/io/nio/NIOClient.java
Normal file
53
code/src/main/java/com/raorao/java/io/nio/NIOClient.java
Normal file
@ -0,0 +1,53 @@
|
||||
package com.raorao.java.io.nio;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-07-10:41
|
||||
*/
|
||||
public class NIOClient {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
// Socket socket = new Socket("127.0.0.1", 8888);
|
||||
// OutputStream out = socket.getOutputStream();
|
||||
// String s = "hello world";
|
||||
// out.write(s.getBytes());
|
||||
// out.close();
|
||||
|
||||
Thread t1 = new Thread(() -> {
|
||||
try {
|
||||
Socket socket = new Socket("127.0.0.1", 8888);
|
||||
OutputStream out = socket.getOutputStream();
|
||||
Thread.sleep(2000);
|
||||
String s = "hello world, this is thread-1, I have slept 2s";
|
||||
out.write(s.getBytes());
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
Thread t2 = new Thread(() -> {
|
||||
try {
|
||||
Socket socket = new Socket("127.0.0.1", 8888);
|
||||
OutputStream out = socket.getOutputStream();
|
||||
Thread.sleep(3000);
|
||||
String s = "hello world, this is thread-2, I have slept 3s";
|
||||
out.write(s.getBytes());
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
t2.start();
|
||||
t1.start();
|
||||
}
|
||||
}
|
103
code/src/main/java/com/raorao/java/io/nio/NIOMain.java
Normal file
103
code/src/main/java/com/raorao/java/io/nio/NIOMain.java
Normal file
@ -0,0 +1,103 @@
|
||||
package com.raorao.java.io.nio;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
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;
|
||||
|
||||
/**
|
||||
* NIO 应用类.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-07-10:02
|
||||
*/
|
||||
public class NIOMain {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
//fastCopy("E:\\test.mkv", "D:\\test.mkv");
|
||||
selector();
|
||||
}
|
||||
|
||||
public static void fastCopy(String src, String dst) throws IOException {
|
||||
FileInputStream fin = new FileInputStream(src); /* 获取源文件的输入字节流 */
|
||||
FileChannel fcin = fin.getChannel(); /* 获取输入字节流的文件通道 */
|
||||
FileOutputStream fout = new FileOutputStream(dst); /* 获取目标文件的输出字节流 */
|
||||
FileChannel fcout = fout.getChannel(); /* 获取输出字节流的通道 */
|
||||
ByteBuffer buffer = ByteBuffer.allocateDirect(1024); /* 为缓冲区分配 1024 个字节 */
|
||||
while (true) {
|
||||
int r = fcin.read(buffer); /* 从输入通道中读取数据到缓冲区中 */
|
||||
if (r == -1) { /* read() 返回 -1 表示 EOF */
|
||||
break;
|
||||
}
|
||||
buffer.flip(); /* 切换读写 */
|
||||
fcout.write(buffer); /* 把缓冲区的内容写入输出文件中 */
|
||||
buffer.clear(); /* 清空缓冲区 */
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* nio 选择器的使用方法。只有套接字的Channel 才能被配置为非阻塞,FileChannel 不行,这里采用SocketChannel 来测试。
|
||||
*/
|
||||
public static void selector() throws IOException {
|
||||
Selector selector = Selector.open();
|
||||
ServerSocketChannel ssChannel = ServerSocketChannel.open();
|
||||
ssChannel.configureBlocking(false); // 配置非阻塞
|
||||
ssChannel.register(selector, SelectionKey.OP_ACCEPT); // 注册选择器
|
||||
|
||||
ServerSocket serverSocket = ssChannel.socket();
|
||||
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 8888);
|
||||
serverSocket.bind(address);
|
||||
|
||||
while (true) {
|
||||
selector.select();
|
||||
Set<SelectionKey> keys = selector.selectedKeys();
|
||||
Iterator<SelectionKey> keyIterator = keys.iterator();
|
||||
while (keyIterator.hasNext()) {
|
||||
SelectionKey key = keyIterator.next();
|
||||
if (key.isAcceptable()) {
|
||||
ServerSocketChannel ssChannel1 = (ServerSocketChannel) key.channel();
|
||||
// 服务器会为每个新连接创建一个 SocketChannel
|
||||
SocketChannel sChannel = ssChannel1.accept();
|
||||
sChannel.configureBlocking(false);
|
||||
// 这个新连接主要用于从客户端读取数据
|
||||
sChannel.register(selector, SelectionKey.OP_READ);
|
||||
} else if (key.isReadable()) {
|
||||
SocketChannel sChannel = (SocketChannel) key.channel();
|
||||
System.out.println(readDataFromSocketChannel(sChannel));
|
||||
sChannel.close();
|
||||
}
|
||||
keyIterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String readDataFromSocketChannel(SocketChannel sChannel) throws IOException {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
StringBuilder data = new StringBuilder();
|
||||
while (true) {
|
||||
buffer.clear();
|
||||
int n = sChannel.read(buffer);
|
||||
if (n == -1) {
|
||||
break;
|
||||
}
|
||||
buffer.flip();
|
||||
int limit = buffer.limit();// 这个地方limit表示的是缓冲区有数据部分实际的末尾部分,小于等于缓冲区容量1024.
|
||||
char[] dst = new char[limit];
|
||||
for (int i = 0; i < limit; i++) {
|
||||
dst[i] = (char) buffer.get(i);
|
||||
}
|
||||
data.append(dst);
|
||||
buffer.clear();
|
||||
}
|
||||
return data.toString();
|
||||
}
|
||||
}
|
52
code/src/main/java/com/raorao/leetcode/Q1.java
Normal file
52
code/src/main/java/com/raorao/leetcode/Q1.java
Normal file
@ -0,0 +1,52 @@
|
||||
package com.raorao.leetcode;
|
||||
|
||||
/**
|
||||
* 二叉树最短路径.
|
||||
*
|
||||
* 题目描述:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along
|
||||
* the shortest path from the root node down to the nearest leaf node.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-07-17:01
|
||||
*/
|
||||
public class Q1 {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
思路:
|
||||
递归,若为空树返回0;
|
||||
若左子树为空,则返回右子树的最小深度+1;(加1是因为要加上根这一层,下同)
|
||||
若右子树为空,则返回左子树的最小深度+1;
|
||||
若左右子树均不为空,则取左、右子树最小深度的较小值,+1;
|
||||
* @param root
|
||||
* @return
|
||||
*/
|
||||
public int run(TreeNode root) {
|
||||
if (root == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (root.left == null) {
|
||||
return run(root.right) + 1;
|
||||
}
|
||||
if (root.right == null) {
|
||||
return run(root.left) + 1;
|
||||
}
|
||||
return run(root.left) < run(root.right) ? run(root.left) + 1 : run(root.right) + 1;
|
||||
}
|
||||
|
||||
static class TreeNode {
|
||||
|
||||
int val;
|
||||
TreeNode left;
|
||||
TreeNode right;
|
||||
|
||||
TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user