diff --git a/code/src/main/java/com/raorao/interview/pdd/Friends.java b/code/src/main/java/com/raorao/interview/pdd/Friends.java index ee8caf06..475166e4 100644 --- a/code/src/main/java/com/raorao/interview/pdd/Friends.java +++ b/code/src/main/java/com/raorao/interview/pdd/Friends.java @@ -4,7 +4,7 @@ import java.util.ArrayList; import java.util.Scanner; /** - * . + * 题目描述: . * * @author Xiong Raorao * @since 2018-08-05-20:03 diff --git a/code/src/main/java/com/raorao/java/io/Main.java b/code/src/main/java/com/raorao/java/io/Main.java new file mode 100644 index 00000000..f055dec2 --- /dev/null +++ b/code/src/main/java/com/raorao/java/io/Main.java @@ -0,0 +1,11 @@ +package com.raorao.java.io; + +/** + * . + * + * @author Xiong Raorao + * @since 2018-08-07-9:58 + */ +public class Main { + +} diff --git a/code/src/main/java/com/raorao/java/io/aio/SimpleClient.java b/code/src/main/java/com/raorao/java/io/aio/SimpleClient.java new file mode 100644 index 00000000..f9b9aa1c --- /dev/null +++ b/code/src/main/java/com/raorao/java/io/aio/SimpleClient.java @@ -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(); + } + } +} diff --git a/code/src/main/java/com/raorao/java/io/aio/SimpleServer.java b/code/src/main/java/com/raorao/java/io/aio/SimpleServer.java new file mode 100644 index 00000000..4578b55d --- /dev/null +++ b/code/src/main/java/com/raorao/java/io/aio/SimpleServer.java @@ -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 future + = serverChannel.accept(); + // 获取连接完成后返回的AsynchronousSocketChannel + AsynchronousSocketChannel socketChannel = future.get(); + // 执行输出。 + socketChannel.write(ByteBuffer.wrap("欢迎你来自AIO的世界!" + .getBytes("UTF-8"))).get(); + } + + } +} diff --git a/code/src/main/java/com/raorao/java/io/nio/NIOClient.java b/code/src/main/java/com/raorao/java/io/nio/NIOClient.java new file mode 100644 index 00000000..4b246bc4 --- /dev/null +++ b/code/src/main/java/com/raorao/java/io/nio/NIOClient.java @@ -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(); + } +} diff --git a/code/src/main/java/com/raorao/java/io/nio/NIOMain.java b/code/src/main/java/com/raorao/java/io/nio/NIOMain.java new file mode 100644 index 00000000..093c053b --- /dev/null +++ b/code/src/main/java/com/raorao/java/io/nio/NIOMain.java @@ -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 keys = selector.selectedKeys(); + Iterator 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(); + } +} diff --git a/code/src/main/java/com/raorao/leetcode/Q1.java b/code/src/main/java/com/raorao/leetcode/Q1.java new file mode 100644 index 00000000..5318937b --- /dev/null +++ b/code/src/main/java/com/raorao/leetcode/Q1.java @@ -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; + } + } +} diff --git a/interview/README.md b/interview/README.md index b9bec425..8cd85aaa 100644 --- a/interview/README.md +++ b/interview/README.md @@ -33,8 +33,9 @@ Thoutworks | 内推 | | [内推链接](https://jinshuju.net/f/CcO2JA) 抖音、头条 | 内推 | 8.1 - 12.31| [招聘官网](https://job.bytedance.com/campus/)
  • 8.2 投简历 携程 | 内推 |
  • 内推:8.2 - 8.12
  • 网申 8.2 - 9.4 | [招聘官网](http://campus.ctrip.com)
  • 8.2 已投简历 老虎证券 | 内推 |
  • 内推: 8.4 - 8.10 |
  • 8.4 日已经提交内推 -贝壳网 | 内推 | | [招聘官网](http://campus.ke.com/)
  • 8.4已投简历 +贝壳网 | 内推 | | [招聘官网](http://campus.ke.com/)
  • 8.4 已投简历 美团 | 内推/网申 | 面试时间:9.6-9.14 |
  • 8.6 已投简历 +招银网络科技 | 内推/网申 | 面试时间:9.8 和 9.25 |
  • 8.7 已投简历 ## 2. 面试记录 diff --git a/interview/experience/ant.md b/interview/experience/ant.md new file mode 100644 index 00000000..685d1fac --- /dev/null +++ b/interview/experience/ant.md @@ -0,0 +1,101 @@ +# 蚂蚁金服面经 + +## 蚂蚁金服二面 + +原文:http://www.54tianzhisheng.cn/2018/07/31/alipay02/ + +1、自我介绍、工作经历、技术栈 + +2、项目中你学到了什么技术?(把三项目具体描述了很久) + +3、微服务划分的粒度 + +4、微服务的高可用怎么保证的? + +5、常用的负载均衡,该怎么用,你能说下吗? + +6、网关能够为后端服务带来哪些好处? + +7、Spring Bean 的生命周期 + +8、xml 中配置的 init、destroy 方法怎么可以做到调用具体的方法? + +9、反射的机制 + +10、Object 类中的方法 + +11、hashcode 和 equals 方法常用地方 + +12、对象比较是否相同 + +13、hashmap put 方法存放的时候怎么判断是否是重复的 + +14、Object toString 方法常用的地方,为什么要重写该方法 + +15、Set 和 List 区别? + +16、ArrayList 和 LinkedList 区别 + +17、如果存取相同的数据,ArrayList 和 LinkedList 谁占用空间更大? + +18、Set 存的顺序是有序的吗? + +19、常见 Set 的实现有哪些? + +20、TreeSet 对存入对数据有什么要求呢? + +21、HashSet 的底层实现呢 + +22、TreeSet 底层源码有看过吗? + +23、HashSet 是不是线程安全的?为什么不是线程安全的? + +24、Java 中有哪些线程安全的 Map? + +25、Concurrenthashmap 是怎么做到线程安全的? + +26、HashTable 你了解过吗? + +27、如何保证线程安全问题? + +28、synchronized、lock + +29、volatile 的原子性问题?为什么 i++ 这种不支持原子性?从计算机原理的设计来讲下不能保证原子性的原因 + +30、happens before 原理 + +31、cas 操作 + +32、lock 和 synchronized 的区别? + +33、公平锁和非公平锁 + +34、Java 读写锁 + +35、读写锁设计主要解决什么问题? + +36、你项目除了写 Java 代码,还有前端代码,那你知道前端有哪些框架吗? + +37、MySQL 分页查询语句 + +38、MySQL 事务特性和隔离级别 + +39、不可重复读会出现在什么场景? + +40、sql having 的使用场景 + +41、前端浏览器地址的一个 http 请求到后端整个流程是怎么样?能够说下吗? + +42、http 默认端口,https 默认端口 + +43、DNS 你知道是干嘛的吗? + +44、你们开发用的 ide 是啥?你能说下 idea 的常用几个快捷键吧? + +45、代码版本管理你们用的是啥? + +46、git rebase 和 merge 有什么区别? + +47、你们公司加班多吗? + +48、后面一起聊 high 了,之间扯了些蛋,哈哈哈 diff --git a/interview/experience/java.md b/interview/experience/java.md new file mode 100644 index 00000000..aa5e475f --- /dev/null +++ b/interview/experience/java.md @@ -0,0 +1,3 @@ +# Java 面经 + +- [Java面试通关要点汇总集【终极版】](http://blog.720ui.com/2018/java_interview_final/?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io) \ No newline at end of file diff --git a/interview/java/base.md b/interview/java/base.md index 24ff9043..a3e97696 100644 --- a/interview/java/base.md +++ b/interview/java/base.md @@ -494,6 +494,10 @@ private interface DefaulableFactory { **默认和静态方法 主要是为了扩充接口的方法,否则,所有实现了该接口的类都需要重新实现新方法** +# socket 编程 + +[socket 编程](https://www.cnblogs.com/rocomp/p/4790340.html) + # 参考文档 - [HashMap? ConcurrentHashMap? 相信看完这篇没人能难住你!](https://crossoverjie.top/2018/07/23/java-senior/ConcurrentHashMap/) diff --git a/interview/note.md b/interview/note.md index c86b4148..a4a94e14 100644 --- a/interview/note.md +++ b/interview/note.md @@ -1,4 +1,18 @@ -# 1 求职目标 + + +- [2 复习内容](#2-复习内容) + - [Java](#java) + - [数据结构和算法](#数据结构和算法) + - [操作系统](#操作系统) + - [数据库](#数据库) + - [计算机网络](#计算机网络) + - [项目技能](#项目技能) + - [经典面经](#经典面经) + - [牛客网题目](#牛客网题目) +- [3 完成进度表](#3-完成进度表) +- [4. 时间轴](#4-时间轴) + + Java后端开发(大数据、分布式应用等) @@ -58,6 +72,8 @@ Java后端开发(大数据、分布式应用等) ## 经典面经 +- [蚂蚁金服](experience/ant.md) +- [java 面经](experience/java.md) ## 牛客网题目 @@ -98,4 +114,16 @@ Kafka | 7.31 | 7.26 | 完成理论复习,代码实践未完成 - 8.3 1. 复习HBase - 2. 复习Linux \ No newline at end of file + 2. 复习Linux + + - 8.6 + + 1. 复习java多线程 + 2. 复习java 8 新特性,lambda表达式,stream API,方法引用等内容 + + - 8.7 + + 1. 复习设计模式 + 2. 复习java IO + 3. 复习http + 4. 刷 Leetcode \ No newline at end of file