From 9e3ae3e8e6ee7b3e565dec49dc907f257991d38b Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Mon, 3 Sep 2018 10:36:55 +0800 Subject: [PATCH 01/23] auto commit --- notes/MySQL.md | 10 +++++----- notes/剑指 offer 题解.md | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/notes/MySQL.md b/notes/MySQL.md index f115ce92..3922b8a6 100644 --- a/notes/MySQL.md +++ b/notes/MySQL.md @@ -22,7 +22,7 @@ * [水平切分](#水平切分) * [垂直切分](#垂直切分) * [Sharding 策略](#sharding-策略) - * [Sharding 存在的问题及解决方案](#sharding-存在的问题及解决方案) + * [Sharding 存在的问题](#sharding-存在的问题) * [六、复制](#六复制) * [主从复制](#主从复制) * [读写分离](#读写分离) @@ -366,19 +366,19 @@ MySQL 提供了 FROM_UNIXTIME() 函数把 UNIX 时间戳转换为日期,并提 - 范围:可以是 ID 范围也可以是时间范围; - 映射表:使用单独的一个数据库来存储映射关系。 -## Sharding 存在的问题及解决方案 +## Sharding 存在的问题 ### 1. 事务问题 使用分布式事务来解决,比如 XA 接口。 -### 2. JOIN +### 2. 连接 -可以将原来的 JOIN 分解成多个单表 JOIN 查询,然后在用户程序中进行 JOIN。 +可以将原来的连接分解成多个单表连接查询,然后在用户程序中进行连接。 ### 3. ID 唯一性 -- 使用全局唯一 ID:GUID +- 使用全局唯一 ID(GUID) - 为每个分片指定一个 ID 范围 - 分布式 ID 生成器 (如 Twitter 的 Snowflake 算法) diff --git a/notes/剑指 offer 题解.md b/notes/剑指 offer 题解.md index af4c1842..9d3ada1a 100644 --- a/notes/剑指 offer 题解.md +++ b/notes/剑指 offer 题解.md @@ -1578,7 +1578,7 @@ private boolean verify(int[] sequence, int first, int last) { int cutIndex = first; while (cutIndex < last && sequence[cutIndex] <= rootVal) cutIndex++; - for (int i = cutIndex + 1; i < last; i++) + for (int i = cutIndex; i < last; i++) if (sequence[i] < rootVal) return false; return verify(sequence, first, cutIndex - 1) && verify(sequence, cutIndex, last - 1); From 137710c64535da5a13ed4f2aa6429a1133356d5a Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Mon, 3 Sep 2018 22:34:58 +0800 Subject: [PATCH 02/23] auto commit --- notes/CyC 学习交流群 问题汇总.md | 16 ---------------- notes/消息队列.md | 2 +- notes/缓存.md | 17 ++++++++++++----- 3 files changed, 13 insertions(+), 22 deletions(-) delete mode 100644 notes/CyC 学习交流群 问题汇总.md diff --git a/notes/CyC 学习交流群 问题汇总.md b/notes/CyC 学习交流群 问题汇总.md deleted file mode 100644 index 806d48d0..00000000 --- a/notes/CyC 学习交流群 问题汇总.md +++ /dev/null @@ -1,16 +0,0 @@ - -* [0. 进程内存空间中,堆和栈的区别](#0-进程内存空间中,堆和栈的区别) - - - -# 0. 进程内存空间中,堆和栈的区别 - -> C++ - -堆:动态、malloc()、new、链式分配、向上生长;栈:函数调用、编译器分配回收、向下生长。 - -https://www.cnblogs.com/sunziying/p/6510030.html - -By @CyC - ---- diff --git a/notes/消息队列.md b/notes/消息队列.md index bc80894d..2226de74 100644 --- a/notes/消息队列.md +++ b/notes/消息队列.md @@ -70,7 +70,7 @@ 接收端能够从消息队列成功消费一次消息。 -实现方法: +两种实现方法: - 保证接收端处理消息的业务逻辑具有幂等性:只要具有幂等性,那么消费多少次消息,最后处理的结果都是一样的。 - 保证消息具有唯一编号,并使用一张日志表来记录已经消费的消息编号。 diff --git a/notes/缓存.md b/notes/缓存.md index 26d96be1..771c5f6a 100644 --- a/notes/缓存.md +++ b/notes/缓存.md @@ -58,6 +58,7 @@ public class LRU implements Iterable { } } + public LRU(int maxSize) { this.maxSize = maxSize; @@ -70,6 +71,7 @@ public class LRU implements Iterable { tail.pre = head; } + public V get(K key) { if (!map.containsKey(key)) { @@ -83,6 +85,7 @@ public class LRU implements Iterable { return node.v; } + public void put(K key, V value) { if (map.containsKey(key)) { @@ -100,30 +103,34 @@ public class LRU implements Iterable { } } + private void unlink(Node node) { Node pre = node.pre; - node.pre = node.next; - node.next = pre; + Node next = node.next; + pre.next = next; + next.pre = pre; } + private void appendHead(Node node) { node.next = head.next; + node.pre = head; head.next = node; } + private Node removeTail() { Node node = tail.pre; - node.pre = tail; + tail.pre = node.pre; return node; } + @Override public Iterator iterator() { return new Iterator() { - private Node cur = head.next; - @Override public boolean hasNext() { return cur != tail; From d44274c7cc898762bc4b6e57650cefd62658ccf5 Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Mon, 3 Sep 2018 22:37:06 +0800 Subject: [PATCH 03/23] auto commit --- notes/CyC 学习交流群 问题汇总.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 notes/CyC 学习交流群 问题汇总.md diff --git a/notes/CyC 学习交流群 问题汇总.md b/notes/CyC 学习交流群 问题汇总.md new file mode 100644 index 00000000..806d48d0 --- /dev/null +++ b/notes/CyC 学习交流群 问题汇总.md @@ -0,0 +1,16 @@ + +* [0. 进程内存空间中,堆和栈的区别](#0-进程内存空间中,堆和栈的区别) + + + +# 0. 进程内存空间中,堆和栈的区别 + +> C++ + +堆:动态、malloc()、new、链式分配、向上生长;栈:函数调用、编译器分配回收、向下生长。 + +https://www.cnblogs.com/sunziying/p/6510030.html + +By @CyC + +--- From 13f44993d874a5e205dae479c0fdf5073a81622b Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Mon, 3 Sep 2018 22:47:07 +0800 Subject: [PATCH 04/23] auto commit --- notes/缓存.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/notes/缓存.md b/notes/缓存.md index 771c5f6a..8f7449bf 100644 --- a/notes/缓存.md +++ b/notes/缓存.md @@ -169,7 +169,7 @@ public class LRU implements Iterable { 使用 Redis、Memcache 等分布式缓存将数据缓存在分布式缓存系统中。 -相对于本地缓存来说,分布式缓存单独部署,可以根据需求分配硬件资源。不仅如此,服务器集群都可以访问分布式缓存,而本地缓存需要在服务器集群之间进行同步,实现和性能开销上都非常大。 +相对于本地缓存来说,分布式缓存单独部署,可以根据需求分配硬件资源。不仅如此,服务器集群都可以访问分布式缓存,而本地缓存需要在服务器集群之间进行同步,实现难度和性能开销上都非常大。 ## 数据库缓存 @@ -248,7 +248,7 @@ Distributed Hash Table(DHT) 是一种哈希分布方式,其目的是为了

-一致性哈希在增加或者删除节点时只会影响到哈希环中相邻的节点,例如下图中新增节点 X,只需要将它后一个节点 C 上的数据重新进行分布即可,对于节点 A、B、D 都没有影响。 +一致性哈希在增加或者删除节点时只会影响到哈希环中相邻的节点,例如下图中新增节点 X,只需要将它前一个节点 C 上的数据重新进行分布即可,对于节点 A、B、D 都没有影响。

From 93090ab6a7668642100ca4a2ac60a6919fda6ff4 Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Tue, 4 Sep 2018 00:04:01 +0800 Subject: [PATCH 05/23] auto commit --- notes/分布式.md | 2 +- notes/攻击技术.md | 40 ++++++++-------------------------------- notes/集群.md | 7 +++---- 3 files changed, 12 insertions(+), 37 deletions(-) diff --git a/notes/分布式.md b/notes/分布式.md index 113837ce..a39ae35e 100644 --- a/notes/分布式.md +++ b/notes/分布式.md @@ -167,7 +167,7 @@ Zookeeper 提供了一种树形结构级的命名空间,/app1/p_1 节点的父 可用性指分布式系统在面对各种异常时可以提供正常服务的能力,可以用系统可用时间占总时间的比值来衡量,4 个 9 的可用性表示系统 99.99% 的时间是可用的。 -在可用性条件下,要求系统提供的服务一直处于可用的状态,对于用户的每一个操,请求总是能够在有限的时间内返回结果。 +在可用性条件下,要求系统提供的服务一直处于可用的状态,对于用户的每一个操作请求总是能够在有限的时间内返回结果。 ## 分区容忍性 diff --git a/notes/攻击技术.md b/notes/攻击技术.md index 46dd4616..31eeaaa4 100644 --- a/notes/攻击技术.md +++ b/notes/攻击技术.md @@ -13,6 +13,8 @@ 跨站脚本攻击(Cross-Site Scripting, XSS),可以将代码注入到用户浏览的网页上,这种代码包括 HTML 和 JavaScript。 +## 攻击原理 + 例如有一个论坛网站,攻击者可以在上面发布以下内容: ```html @@ -43,40 +45,23 @@ 例如将 `<` 转义为 `<`,将 `>` 转义为 `>`,从而避免 HTML 和 Jascript 代码的运行。 -## 富文本编辑器 - 富文本编辑器允许用户输入 HTML 代码,就不能简单地将 `<` 等字符进行过滤了,极大地提高了 XSS 攻击的可能性。 富文本编辑器通常采用 XSS filter 来防范 XSS 攻击,通过定义一些标签白名单或者黑名单,从而不允许有攻击性的 HTML 代码的输入。 以下例子中,form 和 script 等标签都被转义,而 h 和 p 等标签将会保留。 -> [XSS 过滤在线测试](http://jsxss.com/zh/try.html) - ```html

XSS Demo

-

-Sanitize untrusted HTML (to prevent XSS) with a configuration specified by a Whitelist. -

+

123

-
hello
-

- http -

- -

Features:

-
    -
  • Specifies HTML tags and their attributes allowed with whitelist
  • -
  • Handle any tags or attributes using custom function
  • -
- @@ -85,32 +70,21 @@ alert(/xss/); ```html

XSS Demo

-

-Sanitize untrusted HTML (to prevent XSS) with a configuration specified by a Whitelist. -

+

123

<form> <input type="text" name="q" value="test"> - <button id="submit">Submit</button> </form>
hello
-

- http -

- -

Features:

-
    -
  • Specifies HTML tags and their attributes allowed with whitelist
  • -
  • Handle any tags or attributes using custom function
  • -
- <script type="text/javascript"> alert(/xss/); </script> ``` +> [XSS 过滤在线测试](http://jsxss.com/zh/try.html) + # 二、跨站请求伪造 ## 概念 @@ -119,6 +93,8 @@ alert(/xss/); XSS 利用的是用户对指定网站的信任,CSRF 利用的是网站对用户浏览器的信任。 +## 攻击原理 + 假如一家银行用以执行转账操作的 URL 地址如下: ``` diff --git a/notes/集群.md b/notes/集群.md index 338ddcb4..111495c3 100644 --- a/notes/集群.md +++ b/notes/集群.md @@ -18,12 +18,12 @@ 负载均衡器可以用来实现高可用以及伸缩性: - 高可用:当某个节点故障时,负载均衡器会将用户请求转发到另外的节点上,从而保证所有服务持续可用; -- 伸缩性:可以很容易地添加移除节点。 +- 伸缩性:根据系统整体负载情况,可以很容易地添加移除节点。 负载均衡运行过程包含两个部分: -1. 根据负载均衡算法得到请求转发的节点; -2. 将请求进行转发。 +1. 根据负载均衡算法得到转发的节点; +2. 进行转发。 ## 负载均衡算法 @@ -205,4 +205,3 @@ HTTP 重定向负载均衡服务器使用某种负载均衡算法计算得到服 - [Session Management using Spring Session with JDBC DataStore](https://sivalabs.in/2018/02/session-management-using-spring-session-jdbc-datastore/) - From 1cdc78e44ac498bbe952a15a2cb5ea1b040c3a5f Mon Sep 17 00:00:00 2001 From: harleyzhao Date: Wed, 5 Sep 2018 11:31:40 +0800 Subject: [PATCH 06/23] =?UTF-8?q?add=20=E7=AE=97=E6=B3=95=E5=92=8C?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Interview-Notebook | 1 + other/算法与数据结构.md | 158 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 160000 Interview-Notebook create mode 100644 other/算法与数据结构.md diff --git a/Interview-Notebook b/Interview-Notebook new file mode 160000 index 00000000..3e35079c --- /dev/null +++ b/Interview-Notebook @@ -0,0 +1 @@ +Subproject commit 3e35079cf9b79b8cc1d577d886aa6abc58793600 diff --git a/other/算法与数据结构.md b/other/算法与数据结构.md new file mode 100644 index 00000000..3f0362f6 --- /dev/null +++ b/other/算法与数据结构.md @@ -0,0 +1,158 @@ +# Algorithm +leetcode/lintcode上的算法题 + +**关于问题的答案和解体的思路,可以移步我的github: https://github.com/zhaozhengcoder/Algorithm** + +### About + + 这个仓库最初的想法是把lintcode/lintocde上面的算法题目整理一下,因为很多题目太多了显得太乱了,就不继续在GitHub上面写了,以前写的一部分移到我的博客上面了。 + GitHub上面打算整理一些比较典型 或者是 自己思考过的觉得很好的问题。 + + + 在博客上面开了两个专栏 + + 1. 数据结构/算法导论 : + https://www.jianshu.com/nb/12397278 + + 2. OJ练习题 : + https://www.jianshu.com/nb/9973135 + + 推荐两篇自己对 递归搜索和动态规划 的理解的blog : + + 1. https://www.jianshu.com/p/5eb4da919efe + + 2. https://www.jianshu.com/p/6b3a2304f63f + + + +### 题目的索引 + GITHUB上面打算整理一些比较典型 或者是 自己思考过的觉得很好的问题。 + + 1.从数据结构的角度索引 : + + a. 数组 + + 两数之和 + + 连续最大子数组 + + 乘积最大子数组 + + 买卖股票的最佳时机1,2,3 + + 买卖股票的最佳时机1:寻找数组里面的最大上升子序列 + 买卖股票的最佳时机2:寻找数组里面所有的上升子序列 + 买卖股票的最佳时机3:寻找数组里面两个不重合的上升子序列,并且使他们的和最大 to-do + + 区间合并(将有交集的区间合并) + + 寻找缺失的数 + + 1. 一个顺序的数组[1,2,3,5,6],缺少了一个数字,如何找到它? + + 2. 一个arr的数组,只有一个数字出现了一次,其他都出现了两次,如何找到它? + + 数组的近似划分(将一个数组分成两个,但是差最小) + + 数组里面第k大的数 + + 跳跃游戏1,2 + + 跳跃游戏1: + 给出一个非负整数数组,你最初定位在数组的第一个位置, + 数组中的每个元素代表你在那个位置可以跳跃的最大长度, + 返回 是否能到达数组的最后一个位置 + + 跳跃游戏2: + 给出一个非负整数数组,你最初定位在数组的第一个位置, + 数组中的每个元素代表你在那个位置可以跳跃的最大长度,    + 返回 使用最少的跳跃次数到达数组的最后一个位置 + + a+. 二维矩阵 + + 顺时针打印二维矩阵 + + 给出一个二维矩阵,找到一个路径(从某个左上角到某个角右下)使这条路径的值最大 + + b. 链表 + + c. 字符串 + + 最长公共子序列(并不是连续的) + + 最长回文子串 + + d. 二叉树 + + 返回一个平衡二叉树的第k大的节点 + + 二叉树的最低公共祖先 + + 非递归遍历二叉树 + + e. 图 + + 最短路径 + + 深度/广度优先遍历 + + 2. 从算法的角度建立索引 : + + a. 递归搜索问题 + + N后问题 + + 全排列 + + 组合问题1,2 + + b. 动态规划 + + 背包问题1,2 + + 数组的近似划分(将一个数组分成两个,但是差最小) + + 跳跃游戏1,2 + + 给出一个二维矩阵,找到一个路径(从某个左上角到某个角右下)使这条路径的值最大 + + + 3. 常用 + + a. 排列/组合 + + b. 深度优先遍历 + + c. 最短路径 + + 4. 智力题(算法本身很简单,就是想不到的那种) + + 最多有多少个点在同一条直线上 + + +### Others + + 1. 类似于系统设计的题目 + + 带最小值的栈/队列 + + url长链接转短链接 + + 2. 解决特定问题 + + 并查集 + + 布隆过滤器 + + + +如果你对机器学习的算法感兴趣,欢迎共同讨论: + +https://github.com/zhaozhengcoder/Machine-Learning + + +### Flag + +刷到200题吧~ + +![](1.PNG) \ No newline at end of file From 8156c42eec21885ed4db5a19b4d93ef373a7b441 Mon Sep 17 00:00:00 2001 From: harleyzhao Date: Wed, 5 Sep 2018 11:34:33 +0800 Subject: [PATCH 07/23] add --- other/算法与数据结构.md | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/other/算法与数据结构.md b/other/算法与数据结构.md index 3f0362f6..d1e4ddf3 100644 --- a/other/算法与数据结构.md +++ b/other/算法与数据结构.md @@ -1,7 +1,7 @@ # Algorithm leetcode/lintcode上的算法题 -**关于问题的答案和解体的思路,可以移步我的github: https://github.com/zhaozhengcoder/Algorithm** +**关于问题的答案和解体的思路,可以移步 : https://github.com/zhaozhengcoder/Algorithm** ### About @@ -149,10 +149,3 @@ leetcode/lintcode上的算法题 如果你对机器学习的算法感兴趣,欢迎共同讨论: https://github.com/zhaozhengcoder/Machine-Learning - - -### Flag - -刷到200题吧~ - -![](1.PNG) \ No newline at end of file From 5fe72c31d8bf627ebb4776d7ec72af4ce832d2bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=B0=B8=E5=B7=9D?= Date: Wed, 5 Sep 2018 16:04:01 +0800 Subject: [PATCH 08/23] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 7ee0bf57..ded19a67 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,6 @@ - - ### :pencil2: 算法 - [剑指 Offer 题解](https://github.com/CyC2018/InnterviewNotes/blob/master/notes/剑指%20offer%20题解.md) From 9aebc4660fd0cd02d43f03e81d16178fbd00447b Mon Sep 17 00:00:00 2001 From: peierlong Date: Wed, 5 Sep 2018 16:59:23 +0800 Subject: [PATCH 09/23] =?UTF-8?q?=E5=8D=95=E4=BE=8B=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9E=9A=E4=B8=BE=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- notes/设计模式.md | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/notes/设计模式.md b/notes/设计模式.md index 9d9ab38f..b893599c 100644 --- a/notes/设计模式.md +++ b/notes/设计模式.md @@ -179,6 +179,52 @@ public class Singleton { 该实现可以防止反射攻击。在其它实现中,通过 setAccessible() 方法可以将私有构造函数的访问级别设置为 public,然后调用构造函数从而实例化对象,如果要防止这种攻击,需要在构造函数中添加防止实例化第二个对象的代码。但是该实现是由 JVM 保证只会实例化一次,因此不会出现上述的反射攻击。 +#### Ⅵ 枚举实现 + +使用单元素的枚举类型来实现单例模式,相对于常规的单例模式,枚举实现的单例天生具有防止反射实例化对象和反序列化产生实例化对象,而且代码更加简洁,非常适合单例模式场景下使用。以下是枚举单例模式的实现。 + +```java + +public enum EnumSingleton { + INSTANCE; //单元素枚举实现单例 + + private String objName; + + public String getObjName() { + return objName; + } + + public void setObjName(String objName) { + this.objName = objName; + } + + public static void main(String[] args) { + // 单例测试 + EnumSingleton firstSingleton = EnumSingleton.INSTANCE; + firstSingleton.setObjName("firstName"); + System.out.println(firstSingleton.getObjName()); + + EnumSingleton secondSingleton = EnumSingleton.INSTANCE; + secondSingleton.setObjName("secondName"); + System.out.println(firstSingleton.getObjName()); + System.out.println(secondSingleton.getObjName()); + + // 反射获取实例测试 + try { + Constructor constructor = EnumSingleton.class.getDeclaredConstructor(); + constructor.setAccessible(true); + EnumSingleton enumSingleton = constructor.newInstance(); + System.out.println(enumSingleton.getObjName()); + } catch (Exception e) { + e.printStackTrace(); + } + + } +} + +``` + + ### Examples - Logger Classes @@ -2990,3 +3036,5 @@ public class ImageViewer { - [Design Patterns](http://www.oodesign.com/) - [Design patterns implemented in Java](http://java-design-patterns.com/) - [The breakdown of design patterns in JDK](http://www.programering.com/a/MTNxAzMwATY.html) + + From a015b110387eb4a183fac7dc9526de6cd9e316b3 Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Wed, 5 Sep 2018 17:22:45 +0800 Subject: [PATCH 10/23] auto commit --- notes/Java IO.md | 3 +++ notes/Java 基础.md | 11 ++++++----- notes/Java 虚拟机.md | 4 ++-- notes/剑指 offer 题解.md | 3 ++- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/notes/Java IO.md b/notes/Java IO.md index ee23a4bb..acb95b36 100644 --- a/notes/Java IO.md +++ b/notes/Java IO.md @@ -182,8 +182,10 @@ public static void readFileContent(String filePath) throws IOException { ```java public static void main(String[] args) throws IOException, ClassNotFoundException { + A a1 = new A(123, "abc"); String objectFile = "file/a1"; + ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(objectFile)); objectOutputStream.writeObject(a1); objectOutputStream.close(); @@ -195,6 +197,7 @@ public static void main(String[] args) throws IOException, ClassNotFoundExceptio } private static class A implements Serializable { + private int x; private String y; diff --git a/notes/Java 基础.md b/notes/Java 基础.md index 5b689cf8..d8f17cef 100644 --- a/notes/Java 基础.md +++ b/notes/Java 基础.md @@ -685,26 +685,26 @@ protected void finalize() throws Throwable {} **1. 等价关系** -(一)自反性 +Ⅰ 自反性 ```java x.equals(x); // true ``` -(二)对称性 +Ⅱ 对称性 ```java x.equals(y) == y.equals(x); // true ``` -(三)传递性 +Ⅲ 传递性 ```java if (x.equals(y) && y.equals(z)) x.equals(z); // true; ``` -(四)一致性 +Ⅳ 一致性 多次调用 equals() 方法结果不变 @@ -712,7 +712,7 @@ if (x.equals(y) && y.equals(z)) x.equals(y) == x.equals(y); // true ``` -(五)与 null 的比较 +Ⅴ 与 null 的比较 对任何不是 null 的对象 x 调用 x.equals(null) 结果都为 false @@ -741,6 +741,7 @@ System.out.println(x == y); // false ```java public class EqualExample { + private int x; private int y; private int z; diff --git a/notes/Java 虚拟机.md b/notes/Java 虚拟机.md index 333c1418..2ad1527c 100644 --- a/notes/Java 虚拟机.md +++ b/notes/Java 虚拟机.md @@ -30,7 +30,7 @@ # 一、运行时数据区域 -

+

## 程序计数器 @@ -40,7 +40,7 @@ 每个 Java 方法在执行的同时会创建一个栈帧用于存储局部变量表、操作数栈、常量池引用等信息。从方法调用直至执行完成的过程,就对应着一个栈帧在 Java 虚拟机栈中入栈和出栈的过程。 -

+

可以通过 -Xss 这个虚拟机参数来指定每个线程的 Java 虚拟机栈内存大小: diff --git a/notes/剑指 offer 题解.md b/notes/剑指 offer 题解.md index 9d3ada1a..2c8f2822 100644 --- a/notes/剑指 offer 题解.md +++ b/notes/剑指 offer 题解.md @@ -1433,7 +1433,8 @@ public boolean IsPopOrder(int[] pushSequence, int[] popSequence) { Stack stack = new Stack<>(); for (int pushIndex = 0, popIndex = 0; pushIndex < n; pushIndex++) { stack.push(pushSequence[pushIndex]); - while (popIndex < n && stack.peek() == popSequence[popIndex]) { + while (popIndex < n && !stack.isEmpty() + && stack.peek() == popSequence[popIndex]) { stack.pop(); popIndex++; } From 4fa8a6b8d63306c6435401fc40a30b466f8c1d04 Mon Sep 17 00:00:00 2001 From: Bandi Yugandhar Date: Wed, 5 Sep 2018 21:50:08 +0530 Subject: [PATCH 11/23] =?UTF-8?q?Update=20and=20rename=20notes/=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E6=9C=BA=E6=93=8D=E4=BD=9C=E7=B3=BB=E7=BB=9F.md=20to?= =?UTF-8?q?=20=20notes/=E8=AE=A1=E7=AE=97=E6=9C=BA=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- notes/计算机操作系统.md | 112 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/notes/计算机操作系统.md b/notes/计算机操作系统.md index 4125d86d..8751d4f8 100644 --- a/notes/计算机操作系统.md +++ b/notes/计算机操作系统.md @@ -456,6 +456,118 @@ void writer() { up(&data_mutex); } } +``` +The first case may result Writer to starve. This case favous Writers i.e no writer, once added to the queue, shall be kept waiting longer than absolutely necessary(only when there are readers that entered the queue before the writer). + + + + +```c +int readcount, writecount; //(initial value = 0) +semaphore rmutex, wmutex, readTry, resource; //(initial value = 1) + +//READER +reader() { + + down(&readLock); // reader is trying to enter + down(&rmutex); // lock to increase readcount + readcount++; + if (readcount == 1) + down(&resource); //if you are the first reader then lock the resource + up(&rmutex); //release for other readers + up(&readLock); //Done with trying to access the resource + + +//reading is performed + + + down(&rmutex); //reserve exit section - avoids race condition with readers + readcount--; //indicate you're leaving + if (readcount == 0) //checks if you are last reader leaving + up(&resource); //if last, you must release the locked resource + up(&rmutex); //release exit section for other readers +} + + +//WRITER +writer() { + + down(&wmutex); //reserve entry section for writers - avoids race conditions + writecount++; //report yourself as a writer entering + if (writecount == 1) //checks if you're first writer + down(&readLock); //if you're first, then you must lock the readers out. Prevent them from trying to enter CS + up(&wmutex); //release entry section + + + down(&resource); //reserve the resource for yourself - prevents other writers from simultaneously editing the shared resource + //writing is performed + up(&resource); //release file + + + down(&wmutex); //reserve exit section + writecount--; //indicate you're leaving + if (writecount == 0) //checks if you're the last writer + up(&readLock); //if you're last writer, you must unlock the readers. Allows them to try enter CS for reading + up(&wmutex); //release exit section +} + +We can observe that every reader is forced to acquire ReadTry lock. On the otherhand, writers doesn’t need to lock individually. Once the first writer locks the ReadTry lock, it will be released only when there is writer left in the queue. + + +From the both cases we observed that either reader or writer has to starve. Below solutionadds the constraint that no thread shall be allowed to starve; that is, the operation of obtaining a lock on the shared data will always terminate in a bounded amount of time. + +int readCount; // init to 0; number of readers currently accessing resource + +// all semaphores initialised to 1 +Semaphore resourceAccess; // controls access (read/write) to the resource +Semaphore readCountAccess; // for syncing changes to shared variable readCount +Semaphore serviceQueue; // FAIRNESS: preserves ordering of requests (signaling must be FIFO) + +void writer() +{ + down(&serviceQueue); // wait in line to be servicexs + // + down(&resourceAccess); // request exclusive access to resource + // + up(&serviceQueue); // let next in line be serviced + + // + writeResource(); // writing is performed + // + + // + up(&resourceAccess); // release resource access for next reader/writer + // +} + + +void reader() +{ + down(&serviceQueue); // wait in line to be serviced + down(&readCountAccess); // request exclusive access to readCount + // + if (readCount == 0) // if there are no readers already reading: + down(&resourceAccess); // request resource access for readers (writers blocked) + readCount++; // update count of active readers + // + up(&serviceQueue); // let next in line be serviced + up(&readCountAccess); // release access to readCount + + // + readResource(); // reading is performed + // + + down(&readCountAccess); // request exclusive access to readCount + // + readCount--; // update count of active readers + if (readCount == 0) // if there are no readers left: + up(&resourceAccess); // release resource access for all + // + up(&readCountAccess); // release access to readCount +} + + + ``` ### 2. 哲学家进餐问题 From 4ee1497ade4f9d0fb58d2e5fed0417919a13c158 Mon Sep 17 00:00:00 2001 From: Bandi Yugandhar Date: Wed, 5 Sep 2018 22:22:22 +0530 Subject: [PATCH 12/23] =?UTF-8?q?Update=20=E8=AE=A1=E7=AE=97=E6=9C=BA?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E7=B3=BB=E7=BB=9F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- notes/计算机操作系统.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/notes/计算机操作系统.md b/notes/计算机操作系统.md index 8751d4f8..a25daa6f 100644 --- a/notes/计算机操作系统.md +++ b/notes/计算机操作系统.md @@ -464,10 +464,10 @@ The first case may result Writer to starve. This case favous Writers i.e no writ ```c int readcount, writecount; //(initial value = 0) -semaphore rmutex, wmutex, readTry, resource; //(initial value = 1) +semaphore rmutex, wmutex, readLock, resource; //(initial value = 1) //READER -reader() { +void reader() { down(&readLock); // reader is trying to enter down(&rmutex); // lock to increase readcount @@ -482,7 +482,7 @@ reader() { down(&rmutex); //reserve exit section - avoids race condition with readers - readcount--; //indicate you're leaving + readcount--; //indicate you're leaving if (readcount == 0) //checks if you are last reader leaving up(&resource); //if last, you must release the locked resource up(&rmutex); //release exit section for other readers @@ -490,8 +490,8 @@ reader() { //WRITER -writer() { - +void writer() { + down(&wmutex); //reserve entry section for writers - avoids race conditions writecount++; //report yourself as a writer entering if (writecount == 1) //checks if you're first writer @@ -510,12 +510,14 @@ writer() { up(&readLock); //if you're last writer, you must unlock the readers. Allows them to try enter CS for reading up(&wmutex); //release exit section } +``` -We can observe that every reader is forced to acquire ReadTry lock. On the otherhand, writers doesn’t need to lock individually. Once the first writer locks the ReadTry lock, it will be released only when there is writer left in the queue. +We can observe that every reader is forced to acquire ReadLock. On the otherhand, writers doesn’t need to lock individually. Once the first writer locks the ReadLock, it will be released only when there is no writer left in the queue. From the both cases we observed that either reader or writer has to starve. Below solutionadds the constraint that no thread shall be allowed to starve; that is, the operation of obtaining a lock on the shared data will always terminate in a bounded amount of time. +```c int readCount; // init to 0; number of readers currently accessing resource // all semaphores initialised to 1 @@ -524,7 +526,7 @@ Semaphore readCountAccess; // for syncing changes to shared variable readCo Semaphore serviceQueue; // FAIRNESS: preserves ordering of requests (signaling must be FIFO) void writer() -{ +{ down(&serviceQueue); // wait in line to be servicexs // down(&resourceAccess); // request exclusive access to resource @@ -542,7 +544,7 @@ void writer() void reader() -{ +{ down(&serviceQueue); // wait in line to be serviced down(&readCountAccess); // request exclusive access to readCount // From 59fda4cc63932fff2df51645a843cb2110631734 Mon Sep 17 00:00:00 2001 From: duang123 <643363707@qq.com> Date: Thu, 6 Sep 2018 09:21:14 +0800 Subject: [PATCH 13/23] =?UTF-8?q?Update=20=E8=AE=A1=E7=AE=97=E6=9C=BA?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E7=B3=BB=E7=BB=9F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 线程间通信也要做好同步,参考自UNP卷2 p4 --- notes/计算机操作系统.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notes/计算机操作系统.md b/notes/计算机操作系统.md index 4125d86d..60465ac5 100644 --- a/notes/计算机操作系统.md +++ b/notes/计算机操作系统.md @@ -181,7 +181,7 @@ QQ 和浏览器是两个进程,浏览器进程里面有很多线程,例如 H Ⅳ 通信方面 -进程间通信 (IPC) 需要进程同步和互斥手段的辅助,以保证数据的一致性。而线程间可以通过直接读/写同一进程中的数据段(如全局变量)来进行通信。 +进程间通信 (IPC) 需要进程同步和互斥手段的辅助,以保证数据的一致性。而线程间可以通过直接读/写同一进程中的数据段(如全局变量)来进行通信(需要做好同步)。 ## 进程状态的切换 From 6e61d32e20aff1ad8fc477f5e5f0d4a6b06dfaf1 Mon Sep 17 00:00:00 2001 From: zixiao Date: Thu, 6 Sep 2018 11:27:22 +0800 Subject: [PATCH 14/23] the difference of new and malloc --- notes/CyC 学习交流群 问题汇总.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/notes/CyC 学习交流群 问题汇总.md b/notes/CyC 学习交流群 问题汇总.md index 806d48d0..521822b9 100644 --- a/notes/CyC 学习交流群 问题汇总.md +++ b/notes/CyC 学习交流群 问题汇总.md @@ -14,3 +14,21 @@ https://www.cnblogs.com/sunziying/p/6510030.html By @CyC --- +# 1. new 和 malloc 的区别 + +* 属性 +> new/delete是C++关键字,需要编译器支持。malloc/free是库函数,需要头文件支持 +* 参数 +> 使用new操作符申请内存分配时无需指定内存块的大小,编译器会根据类型信息自行计算。malloc则需要显式指出内存的尺寸。 +* 返回类型 +> new操作内存分配成功时,返回的是对象的类型指针,类型严格与对象匹配,无需类型转换,因此new是符合类型安全的操作符。而malloc内存分配成功则是返回void *,需要通过强制类型转换将void *指针转换成我们需要的类型。 +* 分配失败 +> new内存分配失败时,会抛出bac_alloc异常。malloc分配内存失败返回NULL。 +* 自定义类型 +> new会先调用operator new 函数,申请足够的内存(通常使用malloc实现)。然后调用类型的构造函数,初始化成员变量,最后返回自定义类型的指针。delete先调用析构函数,然后调用operator delete函函数释放内存(通常底层使用free实现) +* 重载 +> C++允许重载new/delete操作符,特别的,布局new的就不需要为对象分配内存,而是指定了一个地址作为内存的起始区域,new在这段内存上为对象调用构造函数完成初始化工作,并返回此地址。malloc不允许重载。 +* 内存区域 +> new 操作符从自由存储区(free store)上为对象动态分配内存空间,而malloc函数从堆上动态分配内存。自由存储区是c++基于new操作符的一个抽象概念,凡是通过new操作符进行内存申请,该内存即为自由存储区。而堆是操作系统中的术语,是操作系统中的术语,是操作系统所维护的一块特殊内存,用于程序内存的动态分配,C语言使用malloc从堆上分配内存,使用free释放已分配的对应内存。自由存储区不等于堆,如上所述,布局new就可以不位于堆中。 +* 能够直观地重新分配内存 +> malloc可以通过relloc进行内存重新分配实现内存扩充。new没有这样的直观配套设施来扩充内存。 From 1365bc4efe3ecb2e2774cbe5b71cba3ce5e810f5 Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Thu, 6 Sep 2018 18:13:57 +0800 Subject: [PATCH 15/23] auto commit --- notes/CyC 学习交流群 问题汇总.md | 18 ---------- notes/MySQL.md | 2 +- notes/计算机操作系统.md | 10 +++--- notes/设计模式.md | 56 +++++--------------------------- 4 files changed, 14 insertions(+), 72 deletions(-) diff --git a/notes/CyC 学习交流群 问题汇总.md b/notes/CyC 学习交流群 问题汇总.md index 521822b9..806d48d0 100644 --- a/notes/CyC 学习交流群 问题汇总.md +++ b/notes/CyC 学习交流群 问题汇总.md @@ -14,21 +14,3 @@ https://www.cnblogs.com/sunziying/p/6510030.html By @CyC --- -# 1. new 和 malloc 的区别 - -* 属性 -> new/delete是C++关键字,需要编译器支持。malloc/free是库函数,需要头文件支持 -* 参数 -> 使用new操作符申请内存分配时无需指定内存块的大小,编译器会根据类型信息自行计算。malloc则需要显式指出内存的尺寸。 -* 返回类型 -> new操作内存分配成功时,返回的是对象的类型指针,类型严格与对象匹配,无需类型转换,因此new是符合类型安全的操作符。而malloc内存分配成功则是返回void *,需要通过强制类型转换将void *指针转换成我们需要的类型。 -* 分配失败 -> new内存分配失败时,会抛出bac_alloc异常。malloc分配内存失败返回NULL。 -* 自定义类型 -> new会先调用operator new 函数,申请足够的内存(通常使用malloc实现)。然后调用类型的构造函数,初始化成员变量,最后返回自定义类型的指针。delete先调用析构函数,然后调用operator delete函函数释放内存(通常底层使用free实现) -* 重载 -> C++允许重载new/delete操作符,特别的,布局new的就不需要为对象分配内存,而是指定了一个地址作为内存的起始区域,new在这段内存上为对象调用构造函数完成初始化工作,并返回此地址。malloc不允许重载。 -* 内存区域 -> new 操作符从自由存储区(free store)上为对象动态分配内存空间,而malloc函数从堆上动态分配内存。自由存储区是c++基于new操作符的一个抽象概念,凡是通过new操作符进行内存申请,该内存即为自由存储区。而堆是操作系统中的术语,是操作系统中的术语,是操作系统所维护的一块特殊内存,用于程序内存的动态分配,C语言使用malloc从堆上分配内存,使用free释放已分配的对应内存。自由存储区不等于堆,如上所述,布局new就可以不位于堆中。 -* 能够直观地重新分配内存 -> malloc可以通过relloc进行内存重新分配实现内存扩充。new没有这样的直观配套设施来扩充内存。 diff --git a/notes/MySQL.md b/notes/MySQL.md index 3922b8a6..2c5d8935 100644 --- a/notes/MySQL.md +++ b/notes/MySQL.md @@ -316,7 +316,7 @@ FLOAT、DOUBLE 和 DECIMAL 都可以指定列宽,例如 DECIMAL(18, 9) 表示 VARCHAR 这种变长类型能够节省空间,因为只需要存储必要的内容。但是在执行 UPDATE 时可能会使行变得比原来长,当超出一个页所能容纳的大小时,就要执行额外的操作。MyISAM 会将行拆成不同的片段存储,而 InnoDB 则需要分裂页来使行放进页内。 -VARCHAR 会保留字符串末尾的空格,而 CHAR 会删除。 +在进行存储和检索时,会保留 VARCHAR 末尾的空格,而会删除 CHAR 末尾的空格。 ## 时间和日期 diff --git a/notes/计算机操作系统.md b/notes/计算机操作系统.md index 60465ac5..4e3d849f 100644 --- a/notes/计算机操作系统.md +++ b/notes/计算机操作系统.md @@ -13,8 +13,8 @@ * [经典同步问题](#经典同步问题) * [进程通信](#进程通信) * [三、死锁](#三死锁) - * [死锁的必要条件](#死锁的必要条件) - * [死锁的处理方法](#死锁的处理方法) + * [必要条件](#必要条件) + * [处理方法](#处理方法) * [鸵鸟策略](#鸵鸟策略) * [死锁检测与死锁恢复](#死锁检测与死锁恢复) * [死锁预防](#死锁预防) @@ -181,7 +181,7 @@ QQ 和浏览器是两个进程,浏览器进程里面有很多线程,例如 H Ⅳ 通信方面 -进程间通信 (IPC) 需要进程同步和互斥手段的辅助,以保证数据的一致性。而线程间可以通过直接读/写同一进程中的数据段(如全局变量)来进行通信(需要做好同步)。 +进程间通信需要进程同步和互斥手段的辅助,以保证数据的一致性。而线程间可以通过直接读/写同一进程中的数据段(如全局变量)来进行通信(需要做好同步)。 ## 进程状态的切换 @@ -596,7 +596,7 @@ FIFO 常用于客户-服务器应用程序中,FIFO 用作汇聚点,在客户 # 三、死锁 -## 死锁的必要条件 +## 必要条件

@@ -605,7 +605,7 @@ FIFO 常用于客户-服务器应用程序中,FIFO 用作汇聚点,在客户 - 不可抢占:已经分配给一个进程的资源不能强制性地被抢占,它只能被占有它的进程显式地释放。 - 环路等待:有两个或者两个以上的进程组成一条环路,该环路中的每个进程都在等待下一个进程所占有的资源。 -## 死锁的处理方法 +## 处理方法 主要有以下四种方法: diff --git a/notes/设计模式.md b/notes/设计模式.md index b893599c..262fb788 100644 --- a/notes/设计模式.md +++ b/notes/设计模式.md @@ -175,56 +175,18 @@ public class Singleton { } ``` +#### Ⅵ 枚举实现 + + ```java +public enum Singleton { + INSTANCE; +} + ``` + 该实现在多次序列化再进行反序列化之后,不会得到多个实例。而其它实现,为了保证不会出现反序列化之后出现多个实例,需要使用 transient 修饰所有字段,并且实现序列化和反序列化的方法。 该实现可以防止反射攻击。在其它实现中,通过 setAccessible() 方法可以将私有构造函数的访问级别设置为 public,然后调用构造函数从而实例化对象,如果要防止这种攻击,需要在构造函数中添加防止实例化第二个对象的代码。但是该实现是由 JVM 保证只会实例化一次,因此不会出现上述的反射攻击。 -#### Ⅵ 枚举实现 - -使用单元素的枚举类型来实现单例模式,相对于常规的单例模式,枚举实现的单例天生具有防止反射实例化对象和反序列化产生实例化对象,而且代码更加简洁,非常适合单例模式场景下使用。以下是枚举单例模式的实现。 - -```java - -public enum EnumSingleton { - INSTANCE; //单元素枚举实现单例 - - private String objName; - - public String getObjName() { - return objName; - } - - public void setObjName(String objName) { - this.objName = objName; - } - - public static void main(String[] args) { - // 单例测试 - EnumSingleton firstSingleton = EnumSingleton.INSTANCE; - firstSingleton.setObjName("firstName"); - System.out.println(firstSingleton.getObjName()); - - EnumSingleton secondSingleton = EnumSingleton.INSTANCE; - secondSingleton.setObjName("secondName"); - System.out.println(firstSingleton.getObjName()); - System.out.println(secondSingleton.getObjName()); - - // 反射获取实例测试 - try { - Constructor constructor = EnumSingleton.class.getDeclaredConstructor(); - constructor.setAccessible(true); - EnumSingleton enumSingleton = constructor.newInstance(); - System.out.println(enumSingleton.getObjName()); - } catch (Exception e) { - e.printStackTrace(); - } - - } -} - -``` - - ### Examples - Logger Classes @@ -3036,5 +2998,3 @@ public class ImageViewer { - [Design Patterns](http://www.oodesign.com/) - [Design patterns implemented in Java](http://java-design-patterns.com/) - [The breakdown of design patterns in JDK](http://www.programering.com/a/MTNxAzMwATY.html) - - From 3c08c153beb648116e72764bd60cd0c3d925a2ec Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Thu, 6 Sep 2018 18:24:01 +0800 Subject: [PATCH 16/23] auto commit --- notes/计算机操作系统.md | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/notes/计算机操作系统.md b/notes/计算机操作系统.md index 5eee94b4..e6c462a6 100644 --- a/notes/计算机操作系统.md +++ b/notes/计算机操作系统.md @@ -457,12 +457,12 @@ void writer() { } } ``` + +以下内容由 [@Bandi Yugandhar](https://github.com/yugandharbandi) 提供。 + The first case may result Writer to starve. This case favous Writers i.e no writer, once added to the queue, shall be kept waiting longer than absolutely necessary(only when there are readers that entered the queue before the writer). - - - -```c +```source-c int readcount, writecount; //(initial value = 0) semaphore rmutex, wmutex, readLock, resource; //(initial value = 1) @@ -482,13 +482,12 @@ void reader() { down(&rmutex); //reserve exit section - avoids race condition with readers - readcount--; //indicate you're leaving + readcount--; //indicate you're leaving if (readcount == 0) //checks if you are last reader leaving up(&resource); //if last, you must release the locked resource up(&rmutex); //release exit section for other readers } - //WRITER void writer() { @@ -514,10 +513,9 @@ void writer() { We can observe that every reader is forced to acquire ReadLock. On the otherhand, writers doesn’t need to lock individually. Once the first writer locks the ReadLock, it will be released only when there is no writer left in the queue. - From the both cases we observed that either reader or writer has to starve. Below solutionadds the constraint that no thread shall be allowed to starve; that is, the operation of obtaining a lock on the shared data will always terminate in a bounded amount of time. -```c +```source-c int readCount; // init to 0; number of readers currently accessing resource // all semaphores initialised to 1 @@ -532,17 +530,16 @@ void writer() down(&resourceAccess); // request exclusive access to resource //
up(&serviceQueue); // let next in line be serviced - + // writeResource(); // writing is performed // - + // up(&resourceAccess); // release resource access for next reader/writer // } - void reader() { down(&serviceQueue); // wait in line to be serviced @@ -554,11 +551,11 @@ void reader() //
up(&serviceQueue); // let next in line be serviced up(&readCountAccess); // release access to readCount - + // readResource(); // reading is performed // - + down(&readCountAccess); // request exclusive access to readCount // readCount--; // update count of active readers @@ -568,10 +565,9 @@ void reader() up(&readCountAccess); // release access to readCount } - - ``` + ### 2. 哲学家进餐问题

From bd7d42b5166ea8bfa01a77d43a75753f7e28fad3 Mon Sep 17 00:00:00 2001 From: peierlong Date: Thu, 6 Sep 2018 18:26:39 +0800 Subject: [PATCH 17/23] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- notes/设计模式.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/notes/设计模式.md b/notes/设计模式.md index b893599c..febb434a 100644 --- a/notes/设计模式.md +++ b/notes/设计模式.md @@ -211,14 +211,13 @@ public enum EnumSingleton { // 反射获取实例测试 try { - Constructor constructor = EnumSingleton.class.getDeclaredConstructor(); - constructor.setAccessible(true); - EnumSingleton enumSingleton = constructor.newInstance(); - System.out.println(enumSingleton.getObjName()); + EnumSingleton[] enumConstants = EnumSingleton.class.getEnumConstants(); + for (EnumSingleton enumConstant : enumConstants) { + System.out.println(enumConstant.getObjName()); + } } catch (Exception e) { e.printStackTrace(); } - } } From 941f8e352ccf7ab59d0079efeb3e0940be2a8446 Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Thu, 6 Sep 2018 18:38:45 +0800 Subject: [PATCH 18/23] auto commit --- notes/设计模式.md | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/notes/设计模式.md b/notes/设计模式.md index 1d80e151..45cc71f6 100644 --- a/notes/设计模式.md +++ b/notes/设计模式.md @@ -177,44 +177,46 @@ public class Singleton { #### Ⅵ 枚举实现 -使用单元素的枚举类型来实现单例模式,相对于常规的单例模式,枚举实现的单例天生具有防止反射实例化对象和反序列化产生实例化对象,而且代码更加简洁,非常适合单例模式场景下使用。以下是枚举单例模式的实现。 + ```java +public enum Singleton { -```java - -public enum EnumSingleton { - INSTANCE; //单元素枚举实现单例 + INSTANCE; private String objName; + public String getObjName() { return objName; } + public void setObjName(String objName) { this.objName = objName; } + public static void main(String[] args) { + // 单例测试 - EnumSingleton firstSingleton = EnumSingleton.INSTANCE; + Singleton firstSingleton = Singleton.INSTANCE; firstSingleton.setObjName("firstName"); System.out.println(firstSingleton.getObjName()); - - EnumSingleton secondSingleton = EnumSingleton.INSTANCE; + Singleton secondSingleton = Singleton.INSTANCE; secondSingleton.setObjName("secondName"); System.out.println(firstSingleton.getObjName()); System.out.println(secondSingleton.getObjName()); // 反射获取实例测试 try { - EnumSingleton[] enumConstants = EnumSingleton.class.getEnumConstants(); - for (EnumSingleton enumConstant : enumConstants) { + Singleton[] enumConstants = Singleton.class.getEnumConstants(); + for (Singleton enumConstant : enumConstants) { System.out.println(enumConstant.getObjName()); } } catch (Exception e) { e.printStackTrace(); } } +} ``` 该实现在多次序列化再进行反序列化之后,不会得到多个实例。而其它实现,为了保证不会出现反序列化之后出现多个实例,需要使用 transient 修饰所有字段,并且实现序列化和反序列化的方法。 From f82b21b49e1d769548684051456bf3c88a7a26a5 Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Thu, 6 Sep 2018 20:23:48 +0800 Subject: [PATCH 19/23] remove submodule --- Interview-Notebook | 1 - 1 file changed, 1 deletion(-) delete mode 160000 Interview-Notebook diff --git a/Interview-Notebook b/Interview-Notebook deleted file mode 160000 index 3e35079c..00000000 --- a/Interview-Notebook +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 3e35079cf9b79b8cc1d577d886aa6abc58793600 From 732420780c0d0044a9305224db9d2631126cb237 Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Fri, 7 Sep 2018 10:39:03 +0800 Subject: [PATCH 20/23] auto commit --- notes/HTTP.md | 11 +++++------ notes/计算机网络.md | 30 +++++++++++++++--------------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/notes/HTTP.md b/notes/HTTP.md index aae807e6..d03beaa5 100644 --- a/notes/HTTP.md +++ b/notes/HTTP.md @@ -25,7 +25,6 @@ * [实体首部字段](#实体首部字段) * [五、具体应用](#五具体应用) * [Cookie](#cookie) - * [6. Secure](#6-secure) * [缓存](#缓存) * [连接管理](#连接管理) * [内容协商](#内容协商) @@ -369,7 +368,7 @@ document.cookie = "tasty_cookie=strawberry"; console.log(document.cookie); ``` -### 6. HttpOnly +### 6. HttpOnly 标记为 HttpOnly 的 Cookie 不能被 JavaScript 脚本调用。跨站脚本攻击 (XSS) 常常使用 JavaScript 的 `Document.cookie` API 窃取用户的 Cookie 信息,因此使用 HttpOnly 标记可以在一定程度上避免 XSS 攻击。 @@ -377,11 +376,11 @@ console.log(document.cookie); Set-Cookie: id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Secure; HttpOnly ``` -## 6. Secure +### 7. Secure 标记为 Secure 的 Cookie 只能通过被 HTTPS 协议加密过的请求发送给服务端。但即便设置了 Secure 标记,敏感信息也不应该通过 Cookie 传输,因为 Cookie 有其固有的不安全性,Secure 标记也无法提供确实的安全保障。 -### 7. Session +### 8. Session 除了可以将用户信息通过 Cookie 存储在用户浏览器中,也可以利用 Session 存储在服务器端,存储在服务器端的信息更加安全。 @@ -396,11 +395,11 @@ Session 可以存储在服务器上的文件、数据库或者内存中。也可 应该注意 Session ID 的安全性问题,不能让它被恶意攻击者轻易获取,那么就不能产生一个容易被猜到的 Session ID 值。此外,还需要经常重新生成 Session ID。在对安全性要求极高的场景下,例如转账等操作,除了使用 Session 管理用户状态之外,还需要对用户进行重新验证,比如重新输入密码,或者使用短信验证码等方式。 -### 8. 浏览器禁用 Cookie +### 9. 浏览器禁用 Cookie 此时无法使用 Cookie 来保存用户信息,只能使用 Session。除此之外,不能再将 Session ID 存放到 Cookie 中,而是使用 URL 重写技术,将 Session ID 作为 URL 的参数进行传递。 -### 9. Cookie 与 Session 选择 +### 10. Cookie 与 Session 选择 - Cookie 只能存储 ASCII 码字符串,而 Session 则可以存取任何类型的数据,因此在考虑数据复杂性时首选 Session; - Cookie 存储在浏览器中,容易被恶意查看。如果非要将一些隐私数据存在 Cookie 中,可以将 Cookie 值进行加密,然后在服务器进行解密; diff --git a/notes/计算机网络.md b/notes/计算机网络.md index 3a08e2a0..fba8a6d0 100644 --- a/notes/计算机网络.md +++ b/notes/计算机网络.md @@ -5,7 +5,7 @@ * [主机之间的通信方式](#主机之间的通信方式) * [电路交换与分组交换](#电路交换与分组交换) * [时延](#时延) - * [计算机网络体系结构*](#计算机网络体系结构) + * [计算机网络体系结构](#计算机网络体系结构) * [二、物理层](#二物理层) * [通信方式](#通信方式) * [带通调制](#带通调制) @@ -13,14 +13,14 @@ * [基本问题](#基本问题) * [信道分类](#信道分类) * [信道复用技术](#信道复用技术) - * [CSMA/CD 协议*](#csmacd-协议) + * [CSMA/CD 协议](#csmacd-协议) * [PPP 协议](#ppp-协议) * [MAC 地址](#mac-地址) * [局域网](#局域网) - * [以太网*](#以太网) - * [交换机*](#交换机) + * [以太网](#以太网) + * [交换机](#交换机) * [虚拟局域网](#虚拟局域网) -* [四、网络层*](#四网络层) +* [四、网络层](#四网络层) * [概述](#概述) * [IP 数据报格式](#ip-数据报格式) * [IP 地址编址方式](#ip-地址编址方式) @@ -31,7 +31,7 @@ * [路由器的结构](#路由器的结构) * [路由器分组转发流程](#路由器分组转发流程) * [路由选择协议](#路由选择协议) -* [五、运输层*](#五运输层) +* [五、运输层](#五运输层) * [UDP 和 TCP 的特点](#udp-和-tcp-的特点) * [UDP 首部格式](#udp-首部格式) * [TCP 首部格式](#tcp-首部格式) @@ -121,7 +121,7 @@ 分组在路由器的输入队列和输出队列中排队等待的时间,取决于网络当前的通信量。 -## 计算机网络体系结构* +## 计算机网络体系结构

@@ -129,11 +129,11 @@ - **应用层** :为特定应用程序提供数据传输服务,例如 HTTP、DNS 等。数据单位为报文。 -- **运输层** :提供的是进程间的通用数据传输服务。由于应用层协议很多,定义通用的运输层协议就可以支持不断增多的应用层协议。运输层包括两种协议:传输控制协议 TCP,提供面向连接、可靠的数据传输服务,数据单位为报文段;用户数据报协议 UDP,提供无连接、尽最大努力的数据传输服务,数据单位为用户数据报。TCP 主要提供完整性服务,UDP 主要提供及时性服务。 +- **运输层** :为进程提供通用数据传输服务。由于应用层协议很多,定义通用的运输层协议就可以支持不断增多的应用层协议。运输层包括两种协议:传输控制协议 TCP,提供面向连接、可靠的数据传输服务,数据单位为报文段;用户数据报协议 UDP,提供无连接、尽最大努力的数据传输服务,数据单位为用户数据报。TCP 主要提供完整性服务,UDP 主要提供及时性服务。 -- **网络层** :为主机间提供数据传输服务,而运输层协议是为主机中的进程提供服务。网络层把运输层传递下来的报文段或者用户数据报封装成分组。 +- **网络层** :为主机提供数据传输服务。而运输层协议是为主机中的进程提供数据传输服务。网络层把运输层传递下来的报文段或者用户数据报封装成分组。 -- **数据链路层** :网络层针对的还是主机之间的数据传输服务,而主机之间可以有很多链路,链路层协议就是为同一链路的主机提供服务。数据链路层把网络层传下来的分组封装成帧。 +- **数据链路层** :网络层针对的还是主机之间的数据传输服务,而主机之间可以有很多链路,链路层协议就是为同一链路的主机提供数据传输服务。数据链路层把网络层传下来的分组封装成帧。 - **物理层** :考虑的是怎样在传输媒体上传输数据比特流,而不是指具体的传输媒体。物理层的作用是尽可能屏蔽传输媒体和通信手段的差异,使数据链路层感觉不到这些差异。 @@ -271,7 +271,7 @@ TCP/IP 协议族是一种沙漏形状,中间小两边大,IP 协议在其中

-## CSMA/CD 协议* +## CSMA/CD 协议 CSMA/CD 表示载波监听多点接入 / 碰撞检测。 @@ -316,7 +316,7 @@ MAC 地址是链路层地址,长度为 6 字节(48 位),用于唯一标

-## 以太网* +## 以太网 以太网是一种星型拓扑结构局域网。 @@ -333,7 +333,7 @@ MAC 地址是链路层地址,长度为 6 字节(48 位),用于唯一标

-## 交换机* +## 交换机 交换机具有自学习能力,学习的是交换表的内容,交换表中存储着 MAC 地址到接口的映射。 @@ -353,7 +353,7 @@ MAC 地址是链路层地址,长度为 6 字节(48 位),用于唯一标

-# 四、网络层* +# 四、网络层 ## 概述 @@ -578,7 +578,7 @@ BGP 只能寻找一条比较好的路由,而不是最佳路由。

-# 五、运输层* +# 五、运输层 网络层只把分组发送到目的主机,但是真正通信的并不是主机而是主机中的进程。运输层提供了进程间的逻辑通信,运输层向高层用户屏蔽了下面网络层的核心细节,使应用程序看起来像是在两个运输层实体之间有一条端到端的逻辑通信信道。 From 187f0bd02326d62cfc4486207acd6123d0566733 Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Fri, 7 Sep 2018 11:01:47 +0800 Subject: [PATCH 21/23] auto commit --- notes/HTTP.md | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/notes/HTTP.md b/notes/HTTP.md index d03beaa5..ef6faf87 100644 --- a/notes/HTTP.md +++ b/notes/HTTP.md @@ -39,7 +39,6 @@ * [认证](#认证) * [完整性保护](#完整性保护) * [HTTPs 的缺点](#https-的缺点) - * [配置 HTTPs](#配置-https) * [七、HTTP/2.0](#七http20) * [HTTP/1.x 缺陷](#http1x-缺陷) * [二进制分帧层](#二进制分帧层) @@ -719,11 +718,6 @@ HTTPs 的报文摘要功能之所以安全,是因为它结合了加密和认 - 因为需要进行加密解密等过程,因此速度会更慢; - 需要支付证书授权的高额费用。 - -## 配置 HTTPs - -[Nginx 配置 HTTPS 服务器](https://aotu.io/notes/2016/08/16/nginx-https/index.html) - # 七、HTTP/2.0 ## HTTP/1.x 缺陷 @@ -848,7 +842,7 @@ DELETE /idX/delete HTTP/1.1 -> Returns 404 # 九、HTTP/1.0 与 HTTP/1.1 的区别 -> 详细内容请见上文 +详细内容请见上文 - HTTP/1.1 默认是长连接 From 24c4864bfb0e55641c2613b07347c84cf41be77f Mon Sep 17 00:00:00 2001 From: Elong Date: Fri, 7 Sep 2018 11:07:21 +0800 Subject: [PATCH 22/23] =?UTF-8?q?Update=20=E8=AE=BE=E8=AE=A1=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 枚举代码风格修正 --- notes/设计模式.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/notes/设计模式.md b/notes/设计模式.md index 45cc71f6..4f561df4 100644 --- a/notes/设计模式.md +++ b/notes/设计模式.md @@ -657,7 +657,7 @@ public class ConcreteHandler1 extends Handler { @Override protected void handleRequest(Request request) { - if (request.getType() == RequestType.type1) { + if (request.getType() == RequestType.TYPE1) { System.out.println(request.getName() + " is handle by ConcreteHandler1"); return; } @@ -676,7 +676,7 @@ public class ConcreteHandler2 extends Handler{ @Override protected void handleRequest(Request request) { - if (request.getType() == RequestType.type2) { + if (request.getType() == RequestType.TYPE2) { System.out.println(request.getName() + " is handle by ConcreteHandler2"); return; } @@ -709,7 +709,7 @@ public class Request { ```java public enum RequestType { - type1, type2 + TYPE1, TYPE2 } ``` @@ -718,9 +718,9 @@ public class Client { public static void main(String[] args) { Handler handler1 = new ConcreteHandler1(null); Handler handler2 = new ConcreteHandler2(handler1); - Request request1 = new Request(RequestType.type1, "request1"); + Request request1 = new Request(RequestType.TYPE1, "request1"); handler2.handleRequest(request1); - Request request2 = new Request(RequestType.type2, "request2"); + Request request2 = new Request(RequestType.TYPE2, "request2"); handler2.handleRequest(request2); } } From 0fbafacb9bd94d88a1eaf834589b367316e7bc10 Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Fri, 7 Sep 2018 11:16:10 +0800 Subject: [PATCH 23/23] auto commit --- notes/设计模式.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/notes/设计模式.md b/notes/设计模式.md index 4f561df4..536072c2 100644 --- a/notes/设计模式.md +++ b/notes/设计模式.md @@ -639,22 +639,27 @@ abc ```java public abstract class Handler { + protected Handler successor; + public Handler(Handler successor) { this.successor = successor; } + protected abstract void handleRequest(Request request); } ``` ```java public class ConcreteHandler1 extends Handler { + public ConcreteHandler1(Handler successor) { super(successor); } + @Override protected void handleRequest(Request request) { if (request.getType() == RequestType.TYPE1) { @@ -669,11 +674,13 @@ public class ConcreteHandler1 extends Handler { ``` ```java -public class ConcreteHandler2 extends Handler{ +public class ConcreteHandler2 extends Handler { + public ConcreteHandler2(Handler successor) { super(successor); } + @Override protected void handleRequest(Request request) { if (request.getType() == RequestType.TYPE2) { @@ -689,22 +696,27 @@ public class ConcreteHandler2 extends Handler{ ```java public class Request { + private RequestType type; private String name; + public Request(RequestType type, String name) { this.type = type; this.name = name; } + public RequestType getType() { return type; } + public String getName() { return name; } } + ``` ```java @@ -715,11 +727,15 @@ public enum RequestType { ```java public class Client { + public static void main(String[] args) { + Handler handler1 = new ConcreteHandler1(null); Handler handler2 = new ConcreteHandler2(handler1); + Request request1 = new Request(RequestType.TYPE1, "request1"); handler2.handleRequest(request1); + Request request2 = new Request(RequestType.TYPE2, "request2"); handler2.handleRequest(request2); }