<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title></title>
    <description></description>
    <link>http://viaductooor.github.io/blog/</link>
    <atom:link href="http://viaductooor.github.io/blog/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 06 Oct 2019 03:27:30 +0000</pubDate>
    <lastBuildDate>Sun, 06 Oct 2019 03:27:30 +0000</lastBuildDate>
    <generator>Jekyll v3.8.5</generator>
    
      <item>
        <title>面试常见手撕代码题</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#常见排序算法的实现&quot; id=&quot;markdown-toc-常见排序算法的实现&quot;&gt;常见排序算法的实现&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#堆排序&quot; id=&quot;markdown-toc-堆排序&quot;&gt;堆排序&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#快速排序&quot; id=&quot;markdown-toc-快速排序&quot;&gt;快速排序&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#归并排序&quot; id=&quot;markdown-toc-归并排序&quot;&gt;归并排序&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#链表&quot; id=&quot;markdown-toc-链表&quot;&gt;链表&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#反转链表递归和非递归&quot; id=&quot;markdown-toc-反转链表递归和非递归&quot;&gt;反转链表（递归和非递归）&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#非递归头插法&quot; id=&quot;markdown-toc-非递归头插法&quot;&gt;非递归（头插法）&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#递归头插法&quot; id=&quot;markdown-toc-递归头插法&quot;&gt;递归（头插法）&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#倒数第k个节点&quot; id=&quot;markdown-toc-倒数第k个节点&quot;&gt;倒数第K个节点&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#检测环&quot; id=&quot;markdown-toc-检测环&quot;&gt;检测环&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#引申问题1为什么快指针的步长是2而不是345&quot; id=&quot;markdown-toc-引申问题1为什么快指针的步长是2而不是345&quot;&gt;引申问题1：为什么快指针的步长是2，而不是3、4、5…？&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#引申问题2如何确定环的入口&quot; id=&quot;markdown-toc-引申问题2如何确定环的入口&quot;&gt;引申问题2：如何确定环的入口？&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#二叉树&quot; id=&quot;markdown-toc-二叉树&quot;&gt;二叉树&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#二叉树的递归遍历以及前中后序非递归遍历&quot; id=&quot;markdown-toc-二叉树的递归遍历以及前中后序非递归遍历&quot;&gt;二叉树的递归遍历以及（前中后序）非递归遍历&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#二叉树的层序遍历使用不使用额外的数据结构&quot; id=&quot;markdown-toc-二叉树的层序遍历使用不使用额外的数据结构&quot;&gt;二叉树的层序遍历（使用/不使用额外的数据结构）&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#图矩阵的深度优先和广度优先遍历&quot; id=&quot;markdown-toc-图矩阵的深度优先和广度优先遍历&quot;&gt;图（矩阵）的深度优先和广度优先遍历&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#动态规划&quot; id=&quot;markdown-toc-动态规划&quot;&gt;动态规划&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#最长公共子串&quot; id=&quot;markdown-toc-最长公共子串&quot;&gt;最长公共子串&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#最长递增子序列&quot; id=&quot;markdown-toc-最长递增子序列&quot;&gt;最长递增子序列&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#最长回文串&quot; id=&quot;markdown-toc-最长回文串&quot;&gt;最长回文串&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#实现一个lru的cache&quot; id=&quot;markdown-toc-实现一个lru的cache&quot;&gt;实现一个LRU的Cache&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#1-使用java自带的linkedhashmap&quot; id=&quot;markdown-toc-1-使用java自带的linkedhashmap&quot;&gt;1. 使用Java自带的LinkedHashMap&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2-仅使用java中的hashmap和deque&quot; id=&quot;markdown-toc-2-仅使用java中的hashmap和deque&quot;&gt;2. 仅使用Java中的HashMap和Deque&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#实现一个阻塞队列&quot; id=&quot;markdown-toc-实现一个阻塞队列&quot;&gt;实现一个阻塞队列&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#实现生产者消费者模型&quot; id=&quot;markdown-toc-实现生产者消费者模型&quot;&gt;实现生产者/消费者模型&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#实现两个线程交替打印&quot; id=&quot;markdown-toc-实现两个线程交替打印&quot;&gt;实现两个线程交替打印&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#文件的输入和输出&quot; id=&quot;markdown-toc-文件的输入和输出&quot;&gt;文件的输入和输出&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#字符串转整型&quot; id=&quot;markdown-toc-字符串转整型&quot;&gt;字符串转整型&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;常见排序算法的实现&quot;&gt;常见排序算法的实现&lt;/h1&gt;

&lt;h2 id=&quot;堆排序&quot;&gt;堆排序&lt;/h2&gt;

&lt;p&gt;https://www.jianshu.com/p/11655047ab58&lt;/p&gt;

&lt;p&gt;（升序排序）整体思路是：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;初始数组表示一个未排序的原始堆&lt;/li&gt;
  &lt;li&gt;构造一个最大堆&lt;/li&gt;
  &lt;li&gt;依次将堆顶元素交换到堆尾，调整使其保持最大堆，堆尾指针前移，直到所有元素有序（堆中之剩下堆顶元素）&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;快速排序&quot;&gt;快速排序&lt;/h2&gt;

&lt;p&gt;快排的核心部分在于partition的实现。partition需要找到一个元素的正确位置，将其移动到这个位置，并返回这个位置。&lt;/p&gt;

&lt;h2 id=&quot;归并排序&quot;&gt;归并排序&lt;/h2&gt;

&lt;p&gt;https://www.cnblogs.com/chengxiao/p/6194356.html&lt;/p&gt;

&lt;p&gt;归并排序的核心在于分治。分就是把原数组一分为二，然后再分，直到每一部分只剩下最多一个元素；并就是把结果合并，当每一部分最多有一个元素的时候可以直接交换，而多于一个元素的时候可以将有序的两部分从头到尾比较排序。&lt;/p&gt;

&lt;p&gt;在合并的时候会用到额外的空间，因此可以在一开始就定义一块和原数组相同大小的空间用于合并，避免递归过程中频繁开辟新的空间。&lt;/p&gt;

&lt;h1 id=&quot;链表&quot;&gt;链表&lt;/h1&gt;

&lt;h2 id=&quot;反转链表递归和非递归&quot;&gt;反转链表（递归和非递归）&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://leetcode-cn.com/problems/reverse-linked-list/&quot;&gt;https://leetcode-cn.com/problems/reverse-linked-list/&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;非递归头插法&quot;&gt;非递归（头插法）&lt;/h3&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ListNode&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reverseList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ListNode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ListNode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newHead&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ListNode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;next&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newHead&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;newHead&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newHead&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;递归头插法&quot;&gt;递归（头插法）&lt;/h3&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ListNode&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reverseList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ListNode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reverse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ListNode&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reverse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ListNode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newHead&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ListNode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newHead&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ListNode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;next&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newHead&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;newHead&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reverse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newHead&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;倒数第k个节点&quot;&gt;倒数第K个节点&lt;/h2&gt;

&lt;p&gt;使用双指针的时候要注意链表的长度，要考虑第二个指针在第一次前进k个节点时出现null的情况。&lt;/p&gt;

&lt;h2 id=&quot;检测环&quot;&gt;检测环&lt;/h2&gt;

&lt;p&gt;使用快慢指针。&lt;/p&gt;

&lt;h3 id=&quot;引申问题1为什么快指针的步长是2而不是345&quot;&gt;引申问题1：为什么快指针的步长是2，而不是3、4、5…？&lt;/h3&gt;

&lt;p&gt;https://blog.csdn.net/xgjonathan/article/details/18034825&lt;/p&gt;

&lt;p&gt;结论：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;无论步长为多少，快慢指针在环里都能相遇&lt;/li&gt;
  &lt;li&gt;步长设置为2能最快确定有环&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;引申问题2如何确定环的入口&quot;&gt;引申问题2：如何确定环的入口？&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;s：从起始点到环入口的距离&lt;/p&gt;

  &lt;p&gt;cl：环长度&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;当慢指针刚好进入环中，也就是慢指针走了 s 步之后，快指针走了 2s 步，所以快指针在环中走了 2s - s = s 步；
由于存在 s &amp;gt; cl 的情况，我们记快指针超出 ks（k为自然数） 的距离是 s % cl ；
此时，快指针需要追及慢指针的距离是 cl - s % cl；
因此，当慢指针在环中走了cl - s % cl 步后，快指针追上了慢指针；&lt;/p&gt;

&lt;p&gt;所以，相遇之后的慢指针距离 ks 的距离是 cl - (cl - s % cl) = s % cl 。因为有环的存在，我们可以把这个距离看成 s + M * cl，M 是正整数。所以相遇时的慢指针距离环的起始结点ks 是 s 。这时，我们再设置另一个指针从单向链表的头开始，以步长为 1 移动，移动 s 步后相遇，而这个相遇结点正好就是环的起始结点。&lt;/p&gt;

&lt;h1 id=&quot;二叉树&quot;&gt;二叉树&lt;/h1&gt;

&lt;h2 id=&quot;二叉树的递归遍历以及前中后序非递归遍历&quot;&gt;二叉树的递归遍历以及（前中后序）非递归遍历&lt;/h2&gt;

&lt;p&gt;https://www.cnblogs.com/dolphin0520/archive/2011/08/25/2153720.html&lt;/p&gt;

&lt;h2 id=&quot;二叉树的层序遍历使用不使用额外的数据结构&quot;&gt;二叉树的层序遍历（使用/不使用额外的数据结构）&lt;/h2&gt;

&lt;p&gt;https://blog.csdn.net/m0_37925202/article/details/80796010&lt;/p&gt;

&lt;h1 id=&quot;图矩阵的深度优先和广度优先遍历&quot;&gt;图（矩阵）的深度优先和广度优先遍历&lt;/h1&gt;

&lt;p&gt;https://blog.csdn.net/jeffleo/article/details/53309286&lt;/p&gt;

&lt;h1 id=&quot;动态规划&quot;&gt;动态规划&lt;/h1&gt;

&lt;h2 id=&quot;最长公共子串&quot;&gt;最长公共子串&lt;/h2&gt;

&lt;p&gt;https://segmentfault.com/a/1190000002641054&lt;/p&gt;

&lt;h2 id=&quot;最长递增子序列&quot;&gt;最长递增子序列&lt;/h2&gt;

&lt;p&gt;https://blog.csdn.net/u013178472/article/details/54926531&lt;/p&gt;

&lt;h2 id=&quot;最长回文串&quot;&gt;最长回文串&lt;/h2&gt;

&lt;p&gt;https://blog.csdn.net/kangroger/article/details/37742639&lt;/p&gt;

&lt;h1 id=&quot;实现一个lru的cache&quot;&gt;实现一个LRU的Cache&lt;/h1&gt;

&lt;p&gt;设计一个类LRUCache，满足以下条件：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;这个类的构造函数应该传入一个int类型的参数size，表示最多可容纳多少个元素&lt;/li&gt;
  &lt;li&gt;包含方法put、get。put是放入新元素，get是获取某一个元素&lt;/li&gt;
  &lt;li&gt;当LRUCache满了之后删除最近最久未使用的元素。&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;1-使用java自带的linkedhashmap&quot;&gt;1. 使用Java自带的LinkedHashMap&lt;/h2&gt;

&lt;p&gt;这种方法比较简单，就是维护一个LinkedHashMap，重写它的removeEldestEntry方法。&lt;/p&gt;

&lt;p&gt;LinkedHashMap的三个构造参数分别表示初始大小、扩张因子、使用accessOrder排序。如果不指定第三个参数，则会按照添加的顺序排序。&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LRUCache&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LinkedHashMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;LRUCache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;maxsize&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;cache&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LinkedHashMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.75f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
            &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
            &lt;span class=&quot;kd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;removeEldestEntry&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Entry&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;eldest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;2-仅使用java中的hashmap和deque&quot;&gt;2. 仅使用Java中的HashMap和Deque&lt;/h2&gt;

&lt;p&gt;本质上LRUCache就是一个HashMap，只不过需要使元素之间维持一定的顺序。LinkedHashMap实现了这个功能。如果不使用LinkedHashMap的话，则需要使用与其类似的思路，使用HashMap存储键值对，然后使用一个双端队列来记录访问顺序。这样效率很低，因为查询的时候需要先从队列中找到对应的key，完成一系列出队、入队操作最后再将这个key入队。&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LRUCache&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HashMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Deque&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;orderQueue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;LRUCache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;maxsize&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;orderQueue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ArrayDeque&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;cache&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HashMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&amp;gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;eldestKey&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;orderQueue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;poll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eldestKey&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;orderQueue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;offer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;||!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;containsKey&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)){&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Deque&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ArrayDeque&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;();&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(!(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;orderQueue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;poll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)){&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;isEmpty&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()){&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;orderQueue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;offerFirst&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;pop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;orderQueue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;offer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;实现一个阻塞队列&quot;&gt;实现一个阻塞队列&lt;/h1&gt;

&lt;p&gt;阻塞队列的实现需要保证多线程围绕队满/队空这两个条件来协作。当队满时，添加元素的线程应当阻塞；当队空时，获取元素的线程应当阻塞。&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt;  &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt;  &lt;span class=&quot;nc&quot;&gt;MyBlockingQueue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;volatile&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MyBlockingQueue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ArrayList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;synchronized&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InterruptedException&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&amp;gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;wait&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()==&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;// 如果为0，则可能有其他线程在阻塞get，因此调用notifyAll&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;notifyAll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;synchronized&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InterruptedException&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;isEmpty&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()){&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;wait&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&amp;gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;// 如果为size，则可能有其他线程在阻塞put，因此调用notifyAll&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;notifyAll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;实现生产者消费者模型&quot;&gt;实现生产者/消费者模型&lt;/h1&gt;

&lt;p&gt;使用上面的阻塞队列实现：&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ProducerConsumer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyBlockingQueue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Producer&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyBlockingQueue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Producer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MyBlockingQueue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
            &lt;span class=&quot;kd&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;queue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(){&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;Random&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rnd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Producing &quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InterruptedException&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;printStackTrace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Consumer&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyBlockingQueue&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Consumer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MyBlockingQueue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
            &lt;span class=&quot;kd&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;queue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(){&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Consuming &quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InterruptedException&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;printStackTrace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;MyBlockingQueue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyBlockingQueue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Producer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Producer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Consumer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Consumer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;实现两个线程交替打印&quot;&gt;实现两个线程交替打印&lt;/h1&gt;

&lt;p&gt;两个线程围绕一个合作变量flag进行合作。&lt;/p&gt;

&lt;p&gt;需要将flag声明为volatile，否则程序会卡住，这跟JMM有关系。理论上说，每次工作线程修改了flag都迟早会同步到主内存，但是如果while循环体为空的话，&lt;strong&gt;访问flag会太频繁导致JVM来不及将修改后的值同步到主内存&lt;/strong&gt;，这样一来程序就会一直卡在一个位置。&lt;/p&gt;

&lt;p&gt;如果不使用volatile也是可以的，将循环体里面加上一句&lt;code class=&quot;highlighter-rouge&quot;&gt;System.out.print(&quot;&quot;)&lt;/code&gt;就行了，这样可以降低访问flag的频率，从而使JVM有空将工作内存中的flag和主内存中的flag进行同步。&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PrintAlternately&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;volatile&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;EvenThread&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(){&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;101&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flag&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){}&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;flag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;OddThread&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(){&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flag&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){}&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;flag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;EvenThread&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EvenThread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;OddThread&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OddThread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;文件的输入和输出&quot;&gt;文件的输入和输出&lt;/h1&gt;

&lt;p&gt;最长见的方法是使用BufferedReader逐行（字符流）读取文件，使用FileWriter或者BufferedWriter（字符流）写文件，区别是后者提供了方法newLine来写换行符，而前者需要手动写入&lt;code class=&quot;highlighter-rouge&quot;&gt;\n&lt;/code&gt;或者&lt;code class=&quot;highlighter-rouge&quot;&gt;\r\n&lt;/code&gt;来换行。&lt;/p&gt;

&lt;p&gt;如果要求写文件的时候是在文件末尾添加，而不是整体覆盖，则FileWriter构造函数需要传入第二个参数true（表示使用append模式）。&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WriterReader&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BufferedReader&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reader&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BufferedReader&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FileReader&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;from.txt&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;BufferedWriter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BufferedWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FileWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;to.txt&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));){&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reader&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;readLine&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())!=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;newLine&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;printStackTrace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;字符串转整型&quot;&gt;字符串转整型&lt;/h1&gt;

&lt;p&gt;思路：应该尽快调试出来一个转化的程序，然后再考虑各种情况。面试时手撕代码时间非常有限，所以优先使代码能工作，而不是所有情况都考虑全了，发现时间不够或者疯狂调bug。&lt;/p&gt;

&lt;p&gt;先是一个合法字符串转正整数的方法：&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--){&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'0'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后再考虑下面这些情况：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;正负号&lt;/li&gt;
  &lt;li&gt;小数点&lt;/li&gt;
  &lt;li&gt;溢出&lt;/li&gt;
  &lt;li&gt;非法字符&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;考虑所有问题后，写出来的参考代码为：&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;// 先判断是否合法&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;valid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)){&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Invalid input string!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// 判断正负&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isNegative&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)==&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'-'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;isNegative&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;substring&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// 去掉前面的0&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)==&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'0'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;substring&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// 去掉小数点&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pointIndex&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getPointIndex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;extra&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pointIndex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pointIndex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;substring&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pointIndex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&amp;gt;&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'4'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;extra&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;substring&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pointIndex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;substring&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pointIndex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// 第一个字符是小数点，则输出只能是0或1&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;extra&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;// 溢出判断&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAXPOS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;MAX_VALUE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAXNEG&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;MIN_VALUE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isNegative&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'-'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAXNEG&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()){&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Overflow error!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()==&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAXNEG&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()){&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;compareTo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAXNEG&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&amp;gt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Overflow error!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;compareTo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAXNEG&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)==&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extra&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Overflow error!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAXPOS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()){&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Overflow error!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()==&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAXPOS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()){&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;compareTo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAXPOS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&amp;gt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Overflow error!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;compareTo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAXPOS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)==&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extra&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Overflow error!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;//当前的字符串为合法的，无符号的，只包含数字的字符串，可以直接转化为数字（按需要添加负号）&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--){&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'0'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isNegative&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getPointIndex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++){&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)==&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'.'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;valid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)==&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'-'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&amp;gt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;valid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;substring&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()));&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nPoint&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;ch:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toCharArray&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()){&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'.'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;nPoint&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nPoint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'9'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'0'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]){&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;//-2147483648 ~ 2147483647&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]{&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;-2147483648.6&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//Overflow error!&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;2147483647.1&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//2147483647&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;0.67&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//1&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;adfasdf&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//Invalid input string!&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;2147483648&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//Overflow error!&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;1.1.5&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//Invalid input string!&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;123.&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//123&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;.67&quot;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//1&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;s:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
            
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Sun, 06 Oct 2019 00:00:00 +0000</pubDate>
        <link>http://viaductooor.github.io/blog/2019/10/06/Frequently-Asked-Coding-Questions-During-an-Interview/</link>
        <guid isPermaLink="true">http://viaductooor.github.io/blog/2019/10/06/Frequently-Asked-Coding-Questions-During-an-Interview/</guid>
        
        <category>interview</category>
        
        
        <category>interview</category>
        
      </item>
    
      <item>
        <title>Note of java programming logic</title>
        <description>&lt;h2 id=&quot;第1章-编程基础&quot;&gt;第1章 编程基础&lt;/h2&gt;

&lt;p&gt;数据基本类型有：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;整数类型：byte/short/int/long，分别占1、2、4、8个字节&lt;/li&gt;
  &lt;li&gt;小数类型：float/double，分别占4、8字节&lt;/li&gt;
  &lt;li&gt;字符类型：char，占2个字节，使用常量字符串初始化时使用&lt;strong&gt;单引号&lt;/strong&gt;括起来&lt;/li&gt;
  &lt;li&gt;布尔类型：boolean&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;基本数据类型都有对应的数组类型。&lt;/p&gt;

&lt;p&gt;数组在存储的时候使用到了&lt;strong&gt;两块空间&lt;/strong&gt;，一块存储内容本身，一块存储内容的位置，因此在赋值的时候只改变引用即可。&lt;/p&gt;

&lt;p&gt;Java中的逻辑运算包括：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;与&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;或&lt;code class=&quot;highlighter-rouge&quot;&gt;|&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;非&lt;code class=&quot;highlighter-rouge&quot;&gt;!&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;异或&lt;code class=&quot;highlighter-rouge&quot;&gt;^&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;短路与&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;&amp;amp;&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;短路或&lt;code class=&quot;highlighter-rouge&quot;&gt;||&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;短路&lt;/strong&gt;的意义在于，可以通过前一个表达式的值得到结果，就不会再去看后一个表达式的值。&lt;/p&gt;

&lt;p&gt;Java中的函数有两类特殊类型的参数：数组和可变长度的参数。基本数据类型作为参数时，对其修改不会影响它本身的值，但是对数组的修改会影响原数组内容。可变长度参数&lt;code class=&quot;highlighter-rouge&quot;&gt;int max(int min, int ... a)&lt;/code&gt;实际上会将这多个参数转化成数组来处理。&lt;strong&gt;可变长度参数必须是参数列表中的最后一个&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;void函数也是可以使用return的，只不过不带值，表示提前结束函数执行：&lt;code class=&quot;highlighter-rouge&quot;&gt;return;&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;第2章-数据背后的二进制&quot;&gt;第2章 数据背后的二进制&lt;/h2&gt;

&lt;h3 id=&quot;整数&quot;&gt;整数&lt;/h3&gt;

&lt;p&gt;整数使用补码进行表示，因此正整数的表示方式与原码相同，负整数表示方式为&lt;strong&gt;绝对值按位取反加一&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;对于byte类型，整数最大表示是01111111，即127，负数最小表示为10000000，即-128，其他类型的整数也类似，&lt;strong&gt;负数能多表示一个数&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;位运算包括：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;左移&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;&amp;lt;&lt;/code&gt;，右边的低位补0，高位舍弃。左移一位相当于乘以2&lt;/li&gt;
  &lt;li&gt;无符号右移&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt;，右边的舍弃，左边补0&lt;/li&gt;
  &lt;li&gt;有符号右移&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;gt;&amp;gt;&lt;/code&gt;，右边舍弃，左边补什么取决于最高位是什么，原来是1就补1，是0就补0.右移一位相当于除以2&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;逻辑运算包括：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;按位与&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;按位或&lt;code class=&quot;highlighter-rouge&quot;&gt;|&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;按位取反&lt;code class=&quot;highlighter-rouge&quot;&gt;~&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;按位异或&lt;code class=&quot;highlighter-rouge&quot;&gt;^&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;小数&quot;&gt;小数&lt;/h3&gt;

&lt;p&gt;Java中的float类似是32位的，double类型是64位的。&lt;/p&gt;

&lt;p&gt;在32位格式中，1位表示符号23位表示尾数，8位表示指数。&lt;/p&gt;

&lt;p&gt;在64位格式中，1位表示符号，52位表示尾数，11位表示指数。&lt;/p&gt;

&lt;h3 id=&quot;字符的编码&quot;&gt;字符的编码&lt;/h3&gt;

&lt;p&gt;ASCII码中最高位设置为0，用剩下的7位表示字符，因此可以表示128个字符（0~127）。&lt;/p&gt;

&lt;p&gt;GB2312用两个字节表示汉字。两个字节的最高为都是1（0表示这是一个ASCII字符）。&lt;/p&gt;

&lt;p&gt;GBK兼容GB2312，但是&lt;strong&gt;低位字节&lt;/strong&gt;最高位可能是0，因为这个编码规定汉字固定使用两个字节表示，因此都是两个两个处理，不考虑低位。&lt;/p&gt;

&lt;p&gt;Unicode是统一编码方式，有多种方案，包括UTF-32，UTF-16和UTF-8：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;UTF-32：用4个字节表示一个字符，根据最高位是第一个字节还是最后一个字节可以分为大端字节序（UTF-32-BE）和小端字节序（UTF-32LE）&lt;/li&gt;
  &lt;li&gt;UTF-16：用变长字节表示。常用字符用两个字节表示，增补字符用4个字节表示&lt;/li&gt;
  &lt;li&gt;UTF-8：用变长字节表示，编号小的使用的字节就少，编号大的使用的字节就多，位1~4不等。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在Java中，可以使用&lt;code class=&quot;highlighter-rouge&quot;&gt;public byte[] getBytes(String charsetName)&lt;/code&gt;来获取一个字符串的给定编码格式的二进制形式；使用&lt;code class=&quot;highlighter-rouge&quot;&gt;public String(byte bytes[], String charsetName)&lt;/code&gt;可以按照指定编码格式解读一个二进制数组位字符串。&lt;/p&gt;

&lt;h3 id=&quot;char&quot;&gt;char&lt;/h3&gt;

&lt;p&gt;char可以表示英文字符和中文字符，并且可以进行算术运算和比较，原因是char在Java中是用UTF-16BE表示的。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;char本质上是一个固定占用两个字节的无符号正整数，这个正整数对应于Unicode编号，用于表示那个Unicode编号对应的字符&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;第3章-类的基础&quot;&gt;第3章 类的基础&lt;/h2&gt;

&lt;p&gt;每个类都要有一个构造方法，如果没有定义，Java会自动生成一个默认构造方法。一旦定义了，就不会生成默认构造方法。&lt;/p&gt;

&lt;p&gt;Java编译和运行时，都需要指定一个classpath，即类路径。类路径可以有多个，对于直接的class文件，路径就是class文件的根目录；对于jar包，路径是jar包的完整名称（包括路径和jar包名）。在windows系统中，多个路径使用分号分隔，在其他系统中，以冒号分隔。&lt;/p&gt;

&lt;p&gt;Java运行时，会根据类的&lt;strong&gt;完全限定名&lt;/strong&gt;寻找并加载类，寻找的方式就是在类路径中寻找，如果是class文件的根目录，则直接查看是否有对应的子目录及文件，如果是jar文件，则现在内存中解压文件，然后再查看是否有对应的类。&lt;/p&gt;

&lt;h2 id=&quot;第4章-类的继承&quot;&gt;第4章 类的继承&lt;/h2&gt;

&lt;h3 id=&quot;动态绑定&quot;&gt;动态绑定&lt;/h3&gt;

&lt;p&gt;子类可以重写父类的非private方法，当调用的时候会动态绑定，执行子类的方法。&lt;/p&gt;

&lt;h3 id=&quot;静态绑定&quot;&gt;静态绑定&lt;/h3&gt;

&lt;p&gt;如果子类的实例变量、静态方法和静态变量与父类重名，&lt;/p&gt;

&lt;p&gt;当通过b（静态类型Base）访问时，访问的是Base的变量和方法，当通过c（静态类型Child）访问时，访问的是Child的变量和方法，这称之为静态绑定，即访问绑定到变量的静态类型。静态绑定在程序编译阶段即可决定，而动态绑定则要等到程序运行时。&lt;strong&gt;实例变量、静态变量、静态方法、private方法，都是静态绑定的。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;当有多个重名函数的时候，在决定要调用哪个函数的过程中，首先是按照参数类型进行匹配的，换句话说，寻找在所有重载版本中最匹配的，然后才看变量的动态类型，进行动态绑定。&lt;/p&gt;

&lt;p&gt;一个父类的变量能不能转换为一个子类的变量，取决于这个父类变量的动态类型（即引用的对象类型）是不是这个子类或这个子类的子类。&lt;/p&gt;

&lt;h2 id=&quot;第15章-并发基础知识&quot;&gt;第15章 并发基础知识&lt;/h2&gt;

&lt;h3 id=&quot;151-线程的基本概念&quot;&gt;15.1 线程的基本概念&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;线程表示一条单独的执行流，有自己的程序执行计数器，有自己的栈。&lt;/strong&gt;在Java中有两种方式创建线程：一种是继承Thread，另一种是实现Runnable接口。&lt;strong&gt;不管哪种方式，最后都应该通过调用Thread的start方法来启动线程&lt;/strong&gt;（实现Runnable接口后，应该将此Runnable对象作为参数构造一个Thread对象）。直接调用run方法并不会启动一条单独的执行流，而是会将其作为一个普通方法在主线程中执行。&lt;/p&gt;

&lt;p&gt;使用Thread的静态方法currentThread可以返回当前执行的线程对象。&lt;/p&gt;

&lt;p&gt;线程有以下基本属性和方法：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;id和name：id是递增的整数，name可以通过setName设置；&lt;/li&gt;
  &lt;li&gt;优先级：优先级只是一种&lt;strong&gt;建议&lt;/strong&gt;，而非强制；&lt;/li&gt;
  &lt;li&gt;状态：包含NEW, TERMINATED, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING；&lt;/li&gt;
  &lt;li&gt;是否为daemon线程：当程序中只剩下daemon线程时，程序会退出。垃圾回收线程也是daemon线程；&lt;/li&gt;
  &lt;li&gt;sleep方法：睡眠期间，该线程会让出CPU。&lt;strong&gt;睡眠期间可以被中断&lt;/strong&gt;，抛出InterruptedException；&lt;/li&gt;
  &lt;li&gt;yield方法：静态方法，&lt;strong&gt;建议&lt;/strong&gt;让其他线程运行，并非强制；&lt;/li&gt;
  &lt;li&gt;join方法：如果在线程A中调用B.join()，那么线程A会等待线程B执行完再继续执行。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;竞态条件：&lt;/strong&gt;当多个线程访问和操作同一个对象时，最终执行结果与执行时序有关，可能正确也可能不正确。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;内存可见性问题：&lt;/strong&gt;因为数据会被存储到内存、CPU的寄存器以及各级缓存中，所以当一个线程访问一个变量的时候，访问到的不一定是最新的值（主要是&lt;strong&gt;多处理器&lt;/strong&gt;会有多个缓存，他们一般会周期性同步）。&lt;/p&gt;

&lt;p&gt;解决内存可见性的方法一般是：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;使用volatile关键字&lt;/li&gt;
  &lt;li&gt;使用synchronized关键字或显式锁同步&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;152-理解synchronized&quot;&gt;15.2 理解synchronized&lt;/h3&gt;

&lt;p&gt;synchronized有三种使用方法：实例方法、静态方法和代码块。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;实例方法&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;synchronized实例方法实际保护的是&lt;strong&gt;同一个对象&lt;/strong&gt;的方法调用，因此多个线程可以执行同一个synchronized实例方法，只要他们属于不同对象。&lt;/p&gt;

&lt;p&gt;同一个对象中的不同synchronized方法是不能同时执行的，但是非synchronized方法可以和synchronized方法同时执行，所以，&lt;strong&gt;一般在保护变量时，需要在所有访问该变量的方法上加上synchronized&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;静态方法&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;synchronized修饰的静态方法同样保护的是一个对象，只不过是&lt;strong&gt;类对象&lt;/strong&gt;。因此synchronized修饰的静态方法和实例方法保护的是不同的对象，他们可以同时执行。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;代码块&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;synchronized代码块可以传入参数指定保护哪一个对象，比如&lt;code class=&quot;highlighter-rouge&quot;&gt;synchronized(this){//code block}&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;synchronized有三个特性：可重入性、内存可见性和可能出现死锁。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;可重入性&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;可重入性表示：对同一个线程，它在获得了锁之后，如果要调用其他需要&lt;strong&gt;同样锁&lt;/strong&gt;的代码，可以直接调用。并不是所有锁都是可重入的。&lt;/p&gt;

&lt;p&gt;可重入是通过记录锁的&lt;strong&gt;持有线程&lt;/strong&gt;和&lt;strong&gt;持有数量&lt;/strong&gt;来实现的。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;当调用被synchronized保护的代码时， 检查对象是否已被锁，如果是，再检查是否被当前线程锁定，如果是，增加持有数量，如果不是被当前线程锁定，才加入等待队列，当释放锁时，减少持有数量，当数量变为 0 时才释放整个锁。&lt;/p&gt;

  &lt;p&gt;马俊昌. Java编程的逻辑 (Java核心技术系列) (Kindle 位置 8524-8526). 北京华章图文信息有限公司. Kindle 版本.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;内存可见性&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;synchronized能保证内存可见性，在释放锁时，所有写入都会写回内存，而获得锁后，都会从内存读最新数据。&lt;/p&gt;

&lt;p&gt;不过如果只是为了保证内存可见性，synchronized成本过高，可以直接使用关键字volatile修饰变量，保证读写到内存的最新值，而非缓存的值。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;死锁&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;如果两个线程分别请求相同的两个锁，但是请求顺序相反，那么就可能出现死锁（相互等待对方释放锁）。为了避免死锁，应该：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;尽量避免在持有一个锁的同时去申请另一个锁&lt;/li&gt;
  &lt;li&gt;如果要申请多个锁，所有代码都应该按照相同的顺序去申请锁&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;使用&lt;strong&gt;jstack&lt;/strong&gt;可以检查是否发生了死锁，以及死锁的情况。&lt;/p&gt;

&lt;p&gt;类&lt;code class=&quot;highlighter-rouge&quot;&gt;Collection&lt;/code&gt;的一些方法可以返回线程安全的同步容器，比如&lt;code class=&quot;highlighter-rouge&quot;&gt;synchronizedCollection&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;synchronizedList&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;synchronizedMap&lt;/code&gt;，他们是给&lt;strong&gt;所有容器方法&lt;/strong&gt;都加上synchronized来实现安全的，因此性能比较低，一般使用下面这些容器类来实现线程安全：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;CopyOnWriteArrayList&lt;/li&gt;
  &lt;li&gt;ConcurrentHashMap&lt;/li&gt;
  &lt;li&gt;ConcurrentLinkedQueue&lt;/li&gt;
  &lt;li&gt;ConcurrentSkipListSet&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;153-线程的基本协作机制&quot;&gt;15.3 线程的基本协作机制&lt;/h3&gt;

&lt;h4 id=&quot;waitnotify&quot;&gt;wait/notify&lt;/h4&gt;

&lt;p&gt;wait方法和notify方法都是Object类定义的方法。&lt;/p&gt;

&lt;p&gt;因为wait/notify方法是围绕某一个&lt;strong&gt;协作变量&lt;/strong&gt;工作的，容易出现竞态条件，所以一&lt;strong&gt;般只能在synchronized代码块内被调用&lt;/strong&gt;。如果调用wait/notify时，当前线程没有持有锁，会抛出异常。&lt;/p&gt;

&lt;p&gt;每个对象除了有一把锁和等待队列，还有一个&lt;strong&gt;条件队列&lt;/strong&gt;用于线程间的协作。当调用wait方法时，会将当前线程放到条件队列上并阻塞，表示当前线程执行不下去了，需要等待一个&lt;strong&gt;条件&lt;/strong&gt;，这个条件它自己改变不了，需要其他线程去改变。当其他线程改变了条件之后，会调用这个对象的&lt;strong&gt;notify&lt;/strong&gt;方法。&lt;/p&gt;

&lt;p&gt;notify方法的作用就是从条件队列中选择一个线程，将其从队列中&lt;strong&gt;移除并唤醒&lt;/strong&gt;。notifyAll方法会移除条件队列中的所有线程并全部唤醒。&lt;/p&gt;

&lt;p&gt;一般wait的调用方式是：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-Java&quot;&gt;synchronized(obj){
    while(条件不成立)
        obj.wait();
    ...//执行条件成立后的操作
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;其中的“条件”一般是指协作变量。&lt;/p&gt;

&lt;h4 id=&quot;waitnotify的原理&quot;&gt;wait/notify的原理&lt;/h4&gt;

&lt;p&gt;虽然wait被synchronized保护，但是&lt;strong&gt;调用wait时，线程会释放对象锁&lt;/strong&gt;。wait的过程为：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;把当前线程放入条件等待队列，释放对象锁，阻塞等待，线程状态变为WAITING或TIMED_WAITING；&lt;/li&gt;
  &lt;li&gt;等待时间到或被其他线程调用notify/notifyAll时从条件队列中移除，这时要&lt;strong&gt;重新竞争对象锁&lt;/strong&gt;：
    &lt;ol&gt;
      &lt;li&gt;如果能获得锁，线程状态变为RUNNABLE，并从wait调用中返回。&lt;/li&gt;
      &lt;li&gt;否则，线程进入对象锁等待队列，线程状态变为BLOCKED，只有在获得锁之后才会从wait调用中返回。&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;调用notify会把条件队列中等待的线程唤醒并从等待队列中移除，但是&lt;strong&gt;不会立即释放对象锁&lt;/strong&gt;，只有在包含notify的synchronized代码块执行完后，等待的线程才会从wait调用中返回。&lt;/p&gt;

&lt;h4 id=&quot;生产者消费者模式&quot;&gt;生产者/消费者模式&lt;/h4&gt;

&lt;p&gt;生产者/消费者模式围绕一个队列工作，队列满时生产者需要&lt;strong&gt;等待队列不满&lt;/strong&gt;，队列空时消费者需要&lt;strong&gt;等待队列不空&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;实现这个模式的时候应该使用notifyAll而不是notify，避免唤醒的是同类线程。&lt;/p&gt;

&lt;p&gt;Java提供了专门的阻塞队列实现：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;接口BlockingQueue和BlockingDeque；&lt;/li&gt;
  &lt;li&gt;ArrayBlockingQueue；&lt;/li&gt;
  &lt;li&gt;LinkedBlockingQueue和LinkedBlockingDeque；&lt;/li&gt;
  &lt;li&gt;PriorityBlockingQueue&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;同时开始&quot;&gt;同时开始&lt;/h4&gt;

&lt;p&gt;在同时开始这个模型中，所有线程都&lt;strong&gt;等待同一个开始信号&lt;/strong&gt;（协作变量）。&lt;/p&gt;

&lt;h4 id=&quot;等待结束&quot;&gt;等待结束&lt;/h4&gt;

&lt;p&gt;join()方法实际上调用的是wait，主要代码是：&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isAlive&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()){&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;wait&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;集合点&quot;&gt;集合点&lt;/h4&gt;

&lt;p&gt;多个线程分头行动，各自到达一个集合点，在集合点需要集齐所有线程，交换数据，然后进行下一步动作。协作的共享变量可以是线程的个数，等待条件为这个变量减为0。&lt;/p&gt;

&lt;h3 id=&quot;154-线程的中断&quot;&gt;15.4 线程的中断&lt;/h3&gt;

&lt;p&gt;停止一个线程的主要方法是中断（interrupt），&lt;strong&gt;中断不是强迫终止一个线程，它是一种协作机制，给线程传递一个信号，但是由线程来决定如何以及何时退出&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;线程不同状态对interrupt的反应是不同的：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;RUNNABLE：如果再运行中且没有执行IO操作，interrupt只是设置中断标志位，没有其他作用。线程应该再运行过程中合适的位置检查中断标志位，比如循环的开始处。&lt;/li&gt;
  &lt;li&gt;WAITING/TIMED_WAITING：会抛出InterruptedException，&lt;strong&gt;中断标志位被清空&lt;/strong&gt;，所以抛出异常后输出isInterrupted会输出false。处理这类异常时，应该先清理，再调用interrupt设置中断标志位，告诉其他代码知道发生了中断。&lt;/li&gt;
  &lt;li&gt;BLOCKED：只会设置线程的中断标志位，而线程依然会处于BLOCKED状态。&lt;strong&gt;使用synchronized关键字获取锁的过程中不响应中断请求，显式锁Lock接口支持以响应中断的方式获取锁&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;NEW/TERMINATE：没有任何效果，中断标志位也不会被设置。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;作为线程的实现者，应该提供明确的取消/关闭方法，并用文档描述清楚其行为；作为线程的调用者，应该使用其取消/关闭方法，而不是贸然调用interrupt。&lt;/p&gt;

&lt;h2 id=&quot;第16章-并发包的基石&quot;&gt;第16章 并发包的基石&lt;/h2&gt;

&lt;h3 id=&quot;161-原子变量和cas&quot;&gt;16.1 原子变量和CAS&lt;/h3&gt;

&lt;p&gt;原子变量包括AtomicBoolean, AtomicInteger, AtomicLong, AtomicReference，以及针对数组类型的AtomicLongArray等。&lt;/p&gt;

&lt;p&gt;原子变量的关键在于，它包含了一些以原子方式实现组合操作的方法，比如（以AtomicInteger为例）：&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getAndSet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newValue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getAndIncrement&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getAndDecrement&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getAndAdd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;delta&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;incrementAndGet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;decrementAndGet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这些方法都依赖一个方法：&lt;code class=&quot;highlighter-rouge&quot;&gt;compareAndSet&lt;/code&gt;，简称CAS。CAS操作有两个参数expect和update，如果当前值等于expect，则更新为update并返回true，否则不更新并返回false。&lt;/p&gt;

&lt;p&gt;一般原子操作的主体都是死循环，更新失败则不断循环，直到更新成功为止。&lt;/p&gt;

&lt;p&gt;CAS是乐观的，假设有较少的冲突情况；synchronized是悲观的，假设很可能发生冲突。&lt;/p&gt;

&lt;p&gt;使用CAS方式更新有可能出现ABA问题，&lt;strong&gt;这是一个与程序的逻辑有关的问题，一般不是问题&lt;/strong&gt;。Java中解决的办法是使用AtomicStampedReference，改值的同时附加一个时间戳，只有值和时间戳都相同才修改。&lt;/p&gt;

&lt;p&gt;Java并发包中提供了一些非阻塞容器，比如：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;ConcurrentLinkedQueue和ConcurrentLinkedDeque：非阻塞并发队列&lt;/li&gt;
  &lt;li&gt;ConcurrentSkipListMap和ConcurrentSkipListSet：非阻塞并发Map和Set&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;162-显式锁&quot;&gt;16.2 显式锁&lt;/h3&gt;

&lt;p&gt;显式锁位于java.util.concurrent.locks下；锁接口Lock，主要实现类是ReentrantLock；读写锁接口ReadWriteLock，主要实现类是ReentrantReadWriteLock。&lt;/p&gt;

&lt;p&gt;Lock中的方法有：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;lock()/unlock()：普通的获取锁和释放锁，lock()会阻塞直到成功；&lt;/li&gt;
  &lt;li&gt;lockInterruptibly()：获取锁，&lt;strong&gt;可响应中断。&lt;/strong&gt;如果被中断了则抛出InterruptedException；&lt;/li&gt;
  &lt;li&gt;tryLock()：&lt;strong&gt;非阻塞&lt;/strong&gt;地尝试获取锁，&lt;strong&gt;立即返回&lt;/strong&gt;。如果获取成功则返回true，否则返回false。&lt;/li&gt;
  &lt;li&gt;tryLock(long time, TimeUnit unit)：先尝试获取锁，如果成功则立即返回true；否则阻塞等待，最长等待时间是time；在等待的同时响应中断，会抛出InterruptedException；如果等待后获得了锁则返回true，否则返回false。&lt;/li&gt;
  &lt;li&gt;newCondition：新建一个条件，一个Lock可以关联多个条件。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;使用显式锁，&lt;strong&gt;一定要记得调用unlock&lt;/strong&gt;。一般将lock之后的代码包装到try语句内，在finally语句内释放锁。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;使用tryLock()可以避免死锁&lt;/strong&gt;。在持有一个锁获取另一个锁失败的时候，可以释放已持有的锁，然后重试获取所有锁。&lt;/p&gt;

&lt;h4 id=&quot;reentrantlock的实现原理&quot;&gt;ReentrantLock的实现原理&lt;/h4&gt;

&lt;p&gt;在最底层，ReentrantLock依赖于CAS，而在Java中首先是依赖于类LockSupport。&lt;/p&gt;

&lt;p&gt;LockSupport类里包含这些方法：&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;park&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; 
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;parkNanos&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nanos&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;parkUntil&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deadline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;unpark&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Thread&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;park使得当前线程放弃CPU，进入等待状态（WAITING），操作系统不再对它进行调度，什么时候再调度呢？有其他线程对它调用了unpark，unpark使参数指定的线程恢复可运行状态。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;park是响应中断的&lt;/strong&gt;，当有中断发生时，park会返回，中断标志位会被设置。&lt;/p&gt;

&lt;p&gt;parkNanos可以设置最长等待时间；parkUntil可以设置等待到什么时候，是相对于纪元时的毫秒数。&lt;/p&gt;

&lt;p&gt;LockSupport中的park/unpark方法是调用了Unsafe类中的对应方法（和CAS相同）。&lt;/p&gt;

&lt;p&gt;ReentrantLock直接依赖的类是&lt;strong&gt;AQS&lt;/strong&gt;（AbstractQueuedSynchronizer）。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AQS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;ReentrantLock内部有三个类和AQS有关：&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Sync&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AbstractQueuedSynchronizer&lt;/span&gt; 
&lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;NonfairSync&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sync&lt;/span&gt; 
&lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FairSync&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sync&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;ReentrantLock默认使用NonfairSync。ReentrantLock内部使用state表示是否被锁以及持有数量，如果未被锁定则立即获得锁，否则调用acquire(1)获得锁。acquire是AQS中的方法。&lt;/p&gt;

&lt;h4 id=&quot;对比reentrantlock和synchronized&quot;&gt;对比ReentrantLock和synchronized&lt;/h4&gt;

&lt;p&gt;相比synchronized，ReentrantLock可以实现与synchronized相同的语义，而且支持以&lt;strong&gt;非阻塞&lt;/strong&gt;方式获取锁，&lt;strong&gt;可以响应中断&lt;/strong&gt;，&lt;strong&gt;可以限时&lt;/strong&gt;，更为&lt;strong&gt;灵活&lt;/strong&gt;。不过，synchronized的使用更为简单，写的代码更少，也更不容易出错。&lt;/p&gt;

&lt;p&gt;synchronized代表一种&lt;strong&gt;声明式编程思维&lt;/strong&gt;，程序员更多的是表达表达一种同步声明，由Java系统负责具体实现，程序员不知道其实现细节；显式锁代表一种&lt;strong&gt;命令式编程思维&lt;/strong&gt;，程序员实现所有细节。&lt;/p&gt;

&lt;p&gt;声明式编程的好处除了简单，还在于性能，在较新版本的JVM上，ReentrantLock和synchronized的性能是接近的，但Java编译器和虚拟机可以不断优化synchronized的实现，比如自动分析synchronized的使用，对于没有锁竞争的场景，自动省略对锁获取/释放的调用。&lt;/p&gt;

&lt;p&gt;简单总结下，&lt;strong&gt;能用synchronized就用synchronized&lt;/strong&gt;，不满足要求时再考虑Reentrant-Lock。&lt;/p&gt;

&lt;h3 id=&quot;163-显式条件&quot;&gt;16.3 显式条件&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;wait/notify与synchronized配合使用，显式条件与显式锁配合使用。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;创建条件变量需要调用显示锁的newCondition()方法。&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Condition&lt;/em&gt;是一个接口，定义为：&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Condition&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Interrupted&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Uninterruptibly&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;awaitNanos&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nanosTimeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InterruptedException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TimeUnit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InterruptedException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;awaitUntil&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Date&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deadline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InterruptedException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;signal&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;signalAll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中，await功能与wait类似，signal/signalAll与notify/notifyAll类似。&lt;em&gt;Condition&lt;/em&gt;对象也是有wait和notify/notifyAll方法的，因为这些方法属于对象，但是在这里不能使用，因为wait和notify/notifyAll只能在synchronized中使用，一定不能混淆。&lt;/p&gt;

&lt;h2 id=&quot;第17章-并发容器&quot;&gt;第17章 并发容器&lt;/h2&gt;

&lt;h3 id=&quot;171-写时复制&quot;&gt;17.1 写时复制&lt;/h3&gt;

&lt;p&gt;写时复制的类主要有&lt;em&gt;CopyOnWriteArrayList&lt;/em&gt;和&lt;em&gt;CopyOnWriteArraySet&lt;/em&gt;。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;写时复制的含义是：每次修改操作，都会新建一个数组，复制原数组的内容到新数组，在新数组上进行需要的修改，然后以原子方式设置内部数组的引用。&lt;/strong&gt;所有的读操作都是拿到当前引用的数组，然后直接访问该数组，修改操作不会影响读。&lt;/p&gt;

&lt;p&gt;&lt;em&gt;CopyOnWriteArrayList&lt;/em&gt;特点有:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;线程安全，可以被多个线程并发访问；&lt;/li&gt;
  &lt;li&gt;迭代器不支持修改；&lt;/li&gt;
  &lt;li&gt;以原子方式支持一些复合操作：&lt;em&gt;addIfAbsent(E e)&lt;/em&gt;和&lt;em&gt;addAllAbsent(Collection&amp;lt;? extends E&amp;gt; c)&lt;/em&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;CopyOnWriteArraySet&lt;/em&gt;是基于&lt;em&gt;CopyOnWriteArrayList&lt;/em&gt;实现的。&lt;/p&gt;

&lt;h3 id=&quot;172-concurrenthashmap&quot;&gt;17.2 ConcurrentHashMap&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;HashMap&lt;/em&gt;中可能发生的并发问题：多个线程同时扩容哈希表的时候，可能会出现死循环。&lt;/p&gt;

&lt;p&gt;&lt;em&gt;ConcurrentHashMap&lt;/em&gt;实现了&lt;em&gt;Map&lt;/em&gt;接口和&lt;em&gt;ConcurrentMap&lt;/em&gt;接口，&lt;em&gt;ConcurrentMap&lt;/em&gt;接口定义了一些原子复合操作：&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;V&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;putIfAbsent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;K&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;V&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;K&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;V&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;oldValue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;V&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newValue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;V&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;K&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;V&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在Java 7中&lt;em&gt;ConcurrentHashMap&lt;/em&gt;实现高并发的原因有：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;分段锁&lt;/li&gt;
  &lt;li&gt;读不需要锁&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;内部使用了&lt;strong&gt;CAS&lt;/strong&gt;实现，达到的效果是：&lt;strong&gt;对于写操作，需要获取锁，不能并行，但是读操作可以，多个读可以并行，写的同时可以读&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;Java 8中对&lt;em&gt;ConcurrentHashMap&lt;/em&gt;的实现进一步做了优化，首先是&lt;strong&gt;在哈希冲突比较严重的时候将单链表转化为平衡的排序二叉树&lt;/strong&gt;，提高查找效率；其次是细化了锁的力度，改成了&lt;strong&gt;每一个哈希桶（指向一个单链表或树）都有一个单独的锁&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;&lt;em&gt;ConcurrentHashMap&lt;/em&gt;的&lt;strong&gt;弱一致性&lt;/strong&gt;：创建迭代器后，会按照哈希表的结构遍历每个元素，但是在遍历中，内部元素可能会发生变化，如果变化发生在已遍历过的部分，迭代器就不会反映出来，而如果变化发生在未遍历过的部分，迭代器就会发现并反映出来。&lt;/p&gt;

&lt;p&gt;###17.3 基于跳表的Map和Set&lt;/p&gt;

&lt;p&gt;&lt;em&gt;TreeMap&lt;/em&gt;/&lt;em&gt;TreeSet&lt;/em&gt;对应的并发版本是&lt;em&gt;ConcurrentSkipListMap&lt;/em&gt;和&lt;em&gt;ConcurrentSkipListSet&lt;/em&gt;，其中&lt;strong&gt;ConcurrentSkipListSet是基于ConcurrentSkipListMap实现&lt;/strong&gt;的。&lt;/p&gt;

&lt;p&gt;&lt;em&gt;ConcurrentSkipListMap&lt;/em&gt;的特点有：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;没有使用锁，所有操作都是无阻塞的，所有操作都可以并行，包括写，多线程可以同时写。&lt;/li&gt;
  &lt;li&gt;弱一致性&lt;/li&gt;
  &lt;li&gt;实现了&lt;em&gt;CurrentMap&lt;/em&gt;接口，支持一些原子复合操作&lt;/li&gt;
  &lt;li&gt;可排序，实现了&lt;em&gt;SortedMap&lt;/em&gt;和&lt;em&gt;NavigableMap&lt;/em&gt;接口&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;174-并发队列&quot;&gt;17.4 并发队列&lt;/h3&gt;

&lt;p&gt;并发队列包括：&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;类型&lt;/th&gt;
      &lt;th&gt;包含的类&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;无锁非阻塞并发队列&lt;/td&gt;
      &lt;td&gt;ConcurrentLinkedQueue、ConcurrentLinkedDeque&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;普通阻塞队列&lt;/td&gt;
      &lt;td&gt;基于数组的ArrayBlockingQueue，基于链表的LinkedBlockingQueue和LinkedBlockingDeque&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;优先级阻塞队列&lt;/td&gt;
      &lt;td&gt;PriorityBlockingQueue&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;延时阻塞队列&lt;/td&gt;
      &lt;td&gt;DelayQueue&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;其他阻塞队列&lt;/td&gt;
      &lt;td&gt;SynchronousQueue和LinkedTransferQueue&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;无锁非阻塞：&lt;/strong&gt;不使用锁，所有操作总是可以立即执行，主要通过&lt;strong&gt;循环CAS&lt;/strong&gt;实现并发安全。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;阻塞队列：&lt;/strong&gt;使用&lt;strong&gt;锁&lt;/strong&gt;和&lt;strong&gt;条件&lt;/strong&gt;，很多操作都需要先获取锁或满足特定条件。获取不到锁或等待条件时，会等待（阻塞），获取到锁或满足条件再返回。&lt;/p&gt;

&lt;p&gt;这些队列迭代都不会抛出&lt;em&gt;ConcurrentModificationException&lt;/em&gt;，都是弱一致性的。&lt;/p&gt;

&lt;h4 id=&quot;无锁非阻塞并发队列&quot;&gt;无锁非阻塞并发队列&lt;/h4&gt;

&lt;p&gt;适用于多个线程并发使用一个队列的场合，特点有：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;基于链表实现&lt;/li&gt;
  &lt;li&gt;没有限制大小&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;普通阻塞队列&quot;&gt;普通阻塞队列&lt;/h4&gt;

&lt;p&gt;常用于&lt;strong&gt;生产者/消费者模式&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;&lt;em&gt;ArrayBlockingQueue&lt;/em&gt;基于循环数组实现，有界，创建时需指定大小，且再运行过程中不会改变。&lt;/p&gt;

&lt;p&gt;&lt;em&gt;LinkedBlockingQueue&lt;/em&gt;基于单向链表实现，可选择指定最大长度，默认无限。&lt;/p&gt;

&lt;p&gt;内部，两者都是使用显示锁&lt;em&gt;ReentrantLock&lt;/em&gt;和显式条件&lt;em&gt;Condition&lt;/em&gt;实现。&lt;/p&gt;

&lt;h4 id=&quot;优先级阻塞队列&quot;&gt;优先级阻塞队列&lt;/h4&gt;

&lt;p&gt;大部分结构都与&lt;em&gt;PriorityQueue&lt;/em&gt;相同，包括使用堆结构，没有大小限制，要求元素实现&lt;em&gt;Comparable&lt;/em&gt;结构或者主动提供Comparator等。&lt;/p&gt;

&lt;p&gt;区别在于，&lt;em&gt;PriorityBlockingQueue&lt;/em&gt;实现了&lt;em&gt;BlockingQueue&lt;/em&gt;接口。使用&lt;em&gt;ReentrantLock&lt;/em&gt;和&lt;em&gt;Condition&lt;/em&gt;实现并发。&lt;/p&gt;

&lt;h4 id=&quot;延时阻塞队列&quot;&gt;延时阻塞队列&lt;/h4&gt;

&lt;p&gt;是一种特殊的优先级队列，无界，要求每个元素都实现&lt;em&gt;Delayed&lt;/em&gt;接口。&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Delayed&lt;/em&gt;扩展了&lt;em&gt;Comparable&lt;/em&gt;接口，包含一个额外的方法&lt;em&gt;getDelay&lt;/em&gt;，用于返回&lt;strong&gt;再延迟多少时间&lt;/strong&gt;，如果小于或等于0则不再延迟。&lt;/p&gt;

&lt;p&gt;&lt;em&gt;DelayQueue&lt;/em&gt;可以用于实现定时任务，按元素的延时时间出列，只有当元素的延时过期之后才能被拿走。&lt;/p&gt;

&lt;p&gt;&lt;em&gt;DelayQueue&lt;/em&gt;是基于&lt;em&gt;PriorityQueue&lt;/em&gt;实现的。&lt;/p&gt;

&lt;h4 id=&quot;其他阻塞队列&quot;&gt;其他阻塞队列&lt;/h4&gt;

&lt;p&gt;SynchronousQueue&lt;/p&gt;

&lt;p&gt;LinkedTransferQueue&lt;/p&gt;

&lt;h2 id=&quot;第18章-异步任务执行服务&quot;&gt;第18章 异步任务执行服务&lt;/h2&gt;

&lt;h3 id=&quot;181-基本概念和原理&quot;&gt;18.1 基本概念和原理&lt;/h3&gt;

&lt;p&gt;异步任务执行服务设计到的基本接口有：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;Runnable&lt;/em&gt;和&lt;em&gt;Callable&lt;/em&gt;：表示要执行的异步任务&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Executor&lt;/em&gt;和&lt;em&gt;ExecutorService&lt;/em&gt;：表示执行服务&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Future&lt;/em&gt;：表示异步任务的结果&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Runnable&lt;/em&gt;没有返回结果，而&lt;em&gt;Callable&lt;/em&gt;有；&lt;em&gt;Runnable&lt;/em&gt;不会抛出异常，而&lt;em&gt;Callable&lt;/em&gt;会。&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Executor&lt;/em&gt;表示最简单的执行服务，只包含一个&lt;code class=&quot;highlighter-rouge&quot;&gt;void execute(Runnable command)&lt;/code&gt;；&lt;em&gt;ExecutorService&lt;/em&gt;扩展了&lt;em&gt;Executor&lt;/em&gt;，包含一系列的submit方法（返回&lt;em&gt;Future&lt;/em&gt;）。&lt;/p&gt;

&lt;p&gt;Future包含的方法有：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;cancel：取消异步任务&lt;/li&gt;
  &lt;li&gt;get：返回异步任务的最终结果&lt;/li&gt;
  &lt;li&gt;isCancelled&lt;/li&gt;
  &lt;li&gt;isDone&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;get用于返回异步任务最终的结果，如果任务还未完成，会阻塞等待；另外有一个get可以设置时间，超时任务还未结束则会抛出&lt;em&gt;TimeoutException&lt;/em&gt;。&lt;/p&gt;

&lt;p&gt;任务最大概有三种结果：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;正常完成，返回执行结果；如果是&lt;em&gt;Runnable&lt;/em&gt;且未设置结果，返回null&lt;/li&gt;
  &lt;li&gt;任务执行抛出了异常，get方法会将异常包装位&lt;em&gt;ExcecutionException&lt;/em&gt;重新抛出，通过异常的getCause可以获取原异常&lt;/li&gt;
  &lt;li&gt;任务被取消了，get方法会抛出异常&lt;em&gt;CancellationException&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;调用get方法的线程被中断了，抛出&lt;em&gt;InterruptedException&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;182-线程池&quot;&gt;18.2 线程池&lt;/h3&gt;

&lt;p&gt;线程池主要由两个概念组成：&lt;strong&gt;任务队列&lt;/strong&gt;和&lt;strong&gt;工作者线程&lt;/strong&gt;。工作者线程主体是一个循环，循环从队列中接受任务并执行，任务队列保存待执行的任务。&lt;/p&gt;

&lt;p&gt;线程池的优点有：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;可以重用线程，避免线程创建的开销；&lt;/li&gt;
  &lt;li&gt;任务过多时，通过排队避免创建过多线程，减少系统资源消耗和竞争，确保任务有序完成。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Java并发包中线程池的实现类是&lt;em&gt;ThreadPoolExecutor&lt;/em&gt;，它继承自&lt;em&gt;AbstractExecutorService&lt;/em&gt;，实现了&lt;em&gt;ExecutorService&lt;/em&gt;。&lt;/p&gt;

&lt;p&gt;线程池的大小主要与4个参数有关：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;corePoolSize：核心线程个数；&lt;/li&gt;
  &lt;li&gt;maximumPoolSize：最大线程个数；&lt;/li&gt;
  &lt;li&gt;keepAliveTime和unit：空闲线程存活时间。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ThreadPoolExcecutor要求的队列类型是阻塞队列。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ThreadPoolExecutor实现了生产者/消费者模式，工作者线程是消费者，任务提交者是生产者，线程池自己维护任务队列。当遇到类似生产者/消费者问题时，应该优先考虑直接使用线程池。&lt;/strong&gt;&lt;/p&gt;

&lt;h3 id=&quot;183-定时任务&quot;&gt;18.3 定时任务&lt;/h3&gt;

&lt;p&gt;在Java中，主要有两种方式实现定时任务：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;使用java.util包中的Timer和TimerTask&lt;/li&gt;
  &lt;li&gt;使用Java并发包中的ScheduledExecutorService&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Timer内部主要由任务队列和Timer线程两部分组成。任务队列是基于堆实现的&lt;strong&gt;优先级队列&lt;/strong&gt;，按照下次执行的时间排优先级。&lt;strong&gt;一个Timer对象只有一个Timer线程&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;ScheduledThreadPoolExecutor的实现思路与Timer基本类似，都有一个基于堆的优先级队列，保存待执行的定时任务，不同在于：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;它的背后是线程池，可以有多个线程执行任务；&lt;/li&gt;
  &lt;li&gt;它在任务执行后在设置下次执行的时间，对于固定延时的任务更为合理；&lt;/li&gt;
  &lt;li&gt;任务执行线程会捕获任务执行过程中的所有异常，一个定时任务的异常不会影响其他定时任务，不过，发生异常的任务不会再被调度。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;第19章-同步和协作工具类&quot;&gt;第19章 同步和协作工具类&lt;/h2&gt;

&lt;h2 id=&quot;第21章-反射&quot;&gt;第21章 反射&lt;/h2&gt;

&lt;p&gt;反射是在运行时，而非编译时，动态获取类型的信息，比如接口信息、成员信息、方法信息、构造方法信息等，根据这些动态获取到信息创建对象、访问/修改成员、调用方法等。&lt;/p&gt;

&lt;p&gt;获取类对象的方法有：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;使用Object类的&lt;code class=&quot;highlighter-rouge&quot;&gt;getClass&lt;/code&gt;方法（基本类型不可用）&lt;/li&gt;
  &lt;li&gt;使用&lt;code class=&quot;highlighter-rouge&quot;&gt;类名.class&lt;/code&gt;获取，基本类型可用&lt;/li&gt;
  &lt;li&gt;使用&lt;code class=&quot;highlighter-rouge&quot;&gt;Class.forName&lt;/code&gt;方法根据类名获取类对象&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;反射和泛型&lt;/p&gt;

&lt;p&gt;虽然在运行时，泛型参数会被擦除，但是类信息Class中依然有关于泛型的一些信息，可以通过反射得到：&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;//Class中的&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TypeVariable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getTypeParameters&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; 

&lt;span class=&quot;c1&quot;&gt;//Field中的    &lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetGenericType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; 

&lt;span class=&quot;c1&quot;&gt;//Method中的&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getGenericReturnType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getGenericParameterTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getGenericExceptionTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
    
&lt;span class=&quot;c1&quot;&gt;//Constructor&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getGenericParameterTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;反射的优点是灵活，缺点有：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;更容易出现运行时错误，因为使用反射的话类型是运行时才知道的，编译器无法检查；&lt;/li&gt;
  &lt;li&gt;反射的性能更低一些，在访问字段、调用方法前，反射要先查找对应的Field/Method&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;因此，如果能用接口实现同样的灵活性，就不要使用反射。&lt;/p&gt;

&lt;h2 id=&quot;第22章-注解&quot;&gt;第22章 注解&lt;/h2&gt;

&lt;p&gt;Java的内置注解有：&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;注解&lt;/th&gt;
      &lt;th&gt;含义&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;@Override&lt;/td&gt;
      &lt;td&gt;表示重写了父类的该方法&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;@Deprecated&lt;/td&gt;
      &lt;td&gt;过时了，不建议使用&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;@SupressWarnings&lt;/td&gt;
      &lt;td&gt;压制Java的编译警告，可以指定压制哪些类型的警告&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;创建注解的关键字为&lt;code class=&quot;highlighter-rouge&quot;&gt;@interface&lt;/code&gt;，有两个元注解@Target和@Retention。&lt;/p&gt;

&lt;p&gt;@Target表示注解的目标，是一个ElementType的枚举值，可以有多个（用&lt;code class=&quot;highlighter-rouge&quot;&gt;{}&lt;/code&gt;表示）包含：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;TYPE：表示类、接口、注解或枚举声明&lt;/li&gt;
  &lt;li&gt;FIELD：字段&lt;/li&gt;
  &lt;li&gt;METHOD：方法&lt;/li&gt;
  &lt;li&gt;PARAMETER：方法参数&lt;/li&gt;
  &lt;li&gt;CONSTRUCTOR：构造方法&lt;/li&gt;
  &lt;li&gt;LOCAL_VARIABLE：本地变量&lt;/li&gt;
  &lt;li&gt;MODULE：模块（Java 9引入的）&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;@Target默认适用于所有类型。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;@Retention表示注解信息保留到什么时候，值为RentionPolicy的枚举类型，可以是：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;SOURCE：仅保留到源代码&lt;/li&gt;
  &lt;li&gt;CLASS：保留到字节码文件中&lt;/li&gt;
  &lt;li&gt;RUNTIME：保留到运行时&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;@Retention默认为CLASS。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;注解可以设置参数，不过参数类型只能是基本类型、String、Class、枚举、注解以及这些类型的数组。&lt;/p&gt;

&lt;p&gt;可以用反射来获取注解类型：&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;//获取所有注解&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Annotation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getAnnotations&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;//获取所有本元素上直接声明的注解&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Annotation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getDeclaredAnnotations&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;//获取指定类型的注解，没有返回null&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Annotation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getAnnotation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;annotationClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;//判断是否有指定类型的注解&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;isAnnotationPresent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Annotation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;annotations&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;第23章-动态代理&quot;&gt;第23章 动态代理&lt;/h2&gt;

&lt;h3 id=&quot;231-静态代理&quot;&gt;23.1 静态代理&lt;/h3&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SimpleStaticProxyDemo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; 
    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; 
        &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sayHello&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt; 
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; 
    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;RealService&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; 
        &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
		&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sayHello&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; 
            &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; hello&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt; 
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; 
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; 
    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TraceProxy&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; 
        &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IService&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;realService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; 
        &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;TraceProxy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IService&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;realService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; 
            &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;realService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;realService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; 
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; 
        &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sayHello&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; 
            &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; entering sayHello&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt; 
            &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;realService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sayHello&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt; 
            &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; leaving sayHello&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt; 
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; 
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; 
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; 
        &lt;span class=&quot;n&quot;&gt;IService&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;realService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;RealService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt; 
        &lt;span class=&quot;n&quot;&gt;IService&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;proxyService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TraceProxy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;realService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt; 
        &lt;span class=&quot;n&quot;&gt;proxyService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sayHello&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt; 
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在上面的例子中，我们想达到的目的是在实际对象的方法调用前后加一些调试语句。为了在不修改原类 的情况下达到这个目的，我们在代码中创建了一个代理类 TraceProxy，它的代码是在写程序时固定的， 所以称为&lt;strong&gt;静态代理&lt;/strong&gt;。&lt;/p&gt;

&lt;h2 id=&quot;第24章-类加载机制&quot;&gt;第24章 类加载机制&lt;/h2&gt;

&lt;h3 id=&quot;241-类加载的机制和过程&quot;&gt;24.1 类加载的机制和过程&lt;/h3&gt;

&lt;p&gt;运行Java程序，就是执行java这个命令，指定包含main方法的完整类名，以及一个classpath，即类路径。类路径可以有多个，对于直接的class文件，路径是class文件的根目录，对于jar包，路径是jar包的完整名称（包括路径和jar包名）。&lt;/p&gt;

&lt;p&gt;Java运行时，会根据类的&lt;strong&gt;完全限定名&lt;/strong&gt;寻找并加载类，寻找的方式基本就是在系统类和指定的类路径中寻找，如果是class文件的根目录，则直接查看是否有对应的子目录及文件；如果是jar文件，则首先在内存中解压文件，然后再查看是否有对应的类。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;负责加载类的类就是类加载器，它的输入是完全限定的类名，输出是Class对象。&lt;/strong&gt;一般类加载器有3个（Java9之前）：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;启动类加载器（Bootstrap ClassLoader）：是Java虚拟机实现的一部分，负责加载Java的基础类（String、ArrayList等）；&lt;/li&gt;
  &lt;li&gt;扩展类加载器（Extension ClassLoader）：负责加载扩展类，一般是位于JAVA_HOME/lib/ext中的jar包；&lt;/li&gt;
  &lt;li&gt;应用程序类加载器（Application ClassLoader）：负责加载应程序的类，包括自己写的和引入的第三方类库，即所有在类路径中指定的类。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;双亲委派模型&lt;/p&gt;

&lt;p&gt;加载一个类的过程为：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;判断是否已被加载，加载过了则直接返回Class对象，一个类只会被一个ClassLoader加载一次；&lt;/li&gt;
  &lt;li&gt;如果没有被加载，先让父ClassLoader去加载，如果加载成功，返回得到的Class对象；&lt;/li&gt;
  &lt;li&gt;在父ClassLoader没有加载成功的前提下，自己尝试加载类&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;双亲委派模型的好处是：避免Java类库被覆盖的问题。如果用户也定义了一个类java.lang.String，通过双亲委派，java.lang.String只会被Bootstrap ClassLoader加载，避免自定义的String覆盖Java类库中的定义。&lt;/p&gt;

&lt;h3 id=&quot;242-classloader和forname加载类的区别&quot;&gt;24.2 ClassLoader和forName加载类的区别&lt;/h3&gt;

&lt;p&gt;调用Class的forName方法来加载类有两个方法：&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;forName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;lt;?&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;forName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ClassLoader&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loader&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;forName是可以设置是否执行初始化代码（static语句块）的，而且默认执行；而ClassLoader的loadClass不会执行类的初始化代码。&lt;/p&gt;

</description>
        <pubDate>Thu, 01 Aug 2019 00:00:00 +0000</pubDate>
        <link>http://viaductooor.github.io/blog/2019/08/01/Note-of-Java-Programming-Logic/</link>
        <guid isPermaLink="true">http://viaductooor.github.io/blog/2019/08/01/Note-of-Java-Programming-Logic/</guid>
        
        
      </item>
    
      <item>
        <title>Wordlist - Harry Potter VI</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#phrases&quot; id=&quot;markdown-toc-phrases&quot;&gt;Phrases&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#words&quot; id=&quot;markdown-toc-words&quot;&gt;Words&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#insubstantial&quot; id=&quot;markdown-toc-insubstantial&quot;&gt;insubstantial&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#duplicity&quot; id=&quot;markdown-toc-duplicity&quot;&gt;duplicity&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tribute&quot; id=&quot;markdown-toc-tribute&quot;&gt;tribute&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#-quarry&quot; id=&quot;markdown-toc--quarry&quot;&gt;★ quarry&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#octave&quot; id=&quot;markdown-toc-octave&quot;&gt;octave&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#crescent&quot; id=&quot;markdown-toc-crescent&quot;&gt;crescent&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#graze&quot; id=&quot;markdown-toc-graze&quot;&gt;graze&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#forthwith&quot; id=&quot;markdown-toc-forthwith&quot;&gt;forthwith&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#teeming&quot; id=&quot;markdown-toc-teeming&quot;&gt;teeming&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#-itch&quot; id=&quot;markdown-toc--itch&quot;&gt;★ itch&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#omniscient&quot; id=&quot;markdown-toc-omniscient&quot;&gt;omniscient&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#henchman&quot; id=&quot;markdown-toc-henchman&quot;&gt;henchman&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#-obliterate&quot; id=&quot;markdown-toc--obliterate&quot;&gt;★ obliterate&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#juncture&quot; id=&quot;markdown-toc-juncture&quot;&gt;juncture&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#&quot; id=&quot;markdown-toc-&quot;&gt;—&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#unrequited&quot; id=&quot;markdown-toc-unrequited&quot;&gt;unrequited&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#-attendant&quot; id=&quot;markdown-toc--attendant&quot;&gt;★ attendant&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#sap&quot; id=&quot;markdown-toc-sap&quot;&gt;sap&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#squire&quot; id=&quot;markdown-toc-squire&quot;&gt;squire&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#squalid&quot; id=&quot;markdown-toc-squalid&quot;&gt;squalid&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#-ado&quot; id=&quot;markdown-toc--ado&quot;&gt;★ ado&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#-grandeur&quot; id=&quot;markdown-toc--grandeur&quot;&gt;★ grandeur&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#blackcurrant&quot; id=&quot;markdown-toc-blackcurrant&quot;&gt;blackcurrant&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#oblivious&quot; id=&quot;markdown-toc-oblivious&quot;&gt;oblivious&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#impart&quot; id=&quot;markdown-toc-impart&quot;&gt;impart&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#-grimace&quot; id=&quot;markdown-toc--grimace&quot;&gt;★ grimace&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#forestall&quot; id=&quot;markdown-toc-forestall&quot;&gt;forestall&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#matron&quot; id=&quot;markdown-toc-matron&quot;&gt;matron&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;phrases&quot;&gt;Phrases&lt;/h1&gt;

&lt;h1 id=&quot;words&quot;&gt;Words&lt;/h1&gt;

&lt;h2 id=&quot;insubstantial&quot;&gt;insubstantial&lt;/h2&gt;

&lt;p&gt;脆弱的，不坚固的；虚幻的&lt;/p&gt;

&lt;p&gt;The castle ghosts were there too, barely visible in the bright sunlight, discernible only when they moved, shimmering insubstantially in the gleaming air.&lt;/p&gt;

&lt;h2 id=&quot;duplicity&quot;&gt;duplicity&lt;/h2&gt;

&lt;p&gt;口是心非，虚伪&lt;/p&gt;

&lt;p&gt;… asked Harry, who wanted every detail of Snape’s duplicity and infamy, feverishly collecting more reasons to hate him, to swear vengeance.&lt;/p&gt;

&lt;h2 id=&quot;tribute&quot;&gt;tribute&lt;/h2&gt;

&lt;p&gt;赞辞，献礼&lt;/p&gt;

&lt;p&gt;Our students should pay tribute, it is fitting.&lt;/p&gt;

&lt;h2 id=&quot;-quarry&quot;&gt;★ quarry&lt;/h2&gt;

&lt;p&gt;猎物&lt;/p&gt;

&lt;p&gt;Distracted by the flames, the Inferi seemed unaware that their quarry was leaving as Dumbledore led Harry back to the boat.&lt;/p&gt;

&lt;h2 id=&quot;octave&quot;&gt;octave&lt;/h2&gt;

&lt;p&gt;（音）八度&lt;/p&gt;

&lt;p&gt;‘Someone’s dead,’ said Malfoy and his voice seemed to go up an octave as he said it.&lt;/p&gt;

&lt;h2 id=&quot;crescent&quot;&gt;crescent&lt;/h2&gt;

&lt;p&gt;新月，弦月&lt;/p&gt;

&lt;p&gt;… illuminated by the light of the crescent moon.&lt;/p&gt;

&lt;h2 id=&quot;graze&quot;&gt;graze&lt;/h2&gt;

&lt;p&gt;v. 擦破皮&lt;/p&gt;

&lt;p&gt;Harry wiped his grazed forarm upon the stone.&lt;/p&gt;

&lt;h2 id=&quot;forthwith&quot;&gt;forthwith&lt;/h2&gt;

&lt;p&gt;adv. 立即，毫不拖延地&lt;/p&gt;

&lt;p&gt;Hagrid, please tell the Heads of House - Slughorn can represent Slytherin - that I want to see them in my office forthwith.&lt;/p&gt;

&lt;h2 id=&quot;teeming&quot;&gt;teeming&lt;/h2&gt;

&lt;p&gt;拥挤的，热闹的&lt;/p&gt;

&lt;p&gt;The great black lake, teeming with the dead.&lt;/p&gt;

&lt;h2 id=&quot;-itch&quot;&gt;★ itch&lt;/h2&gt;

&lt;p&gt;发痒；渴望&lt;/p&gt;

&lt;p&gt;(Harry) was itching to try the charm out, but thought it best not to in front of Hermione.&lt;/p&gt;

&lt;h2 id=&quot;omniscient&quot;&gt;omniscient&lt;/h2&gt;

&lt;p&gt;博识的，无所不知的&lt;/p&gt;

&lt;p&gt;You are omniscient as ever, Dumbledore.&lt;/p&gt;

&lt;h2 id=&quot;henchman&quot;&gt;henchman&lt;/h2&gt;

&lt;p&gt;心腹&lt;/p&gt;

&lt;p&gt;Why are you come here tonight, surrounded by henchmen, to request a job we both know you do not want?&lt;/p&gt;

&lt;h2 id=&quot;-obliterate&quot;&gt;★ obliterate&lt;/h2&gt;

&lt;p&gt;消除…的痕迹，抹除&lt;/p&gt;

&lt;p&gt;The files were obliterated in file.&lt;/p&gt;

&lt;p&gt;The heavy snow obliterated their footprints.&lt;/p&gt;

&lt;p&gt;She tried to obliterate the unpleasant experiences.&lt;/p&gt;

&lt;h2 id=&quot;juncture&quot;&gt;juncture&lt;/h2&gt;

&lt;p&gt;时机，关头；连接处&lt;/p&gt;

&lt;p&gt;It seemed extraordinary that Hermione’s mania for upholding rules could have abandoned her at this crucial juncture.&lt;/p&gt;

&lt;h1&gt;—&lt;/h1&gt;

&lt;h2 id=&quot;unrequited&quot;&gt;unrequited&lt;/h2&gt;

&lt;p&gt;单相思的，得不到回报的&lt;/p&gt;

&lt;p&gt;Of course, it is also possible that her unrequited love and the attendant despair sapped her of her powers; that can happen.&lt;/p&gt;

&lt;h2 id=&quot;-attendant&quot;&gt;★ attendant&lt;/h2&gt;

&lt;p&gt;伴随的&lt;/p&gt;

&lt;p&gt;nuclear power, with all its attendant risks.&lt;/p&gt;

&lt;h2 id=&quot;sap&quot;&gt;sap&lt;/h2&gt;

&lt;p&gt;使虚弱&lt;/p&gt;

&lt;h2 id=&quot;squire&quot;&gt;squire&lt;/h2&gt;

&lt;p&gt;乡绅&lt;/p&gt;

&lt;p&gt;You can imagine the gossip it caused when the squire’s son ran off with the tramp’s daughter Merope.&lt;/p&gt;

&lt;h2 id=&quot;squalid&quot;&gt;squalid&lt;/h2&gt;

&lt;p&gt;肮脏邋遢的&lt;/p&gt;

&lt;p&gt;She was standing beside a steaming pot on a grimy black stove, and was fiddlig around with the shelf of squalid-looking pots and pans above it.&lt;/p&gt;

&lt;h2 id=&quot;-ado&quot;&gt;★ ado&lt;/h2&gt;

&lt;p&gt;忙乱，纷杂&lt;/p&gt;

&lt;p&gt;Herbology, Arithmancy, Ancient Runes and Potions, and shot off to a first-period Ancient Runes class without further ado.&lt;/p&gt;

&lt;h2 id=&quot;-grandeur&quot;&gt;★ grandeur&lt;/h2&gt;

&lt;p&gt;伟大，壮观&lt;/p&gt;

&lt;p&gt;Lack of sense coupled with a great liking for grandeur meant that the family gold was squandered several generations before Marvolo was in charge.&lt;/p&gt;

&lt;h2 id=&quot;blackcurrant&quot;&gt;blackcurrant&lt;/h2&gt;

&lt;p&gt;黑加仑&lt;/p&gt;

&lt;p&gt;Her potion already resembled the ‘smooth, blackcurrant-coloured liquid’.&lt;/p&gt;

&lt;h2 id=&quot;oblivious&quot;&gt;oblivious&lt;/h2&gt;

&lt;p&gt;健忘的&lt;/p&gt;

&lt;p&gt;Seemingly oblivious to the sensational nature of the news he had just imparted, Dumbledore said nothing more about staff appointments.&lt;/p&gt;

&lt;h3 id=&quot;impart&quot;&gt;impart&lt;/h3&gt;

&lt;p&gt;透露，通知&lt;/p&gt;

&lt;h2 id=&quot;-grimace&quot;&gt;★ grimace&lt;/h2&gt;

&lt;p&gt;/gri’meis/&lt;/p&gt;

&lt;p&gt;扮鬼脸&lt;/p&gt;

&lt;p&gt;‘When we were in Diagon Alley -‘ Harry began, but Mr Weasley forestalled with him with a grimace.&lt;/p&gt;

&lt;h3 id=&quot;forestall&quot;&gt;forestall&lt;/h3&gt;

&lt;p&gt;预先阻止&lt;/p&gt;

&lt;h2 id=&quot;matron&quot;&gt;matron&lt;/h2&gt;

&lt;p&gt;护士长&lt;/p&gt;

&lt;p&gt;… to visit Madam Pomfrey, the matron, …&lt;/p&gt;

</description>
        <pubDate>Sat, 22 Jun 2019 00:00:00 +0000</pubDate>
        <link>http://viaductooor.github.io/blog/2019/06/22/Learn-English-Harry-Poter-VI/</link>
        <guid isPermaLink="true">http://viaductooor.github.io/blog/2019/06/22/Learn-English-Harry-Poter-VI/</guid>
        
        <category>english</category>
        
        
        <category>private</category>
        
      </item>
    
      <item>
        <title>Start Redis</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#tutorial&quot; id=&quot;markdown-toc-tutorial&quot;&gt;Tutorial&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#开始&quot; id=&quot;markdown-toc-开始&quot;&gt;开始&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#列表&quot; id=&quot;markdown-toc-列表&quot;&gt;列表&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#集合&quot; id=&quot;markdown-toc-集合&quot;&gt;集合&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#哈希对象&quot; id=&quot;markdown-toc-哈希对象&quot;&gt;哈希（对象）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#更多&quot; id=&quot;markdown-toc-更多&quot;&gt;更多&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;tutorial&quot;&gt;Tutorial&lt;/h1&gt;

&lt;p&gt;Tutorial是 &lt;a href=&quot;try.redis.io&quot;&gt;try.redis.io&lt;/a&gt; 中入门内容的总结。&lt;/p&gt;

&lt;h2 id=&quot;开始&quot;&gt;开始&lt;/h2&gt;

&lt;p&gt;Redis使用“键值对”存储数据。Redis中的命令大小写皆可。&lt;/p&gt;

&lt;p&gt;Redis中的String类型数据可以不加双引号。&lt;/p&gt;

&lt;p&gt;使用&lt;code class=&quot;highlighter-rouge&quot;&gt;SET key value&lt;/code&gt;存储值，使用&lt;code class=&quot;highlighter-rouge&quot;&gt;GET key&lt;/code&gt;取值。&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; SET server:name &lt;span class=&quot;s2&quot;&gt;&quot;fido&quot;&lt;/span&gt;
OK
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; GET server:name
&lt;span class=&quot;s2&quot;&gt;&quot;fido&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其他一些简单的命令：&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;命令&lt;/th&gt;
      &lt;th&gt;含义&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;INCR&lt;/td&gt;
      &lt;td&gt;使这个键对应的值增加1。如果这个键之前不存在，则设置为1（不会报错）。&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;DEL&lt;/td&gt;
      &lt;td&gt;删除这个键&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;使用INCR命令实现自增，而不使用传统的“取值-&amp;gt;加一-&amp;gt;设定值”，原因是这样可以保证这一操作的原子性，避免多线程读写数据可能会发生的冲突。&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; GET newkey
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;nil&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; INCR newkey
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;integer&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Redis中的数据（键值对）可以通过&lt;code class=&quot;highlighter-rouge&quot;&gt;EXPIRE&lt;/code&gt;设置有效时间和用&lt;code class=&quot;highlighter-rouge&quot;&gt;TTL&lt;/code&gt;查看剩余时间（单位是秒）。&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; SET resource:lock &lt;span class=&quot;s2&quot;&gt;&quot;demo&quot;&lt;/span&gt; 
OK
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; EXPIRE resource:lock 120
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;integer&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 1
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; TTL resource:lock
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;integer&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 109
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;列表&quot;&gt;列表&lt;/h2&gt;

&lt;p&gt;Redis支持列表作为值，列表相关的操作有：&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;命令&lt;/th&gt;
      &lt;th&gt;含义&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;RPUSH&lt;/td&gt;
      &lt;td&gt;在列表末端添加值&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;LPUSH&lt;/td&gt;
      &lt;td&gt;在列表头部添加值&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;LRANGE&lt;/td&gt;
      &lt;td&gt;获取子列表，两端包含&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;LLEN&lt;/td&gt;
      &lt;td&gt;获取列表长度&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;LPOP&lt;/td&gt;
      &lt;td&gt;获取第一个元素并将其从列表中删除&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;RPOP&lt;/td&gt;
      &lt;td&gt;获取最后一个元素并将其从列表中删除&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;需要注意的是：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;如果之前没有这个键，也是可以直接push的&lt;/li&gt;
  &lt;li&gt;不能通过&lt;code class=&quot;highlighter-rouge&quot;&gt;GET&lt;/code&gt;操作来获取所有的元素，只能用&lt;code class=&quot;highlighter-rouge&quot;&gt;LRANGE key 0 -1&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; RPUSH students &lt;span class=&quot;s2&quot;&gt;&quot;Alice&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;integer&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 1
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; RPUSH students &lt;span class=&quot;s2&quot;&gt;&quot;Bob&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;integer&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 2
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; RPUSH students &lt;span class=&quot;s2&quot;&gt;&quot;Harry&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;integer&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 3
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; RPUSH students &lt;span class=&quot;s2&quot;&gt;&quot;Ron&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;integer&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 4
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; GET students
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;error&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; WRONGTYPE Operation against a key holding the wrong kind of value
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; LRANGE students 0 &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;
1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Alice&quot;&lt;/span&gt;
2&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Bob&quot;&lt;/span&gt;
3&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Harry&quot;&lt;/span&gt;
4&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Ron&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;集合&quot;&gt;集合&lt;/h2&gt;

&lt;p&gt;Redis支持无序集合作为值，相关的操作有：&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;操作&lt;/th&gt;
      &lt;th&gt;含义&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;SADD&lt;/td&gt;
      &lt;td&gt;添加元素&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;SREM&lt;/td&gt;
      &lt;td&gt;删除元素&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;SISMEMBER&lt;/td&gt;
      &lt;td&gt;查询集合是否包含某元素&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;SMEMBERS&lt;/td&gt;
      &lt;td&gt;查看集合中的所有元素&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;SUNION&lt;/td&gt;
      &lt;td&gt;求两个集合的并集&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; SADD fruits &lt;span class=&quot;s2&quot;&gt;&quot;apple&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;integer&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 1
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; SADD fruits &lt;span class=&quot;s2&quot;&gt;&quot;pear&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;integer&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 1
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; SADD fruits &lt;span class=&quot;s2&quot;&gt;&quot;banana&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;integer&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 1
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; SREM fruits &lt;span class=&quot;s2&quot;&gt;&quot;apple&quot;&lt;/span&gt;
1
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; SMEMBERS fruits
1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;banana&quot;&lt;/span&gt;
2&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;pear&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; SISMEMBER fruits &lt;span class=&quot;s2&quot;&gt;&quot;apple&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;integer&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Redis中的无序集合可能存在一些问题，所以从1.2版本之后引入了有序集合的概念（“Sorted Sets”）。有序集合添加元素的方式为：&lt;code class=&quot;highlighter-rouge&quot;&gt;ZADD key score value&lt;/code&gt;，其中key为键，score为分数（用于排序），value为值。&lt;/p&gt;

&lt;p&gt;使用&lt;code class=&quot;highlighter-rouge&quot;&gt;ZRANGE key begin_index end_index&lt;/code&gt;可以查看key对应的集合中第begin_index到第end_index的元素。&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; ZADD scores 89 &lt;span class=&quot;s2&quot;&gt;&quot;math&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;integer&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 1
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; ZADD scores 100 &lt;span class=&quot;s2&quot;&gt;&quot;nature&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;integer&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 1
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; ZADD scores 33 &lt;span class=&quot;s2&quot;&gt;&quot;physics&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;integer&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 1
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; ZADD scores 33 &lt;span class=&quot;s2&quot;&gt;&quot;chemistry&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;integer&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 1
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; ZRANGE scores 0 &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;
1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;chemistry&quot;&lt;/span&gt;
2&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;physics&quot;&lt;/span&gt;
3&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;math&quot;&lt;/span&gt;
4&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;nature&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;哈希对象&quot;&gt;哈希（对象）&lt;/h2&gt;

&lt;p&gt;Redis同样支持哈希数据结构（Hashes）作为值。类似于javascript中的对象类型（或者JSON），哈希结构包含多个键值对，非常适合用于存储对象。&lt;/p&gt;

&lt;p&gt;哈希结构相关的操作有：&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;命令&lt;/th&gt;
      &lt;th&gt;含义&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;HSET&lt;/td&gt;
      &lt;td&gt;设置（或添加）属性&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;HGETALL&lt;/td&gt;
      &lt;td&gt;获取所有属性&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;HMSET&lt;/td&gt;
      &lt;td&gt;一次设置多个属性&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;HGET&lt;/td&gt;
      &lt;td&gt;获取一个哈希对象的某一个属性&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;HINCRBY&lt;/td&gt;
      &lt;td&gt;将某一个值增加一个量&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;HDEL&lt;/td&gt;
      &lt;td&gt;删除这个对象的某一个属性&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; HSET mycomputer memory &lt;span class=&quot;s2&quot;&gt;&quot;16GB&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;integer&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 1
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; HSET mycomputer graphic-card &lt;span class=&quot;s2&quot;&gt;&quot;Nvidia&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;integer&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 1
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; HSET mycomputer hard-disk &lt;span class=&quot;s2&quot;&gt;&quot;1TB Seagate&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;integer&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 1
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; HGETALL mycomputer
1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;memory&quot;&lt;/span&gt;
2&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;16GB&quot;&lt;/span&gt;
3&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;graphic-card&quot;&lt;/span&gt;
4&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Nvidia&quot;&lt;/span&gt;
5&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;hard-disk&quot;&lt;/span&gt;
6&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;1TB Seagate&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; HGET mycomputer graphic-card
&lt;span class=&quot;s2&quot;&gt;&quot;Nvidia&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;更多&quot;&gt;更多&lt;/h2&gt;

&lt;p&gt;更多内容参考：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://redis.io/documentation&quot;&gt;Redis Documentation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://redis.io/commands&quot;&gt;Command Reference&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://redis.io/topics/twitter-clone&quot;&gt;Implement a Twitter Clone in Redis&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://redis.io/topics/data-types-intro&quot;&gt;Introduction to Redis Data Types&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Mon, 17 Jun 2019 00:00:00 +0000</pubDate>
        <link>http://viaductooor.github.io/blog/2019/06/17/Start-Redis/</link>
        <guid isPermaLink="true">http://viaductooor.github.io/blog/2019/06/17/Start-Redis/</guid>
        
        <category>sql</category>
        
        <category>redis</category>
        
        
        <category>sql</category>
        
      </item>
    
      <item>
        <title>Wordlist - Harry Potter V</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#phrases&quot; id=&quot;markdown-toc-phrases&quot;&gt;Phrases&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#trestle-table&quot; id=&quot;markdown-toc-trestle-table&quot;&gt;trestle table&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#words&quot; id=&quot;markdown-toc-words&quot;&gt;Words&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#nutcase&quot; id=&quot;markdown-toc-nutcase&quot;&gt;nutcase&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#senile&quot; id=&quot;markdown-toc-senile&quot;&gt;senile&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#retaliate&quot; id=&quot;markdown-toc-retaliate&quot;&gt;retaliate&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#mallet&quot; id=&quot;markdown-toc-mallet&quot;&gt;mallet&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#edgy&quot; id=&quot;markdown-toc-edgy&quot;&gt;edgy&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#misshapen&quot; id=&quot;markdown-toc-misshapen&quot;&gt;misshapen&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#bladder&quot; id=&quot;markdown-toc-bladder&quot;&gt;bladder&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#slur&quot; id=&quot;markdown-toc-slur&quot;&gt;slur&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#placate&quot; id=&quot;markdown-toc-placate&quot;&gt;placate&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#gouge&quot; id=&quot;markdown-toc-gouge&quot;&gt;gouge&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#repressive&quot; id=&quot;markdown-toc-repressive&quot;&gt;repressive&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#drift&quot; id=&quot;markdown-toc-drift&quot;&gt;drift&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#moron&quot; id=&quot;markdown-toc-moron&quot;&gt;moron&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#swipe&quot; id=&quot;markdown-toc-swipe&quot;&gt;swipe&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#traipse&quot; id=&quot;markdown-toc-traipse&quot;&gt;traipse&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#exemplary&quot; id=&quot;markdown-toc-exemplary&quot;&gt;exemplary&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#&quot; id=&quot;markdown-toc-&quot;&gt;—&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#unravel&quot; id=&quot;markdown-toc-unravel&quot;&gt;unravel&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#astrology&quot; id=&quot;markdown-toc-astrology&quot;&gt;astrology&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#blinkered--fettered&quot; id=&quot;markdown-toc-blinkered--fettered&quot;&gt;blinkered &amp;amp; fettered&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#pungent&quot; id=&quot;markdown-toc-pungent&quot;&gt;pungent&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#foolproof&quot; id=&quot;markdown-toc-foolproof&quot;&gt;foolproof&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#impassive&quot; id=&quot;markdown-toc-impassive&quot;&gt;impassive&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#squally&quot; id=&quot;markdown-toc-squally&quot;&gt;squally&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#lad&quot; id=&quot;markdown-toc-lad&quot;&gt;lad&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#killjoy&quot; id=&quot;markdown-toc-killjoy&quot;&gt;killjoy&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#otter&quot; id=&quot;markdown-toc-otter&quot;&gt;otter&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#gambol&quot; id=&quot;markdown-toc-gambol&quot;&gt;gambol&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#sham&quot; id=&quot;markdown-toc-sham&quot;&gt;sham&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#callous&quot; id=&quot;markdown-toc-callous&quot;&gt;callous&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#pustule&quot; id=&quot;markdown-toc-pustule&quot;&gt;pustule&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#unimpaired&quot; id=&quot;markdown-toc-unimpaired&quot;&gt;unimpaired&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#blatant&quot; id=&quot;markdown-toc-blatant&quot;&gt;blatant&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#corpulent&quot; id=&quot;markdown-toc-corpulent&quot;&gt;corpulent&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#manhandle&quot; id=&quot;markdown-toc-manhandle&quot;&gt;manhandle&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#ogle&quot; id=&quot;markdown-toc-ogle&quot;&gt;ogle&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#snag&quot; id=&quot;markdown-toc-snag&quot;&gt;snag&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#-1&quot; id=&quot;markdown-toc--1&quot;&gt;—&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#delusion&quot; id=&quot;markdown-toc-delusion&quot;&gt;delusion&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#wobble&quot; id=&quot;markdown-toc-wobble&quot;&gt;wobble&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#pandemonium&quot; id=&quot;markdown-toc-pandemonium&quot;&gt;pandemonium&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#soppy&quot; id=&quot;markdown-toc-soppy&quot;&gt;soppy&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#meadow&quot; id=&quot;markdown-toc-meadow&quot;&gt;meadow&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#quake&quot; id=&quot;markdown-toc-quake&quot;&gt;quake&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#exhale&quot; id=&quot;markdown-toc-exhale&quot;&gt;exhale&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#lank&quot; id=&quot;markdown-toc-lank&quot;&gt;lank&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#angular&quot; id=&quot;markdown-toc-angular&quot;&gt;angular&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#liaise&quot; id=&quot;markdown-toc-liaise&quot;&gt;liaise&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#frisk&quot; id=&quot;markdown-toc-frisk&quot;&gt;frisk&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;phrases&quot;&gt;Phrases&lt;/h1&gt;

&lt;h2 id=&quot;trestle-table&quot;&gt;trestle table&lt;/h2&gt;

&lt;p&gt;台桌，在台架上铺板的桌子&lt;/p&gt;

&lt;p&gt;… a long &lt;strong&gt;trestle table&lt;/strong&gt; in front of her laden with twigs.&lt;/p&gt;

&lt;h1 id=&quot;words&quot;&gt;Words&lt;/h1&gt;

&lt;h2 id=&quot;nutcase&quot;&gt;nutcase&lt;/h2&gt;

&lt;p&gt;疯子&lt;/p&gt;

&lt;p&gt;It’s just that before the truth could sink in, everyone went home for the summer, where they spent two months reading about how you’re a &lt;strong&gt;nutcase&lt;/strong&gt; and Dumbledore’s going &lt;strong&gt;senile&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;senile&quot;&gt;senile&lt;/h2&gt;

&lt;p&gt;年迈的&lt;/p&gt;

&lt;h2 id=&quot;retaliate&quot;&gt;retaliate&lt;/h2&gt;

&lt;p&gt;反击&lt;/p&gt;

&lt;p&gt;‘Don’t start arguing again,’ said Harry wearily, as Ron opened his mouth to &lt;strong&gt;retaliate&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;mallet&quot;&gt;mallet&lt;/h2&gt;

&lt;p&gt;木槌&lt;/p&gt;

&lt;p&gt;One by one, as though hit over the head with an invisible &lt;strong&gt;mallet&lt;/strong&gt;, the first-years were slumping unconscious in their seats.&lt;/p&gt;

&lt;h2 id=&quot;edgy&quot;&gt;edgy&lt;/h2&gt;

&lt;p&gt;急躁的，紧张不安的&lt;/p&gt;

&lt;p&gt;Hermione stared down at her blank piece of parchment for a few seconds, then said &lt;strong&gt;edgily&lt;/strong&gt;, ‘Oh, it’s no good, I can’t concentrate now. I’m going to bed.’&lt;/p&gt;

&lt;h2 id=&quot;misshapen&quot;&gt;misshapen&lt;/h2&gt;

&lt;p&gt;畸形的&lt;/p&gt;

&lt;p&gt;Hermione pulled out two &lt;strong&gt;misshapen&lt;/strong&gt; woolly objects, placed them carefully on a table by the fireplace.&lt;/p&gt;

&lt;h2 id=&quot;bladder&quot;&gt;bladder&lt;/h2&gt;

&lt;p&gt;囊，囊状物&lt;/p&gt;

&lt;p&gt;They didn’t look anything like hats to me, more like woolly &lt;strong&gt;bladders&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;slur&quot;&gt;slur&lt;/h2&gt;

&lt;p&gt;诋毁&lt;/p&gt;

&lt;p&gt;Still angry about Ron’s &lt;strong&gt;slur&lt;/strong&gt; on her woolly hats, Hermione did not join them.&lt;/p&gt;

&lt;h2 id=&quot;placate&quot;&gt;placate&lt;/h2&gt;

&lt;p&gt;安抚，抚慰&lt;/p&gt;

&lt;p&gt;It is wise to have a gift of woodlice ready to distract or &lt;strong&gt;placate&lt;/strong&gt; it.&lt;/p&gt;

&lt;h2 id=&quot;gouge&quot;&gt;gouge&lt;/h2&gt;

&lt;p&gt;戳，凿&lt;/p&gt;

&lt;p&gt;If angered they will try to &lt;strong&gt;gouge&lt;/strong&gt; at human eyes with their fingers.&lt;/p&gt;

&lt;h2 id=&quot;repressive&quot;&gt;repressive&lt;/h2&gt;

&lt;p&gt;抑制的，压抑的&lt;/p&gt;

&lt;p&gt;‘Never you mind,’ said Professor Grubbly-Plank &lt;strong&gt;repressively&lt;/strong&gt;, which had been her attitude last time Hagrid had failed to turn up for a class, too.&lt;/p&gt;

&lt;h2 id=&quot;drift&quot;&gt;drift&lt;/h2&gt;

&lt;p&gt;大致的意思&lt;/p&gt;

&lt;p&gt;Maybe he’s been messing with stuff that’s boo big for him, if you get my &lt;strong&gt;drift&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;My German isn’t very good, but I got the &lt;strong&gt;drift&lt;/strong&gt; of what she said.&lt;/p&gt;

&lt;h2 id=&quot;moron&quot;&gt;moron&lt;/h2&gt;

&lt;p&gt;蠢人，笨蛋&lt;/p&gt;

&lt;p&gt;So even if that overgrown &lt;strong&gt;moron&lt;/strong&gt; does show up again, he’ll probably be sent packing straightaway.&lt;/p&gt;

&lt;h2 id=&quot;swipe&quot;&gt;swipe&lt;/h2&gt;

&lt;p&gt;猛击&lt;/p&gt;

&lt;p&gt;Harry had gripped the Bowtruckle so hard that it had almost snapped, and it had just taken a great retaliatory &lt;strong&gt;swipe&lt;/strong&gt; at his hand with its sharp fingers.&lt;/p&gt;

&lt;h2 id=&quot;traipse&quot;&gt;traipse&lt;/h2&gt;

&lt;p&gt;疲惫地走，拖着脚走&lt;/p&gt;

&lt;p&gt;Together, they &lt;strong&gt;traipsed&lt;/strong&gt; across the vegetable patch.&lt;/p&gt;

&lt;h2 id=&quot;exemplary&quot;&gt;exemplary&lt;/h2&gt;

&lt;p&gt;模范的&lt;/p&gt;

&lt;p&gt;‘Because she’ll never be as good as Hagrid,’ said Harry firmly, fully aware that he had just experienced an &lt;strong&gt;exemplary&lt;/strong&gt; Care of Magic Creatures lesson and was thoroughly annoyed about it.&lt;/p&gt;

&lt;h1&gt;—&lt;/h1&gt;

&lt;h2 id=&quot;unravel&quot;&gt;unravel&lt;/h2&gt;

&lt;p&gt;解开&lt;/p&gt;

&lt;p&gt;Centaurs have &lt;strong&gt;unravelled&lt;/strong&gt; the mysteries of these movements over centuries.&lt;/p&gt;

&lt;h2 id=&quot;astrology&quot;&gt;astrology&lt;/h2&gt;

&lt;p&gt;占星术&lt;/p&gt;

&lt;p&gt;Professor Trelawney did &lt;strong&gt;astrology&lt;/strong&gt; with us!&lt;/p&gt;

&lt;h2 id=&quot;blinkered--fettered&quot;&gt;blinkered &amp;amp; fettered&lt;/h2&gt;

&lt;p&gt;blinkered：戴上眼罩的，蒙蔽双眼的
fettered：带上脚拷，受束缚的&lt;/p&gt;

&lt;p&gt;’- is a human,’ said Firenze simply. ‘And is therefore &lt;strong&gt;blinkered&lt;/strong&gt; and &lt;strong&gt;fettered&lt;/strong&gt; by the limitations of your kind.’&lt;/p&gt;

&lt;h2 id=&quot;pungent&quot;&gt;pungent&lt;/h2&gt;

&lt;p&gt;刺鼻的&lt;/p&gt;

&lt;p&gt;… and Firenze told them to look for certain shapes and symbols in the &lt;strong&gt;pungent&lt;/strong&gt; fumes.&lt;/p&gt;

&lt;h2 id=&quot;foolproof&quot;&gt;foolproof&lt;/h2&gt;

&lt;p&gt;错不了的，不会出毛病的&lt;/p&gt;

&lt;p&gt;His priority did not seem to be to teach them what he knew, but rather to impress upon them that nothing, not even centaurs’ knowlege, was &lt;strong&gt;foolproof&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;impassive&quot;&gt;impassive&lt;/h2&gt;

&lt;p&gt;面无表情的，平静的&lt;/p&gt;

&lt;p&gt;Firenze looked at Harry &lt;strong&gt;impassively&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;squally&quot;&gt;squally&lt;/h2&gt;

&lt;p&gt;有狂风的&lt;/p&gt;

&lt;h2 id=&quot;lad&quot;&gt;lad&lt;/h2&gt;

&lt;p&gt;小伙子，老弟&lt;/p&gt;

&lt;p&gt;Don’t worry ablout me, Harry, just get along now, there’s a good &lt;strong&gt;lad&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;killjoy&quot;&gt;killjoy&lt;/h2&gt;

&lt;p&gt;扫兴的人&lt;/p&gt;

&lt;p&gt;‘Oh, don’t be such a &lt;strong&gt;killjoy&lt;/strong&gt;,’ said Cho brightly.&lt;/p&gt;

&lt;h2 id=&quot;otter&quot;&gt;otter&lt;/h2&gt;

&lt;p&gt;水獭&lt;/p&gt;

&lt;h2 id=&quot;gambol&quot;&gt;gambol&lt;/h2&gt;

&lt;p&gt;蹦蹦跳跳，嬉戏&lt;/p&gt;

&lt;p&gt;Hermione’s Patronus, a shining silver otter, was &lt;strong&gt;gambolling&lt;/strong&gt; around her.&lt;/p&gt;

&lt;h2 id=&quot;sham&quot;&gt;sham&lt;/h2&gt;

&lt;p&gt;假装&lt;/p&gt;

&lt;p&gt;The portraits of old headmasters and headmistresses were not &lt;strong&gt;shamming&lt;/strong&gt; sleep tonight.&lt;/p&gt;

&lt;h2 id=&quot;callous&quot;&gt;callous&lt;/h2&gt;

&lt;p&gt;无感觉的，麻木的&lt;/p&gt;

&lt;p&gt;There was an indecent excitement in her voice, the same &lt;strong&gt;callous&lt;/strong&gt; pleasure Harry had heard as she watched professor Trelawney dissolving with misery in the Entrance Hall.&lt;/p&gt;

&lt;h2 id=&quot;pustule&quot;&gt;pustule&lt;/h2&gt;

&lt;p&gt;脓包&lt;/p&gt;

&lt;p&gt;… a series of closeset purple &lt;strong&gt;pustules&lt;/strong&gt; that had spread across her nose and cheeks to form the word ‘SNEAK’.&lt;/p&gt;

&lt;h2 id=&quot;unimpaired&quot;&gt;unimpaired&lt;/h2&gt;

&lt;p&gt;未受损害的，未减弱的&lt;/p&gt;

&lt;p&gt;He was heavily bandaged, it is true, but his hearing was quite &lt;strong&gt;unimpaired&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;blatant&quot;&gt;blatant&lt;/h2&gt;

&lt;p&gt;公然的，露骨的&lt;/p&gt;

&lt;p&gt;‘&lt;strong&gt;Blatant&lt;/strong&gt; corruption!’ roared the portrait of the &lt;strong&gt;corpulent&lt;/strong&gt;, red-nosed wizard on the wall.&lt;/p&gt;

&lt;h2 id=&quot;corpulent&quot;&gt;corpulent&lt;/h2&gt;

&lt;p&gt;肥胖的&lt;/p&gt;

&lt;h2 id=&quot;manhandle&quot;&gt;manhandle&lt;/h2&gt;

&lt;p&gt;粗暴地对待&lt;/p&gt;

&lt;p&gt;I cannot allow you to &lt;strong&gt;manhandle&lt;/strong&gt; my students, Dolores.&lt;/p&gt;

&lt;h2 id=&quot;ogle&quot;&gt;ogle&lt;/h2&gt;

&lt;p&gt;挑逗地看，抛媚眼&lt;/p&gt;

&lt;p&gt;‘Yes, shut up, Potter!’ barked Fudge, who was still &lt;strong&gt;ogling&lt;/strong&gt; Dumbledore with a kind of horrified delight.&lt;/p&gt;

&lt;h2 id=&quot;snag&quot;&gt;snag&lt;/h2&gt;

&lt;p&gt;障碍&lt;/p&gt;

&lt;p&gt;‘Ah,’ said Dumbledore gently,’yes, yes, I thought we might hit that little &lt;strong&gt;snag&lt;/strong&gt;.’&lt;/p&gt;

&lt;h1 id=&quot;-1&quot;&gt;—&lt;/h1&gt;

&lt;h2 id=&quot;delusion&quot;&gt;delusion&lt;/h2&gt;

&lt;p&gt;错觉&lt;/p&gt;

&lt;p&gt;…, you seem to be labouring under the &lt;strong&gt;delusion&lt;/strong&gt; that I am going to come quietly.&lt;/p&gt;

&lt;h2 id=&quot;wobble&quot;&gt;wobble&lt;/h2&gt;

&lt;p&gt;摇摆&lt;/p&gt;

&lt;p&gt;Filch’s jowls &lt;strong&gt;wobbled&lt;/strong&gt; with silent laughter.&lt;/p&gt;

&lt;h2 id=&quot;pandemonium&quot;&gt;pandemonium&lt;/h2&gt;

&lt;p&gt;大混乱&lt;/p&gt;

&lt;p&gt;One floor down, &lt;strong&gt;pandemonium&lt;/strong&gt; reigned.&lt;/p&gt;

&lt;h2 id=&quot;soppy&quot;&gt;soppy&lt;/h2&gt;

&lt;p&gt;多愁善感的&lt;/p&gt;

&lt;h2 id=&quot;meadow&quot;&gt;meadow&lt;/h2&gt;

&lt;p&gt;牧场，草地&lt;/p&gt;

&lt;p&gt;/medow/&lt;/p&gt;

&lt;p&gt;… a &lt;strong&gt;soppy-looking&lt;/strong&gt; witch in the middle of a &lt;strong&gt;meadow&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;quake&quot;&gt;quake&lt;/h2&gt;

&lt;p&gt;颤抖&lt;/p&gt;

&lt;p&gt;… listening to Umbridge and Filch’s yells and &lt;strong&gt;quaking&lt;/strong&gt; with suppressed mirth.&lt;/p&gt;

&lt;h2 id=&quot;exhale&quot;&gt;exhale&lt;/h2&gt;

&lt;p&gt;呼出，呼气&lt;/p&gt;

&lt;p&gt;/eks’heil/&lt;/p&gt;

&lt;p&gt;…(dragon) emitting loud bangs and &lt;strong&gt;exhaling&lt;/strong&gt; flame.&lt;/p&gt;

&lt;h2 id=&quot;lank&quot;&gt;lank&lt;/h2&gt;

&lt;p&gt;（头发）平直的&lt;/p&gt;

&lt;p&gt;His hair was &lt;strong&gt;lank&lt;/strong&gt; and greasy and was flopping on to the table.&lt;/p&gt;

&lt;h2 id=&quot;angular&quot;&gt;angular&lt;/h2&gt;

&lt;p&gt;瘦骨嶙峋的&lt;/p&gt;

&lt;p&gt;Round-shouldered yet &lt;strong&gt;angular&lt;/strong&gt;, he walked in a twitchy manner that recalled a spider.&lt;/p&gt;

&lt;h2 id=&quot;liaise&quot;&gt;liaise&lt;/h2&gt;

&lt;p&gt;建立联络关系&lt;/p&gt;

&lt;p&gt;You don’t seem to need many qualifications to &lt;strong&gt;liaise&lt;/strong&gt; with Muggles.&lt;/p&gt;

&lt;h2 id=&quot;frisk&quot;&gt;frisk&lt;/h2&gt;

&lt;p&gt;v. 搜身&lt;/p&gt;

&lt;p&gt;With Umbridge groping around in the fires and &lt;strong&gt;frisking&lt;/strong&gt; all the owls?&lt;/p&gt;

</description>
        <pubDate>Mon, 27 May 2019 00:00:00 +0000</pubDate>
        <link>http://viaductooor.github.io/blog/2019/05/27/Learn-English-Harry-Potter-V/</link>
        <guid isPermaLink="true">http://viaductooor.github.io/blog/2019/05/27/Learn-English-Harry-Potter-V/</guid>
        
        <category>english</category>
        
        
        <category>private</category>
        
      </item>
    
      <item>
        <title>Java虚拟机纲要</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#java内存模型&quot; id=&quot;markdown-toc-java内存模型&quot;&gt;Java内存模型&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#程序计数器&quot; id=&quot;markdown-toc-程序计数器&quot;&gt;程序计数器&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#java虚拟机栈&quot; id=&quot;markdown-toc-java虚拟机栈&quot;&gt;Java虚拟机栈&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#本地方法栈&quot; id=&quot;markdown-toc-本地方法栈&quot;&gt;本地方法栈&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#java堆&quot; id=&quot;markdown-toc-java堆&quot;&gt;Java堆&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#方法区&quot; id=&quot;markdown-toc-方法区&quot;&gt;方法区&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#垃圾回收机制&quot; id=&quot;markdown-toc-垃圾回收机制&quot;&gt;垃圾回收机制&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#判断对象需要回收的方法&quot; id=&quot;markdown-toc-判断对象需要回收的方法&quot;&gt;判断对象需要回收的方法&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#引用计数器法&quot; id=&quot;markdown-toc-引用计数器法&quot;&gt;引用计数器法&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#可达性分析算法&quot; id=&quot;markdown-toc-可达性分析算法&quot;&gt;可达性分析算法&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#引用的类型&quot; id=&quot;markdown-toc-引用的类型&quot;&gt;引用的类型&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#强引用&quot; id=&quot;markdown-toc-强引用&quot;&gt;强引用&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#软引用&quot; id=&quot;markdown-toc-软引用&quot;&gt;软引用&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#弱引用&quot; id=&quot;markdown-toc-弱引用&quot;&gt;弱引用&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#虚引用&quot; id=&quot;markdown-toc-虚引用&quot;&gt;虚引用&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#垃圾回收算法&quot; id=&quot;markdown-toc-垃圾回收算法&quot;&gt;垃圾回收算法&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#标记-清除算法&quot; id=&quot;markdown-toc-标记-清除算法&quot;&gt;标记-清除算法&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#复制算法&quot; id=&quot;markdown-toc-复制算法&quot;&gt;复制算法&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#标记-整理算法&quot; id=&quot;markdown-toc-标记-整理算法&quot;&gt;标记-整理算法&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#分代收集算法&quot; id=&quot;markdown-toc-分代收集算法&quot;&gt;分代收集算法&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#内存分配策略&quot; id=&quot;markdown-toc-内存分配策略&quot;&gt;内存分配策略&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#类加载机制&quot; id=&quot;markdown-toc-类加载机制&quot;&gt;类加载机制&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#类加载的时机&quot; id=&quot;markdown-toc-类加载的时机&quot;&gt;类加载的时机&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#类加载的过程&quot; id=&quot;markdown-toc-类加载的过程&quot;&gt;类加载的过程&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#加载&quot; id=&quot;markdown-toc-加载&quot;&gt;加载&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#验证&quot; id=&quot;markdown-toc-验证&quot;&gt;验证&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#准备&quot; id=&quot;markdown-toc-准备&quot;&gt;准备&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#解析&quot; id=&quot;markdown-toc-解析&quot;&gt;解析&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#初始化&quot; id=&quot;markdown-toc-初始化&quot;&gt;初始化&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#双亲委派模型&quot; id=&quot;markdown-toc-双亲委派模型&quot;&gt;双亲委派模型&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;java内存模型&quot;&gt;Java内存模型&lt;/h1&gt;

&lt;p&gt;##运行时数据区域&lt;/p&gt;

&lt;p&gt;运行时数据区域包含方法区、堆、虚拟机栈、本地方法栈以及程序计数器&lt;/p&gt;

&lt;h3 id=&quot;程序计数器&quot;&gt;程序计数器&lt;/h3&gt;

&lt;p&gt;每个线程有一个独立的程序计数器，用于保存当前程序的执行状态。字节码解释器工作时通过改变计数器的值来选取吓一跳需要执行的指令，分支、循环、跳转、异常处理、线程恢复等都依赖这个计数器。&lt;/p&gt;

&lt;p&gt;执行Native方法时程序计数器的值为空（Undefined）。&lt;/p&gt;

&lt;h3 id=&quot;java虚拟机栈&quot;&gt;Java虚拟机栈&lt;/h3&gt;

&lt;p&gt;同样是线程私有。虚拟机栈描述的是Java方法执行的内存模型，每个方法在执行的同时都会创建一个栈帧，用于存储局部变量表、操作数栈、动态链接、方法出口灯信息，每一个方法从调用直至执行完成的过程，就对应一个栈帧在虚拟机中入栈到出栈的过程。&lt;/p&gt;

&lt;h3 id=&quot;本地方法栈&quot;&gt;本地方法栈&lt;/h3&gt;

&lt;p&gt;与虚拟机栈类似，不过只为Native方法服务。&lt;/p&gt;

&lt;h3 id=&quot;java堆&quot;&gt;Java堆&lt;/h3&gt;

&lt;p&gt;是所有线程共享的一块内存区域，在虚拟机启动时创建。此内存区域的唯一目的就是存放对象实例，几乎所有的对象实例都在这里分配内存。&lt;/p&gt;

&lt;p&gt;Java堆是垃圾收集器管理的主要区域，因此也叫“GC堆”（Garbage Collector Heap）。&lt;/p&gt;

&lt;h3 id=&quot;方法区&quot;&gt;方法区&lt;/h3&gt;

&lt;p&gt;是所有线程共享的内存区域，用于存储已被虚拟机加载的类信息、常量、静态变量、即时编译器编译后的代码等数据。方法区也叫做Non-heap，和堆区区分开来。&lt;/p&gt;

&lt;p&gt;该区域的内存回收目标主要是针对常量池的回收和对类型的卸载。&lt;/p&gt;

&lt;p&gt;运行时常量池（Runtime Constant Pool）是方法区的一部分，用于存放编译期生成的各种字面量和符号引用。&lt;/p&gt;

&lt;h1 id=&quot;垃圾回收机制&quot;&gt;垃圾回收机制&lt;/h1&gt;

&lt;h2 id=&quot;判断对象需要回收的方法&quot;&gt;判断对象需要回收的方法&lt;/h2&gt;

&lt;h3 id=&quot;引用计数器法&quot;&gt;引用计数器法&lt;/h3&gt;

&lt;p&gt;给每一个对象分配一个计数器，记录有多少个地方引用该对象，当该计数器为0时即回收该对象。&lt;/p&gt;

&lt;p&gt;优点为实现简单，缺点为无法解决相互引用的问题。&lt;/p&gt;

&lt;h3 id=&quot;可达性分析算法&quot;&gt;可达性分析算法&lt;/h3&gt;

&lt;p&gt;主流编程语言中通用的方法。&lt;/p&gt;

&lt;p&gt;通过一系列的GC Roots作为起始点（根）向下搜索，搜索路径被称为引用链（Reference chain），当一个对象到GC Roots没有任何引用链相连接，则回收该对象。&lt;/p&gt;

&lt;p&gt;GC Roots包括：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;虚拟机栈（栈帧中本地变量表）中引用的对象&lt;/li&gt;
  &lt;li&gt;方法区中类静态属性引用的对象&lt;/li&gt;
  &lt;li&gt;方法区中常量引用的对象&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;引用的类型&quot;&gt;引用的类型&lt;/h2&gt;

&lt;p&gt;在JDK 1.2之后，Java对引用的概念进行了扩充，将引用分成了：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;强引用 Strong Reference&lt;/li&gt;
  &lt;li&gt;软引用 Soft Reference&lt;/li&gt;
  &lt;li&gt;弱引用 Weak Reference&lt;/li&gt;
  &lt;li&gt;虚引用 Phantom Reference&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;强引用&quot;&gt;强引用&lt;/h3&gt;

&lt;p&gt;类似&lt;code class=&quot;highlighter-rouge&quot;&gt;Object obj = new Object()&lt;/code&gt;，只要强引用还在，就永远不会被回收。&lt;/p&gt;

&lt;h3 id=&quot;软引用&quot;&gt;软引用&lt;/h3&gt;

&lt;p&gt;通过SoftReference类来使用软引用。在系统即将要发生内存溢出之前，会将只被软引用的对象回收。&lt;/p&gt;

&lt;h3 id=&quot;弱引用&quot;&gt;弱引用&lt;/h3&gt;

&lt;p&gt;通过WeakReference类来使用弱引用。在下一次垃圾回收的时候，一定会回收掉只被弱引用的对象。&lt;/p&gt;

&lt;h3 id=&quot;虚引用&quot;&gt;虚引用&lt;/h3&gt;

&lt;p&gt;通过PhantomReference类来使用虚引用。对于垃圾回收来说，虚引用等于没有引用，唯一目的就是在回收的时候会收到一个系统通知。&lt;/p&gt;

&lt;h2 id=&quot;垃圾回收算法&quot;&gt;垃圾回收算法&lt;/h2&gt;

&lt;h3 id=&quot;标记-清除算法&quot;&gt;标记-清除算法&lt;/h3&gt;

&lt;p&gt;最基础的垃圾收集算法，先标记出所有需要回收的对象，在标记完成后统一回收这些对象。&lt;/p&gt;

&lt;p&gt;这个算法的不足点有：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;效率低，无论是标记过程还是清除过程&lt;/li&gt;
  &lt;li&gt;空间利用率不高，因为标记清楚之后会产生大量不连续的内存碎片&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;复制算法&quot;&gt;复制算法&lt;/h3&gt;

&lt;p&gt;将可用内存分成容量相等的两块，先使用一块，这块用完了就把这一块上面仍然存活的对象拷贝到另一块（未使用的内存）上，同时一次性清理之前的使用过的内存。&lt;/p&gt;

&lt;p&gt;优点是效率比标记-清除算法高；缺点是只能使用一半的内存空间。&lt;/p&gt;

&lt;p&gt;现在的商业虚拟机都采用这种收集算法来回收新生代，但是并不是按照1：1来分配空间的，而是分成了一块较大的Eden空间和两块较小的Survivor空间，因为存活的对象一般比较少。每次使用Eden和一块Survivor，用完了将他们上面的存活对象拷贝到另一个Survivor上，清除这两块空间。&lt;/p&gt;

&lt;h3 id=&quot;标记-整理算法&quot;&gt;标记-整理算法&lt;/h3&gt;

&lt;p&gt;如果对象存活率较高的话，就不适合使用复制算法了。&lt;/p&gt;

&lt;p&gt;标记-整理算法和标记-清除算法类似，不过不是直接清除被标记的内存，而是先把存活的对象向内存一端移动（整理），然后清除除此之外的内存空间。&lt;/p&gt;

&lt;h3 id=&quot;分代收集算法&quot;&gt;分代收集算法&lt;/h3&gt;

&lt;p&gt;这并不是一个新的收集算法，而是根据不同内存的情况来分别处理。当前的商业虚拟机的垃圾收集都采用这一算法。&lt;/p&gt;

&lt;p&gt;一般是把Java堆分成新生代和老年代，在新生代中每次收集时都有大量对象死去，适合用复制算法；老年代中存活率较高，使用标记-清除算法或者标记-整理算法。&lt;/p&gt;

&lt;h1 id=&quot;内存分配策略&quot;&gt;内存分配策略&lt;/h1&gt;

&lt;p&gt;GC分为新生代GC和老年代GC。&lt;/p&gt;

&lt;p&gt;新生代GC（Minor GC）是发生在新生代的垃圾收集，因为大多数Java对象都具备朝生夕灭的特性，所以Minor GC非常频繁，一般回收速度也比较快。&lt;/p&gt;

&lt;p&gt;老年代GC（Major GC/Full GC），发生在老年代，速度一般比Minor GC慢10倍以上。&lt;/p&gt;

&lt;p&gt;常用的内存分配策略有：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;对象优先在Eden分配&lt;/li&gt;
  &lt;li&gt;大对象直接进入老年代，比如长字符串以及数组&lt;/li&gt;
  &lt;li&gt;长期存活的对象进入老年代。每一次Minor GC都会记录和修改存活对象的年龄，年龄足够大（可以通过MaxTenuringThreshold设置）的对象就会进入老年代。&lt;/li&gt;
  &lt;li&gt;动态对象年龄判定，如果在Survivor空间中相同年龄所有对象大小的总和大于Survivor空间的一半，年龄大于或等于该年龄的对象就可以直接进入老年代，无须等到MaxTenuringThreshold中要求的年龄。&lt;/li&gt;
  &lt;li&gt;空间分配担保。&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;类加载机制&quot;&gt;类加载机制&lt;/h1&gt;

&lt;p&gt;类从加载到卸载经过的生命周期包括：加载、验证、准备、解析、初始化、使用和卸载。验证、准备和解析统称为连接。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/blog/assets/class_loading.png&quot; alt=&quot;Process of Class Loading&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;类加载的时机&quot;&gt;类加载的时机&lt;/h2&gt;

&lt;p&gt;开始类加载过程第一个阶段“加载”的时机有：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;使用new实例化对象的时候、读取或设置一个类的静态字段的时候，以及调用一个类的静态方法的时候；&lt;/li&gt;
  &lt;li&gt;使用java.lang.reflect包的方法对类进行反射调用的时候，如果类没有初始化，则先要触发其初始化；&lt;/li&gt;
  &lt;li&gt;当初始化一个类的时候，如果父类没有初始化，则先初始化父类；&lt;/li&gt;
  &lt;li&gt;虚拟机启动时，需要指定一个包含main()方法的主类，虚拟机会先初始化这个主类；&lt;/li&gt;
  &lt;li&gt;如果一个java.lang.invoke.MethodHandler实例最后的解析结果REF_getStatic、REF_putStatic、REF_invokeStatic的方法句柄，并且这个方法句柄所对应的类没有进行过初始化，则先触发其初始化。（？）&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;fenixsoft&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;classloading&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt;/***被动使用类字段演示一：*通过子类引用父类的静态字段，不会导致子类初始化**/&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SuperClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;SuperClassinit!&quot;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;123&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SubClass&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SuperClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;SubClassinit!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ConstClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;ConstClass init!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HELLOWORLD&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;hello world&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;NotInitialization&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SubClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//1&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;SuperClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sca&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SuperClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//2&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ConstClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;HELLOWORLD&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//3&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;假设上面的函数main()中的语句每次只执行其中一条。&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;只会输出“SuperClass init!”，而不会输出“SubClass init!”，因为对于静态字段，只有直接定义这个字段的类才会被初始化。&lt;code class=&quot;highlighter-rouge&quot;&gt;SubClass.value&lt;/code&gt;中调用的value是父类中定义的，所以按照情况1会初始化SuperClass。&lt;/li&gt;
  &lt;li&gt;不会触发SuperClass的初始化，因为通过数组定义来引用类，不会触发此类的初始化。&lt;/li&gt;
  &lt;li&gt;不会触发ConstClass的初始化，因为经过传播优化，常量HELLOWORLD的值在编译阶段就存储到了NotInitialization类出常量池中。所有对ConstClass.HELLOWORLD的引用实际都转化成了NotInitialization自身常量池的引用。&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;类加载的过程&quot;&gt;类加载的过程&lt;/h2&gt;

&lt;h3 id=&quot;加载&quot;&gt;加载&lt;/h3&gt;

&lt;p&gt;加载阶段要完成的工作为：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;通过一个类的全限定名来获取定义此类的二进制字节流；&lt;/li&gt;
  &lt;li&gt;将这个字节流所代表的静态存储结构转化为方法去的运行时数据；&lt;/li&gt;
  &lt;li&gt;在内存中生成一个代表这个类的java.lang.Class对象，作为方法区这个类的各种数据访问入口。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;数组类本身不通过类加载器创建，而是有Java虚拟机直接创建。但是数组类中的Element Type最终是要用类加载器创建的。&lt;/p&gt;

&lt;h3 id=&quot;验证&quot;&gt;验证&lt;/h3&gt;

&lt;p&gt;验证的目的是确保Class文件的字节流中包含的信息符合当前虚拟机的要求，并且不会危害虚拟机自身的安全。&lt;/p&gt;

&lt;p&gt;大致上，验证包含4个检验步骤：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;文件格式验证&lt;/li&gt;
  &lt;li&gt;元数据验证：对字节码描述的信息进行语义分析，保证不存在不符合Java语言规范的元数据信息（主要是数据类型信息）。&lt;/li&gt;
  &lt;li&gt;字节码验证：通过数据流和控制流分析，确定程序语义是合法的、符合逻辑的。主要是对方法体进行校验。&lt;/li&gt;
  &lt;li&gt;符号引用验证：确保解析动作能正常执行，如果无法通过符号引用验证，可能会抛出NoSuchFieldError、NoSuchMethodError等&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;准备&quot;&gt;准备&lt;/h3&gt;

&lt;p&gt;正式为变量（类变量，即static变量）分配内存并设置变量初始值，这些变量所使用的内存都将在方法区中进行分配。&lt;/p&gt;

&lt;p&gt;注意：准备阶段只是赋初始值，而不是定义的值，比如&lt;code class=&quot;highlighter-rouge&quot;&gt;public static int value = 123;&lt;/code&gt;在准备过后value的值是0而不是123，不同类型的初始值为：&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;数据类型&lt;/th&gt;
      &lt;th&gt;零值&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;int&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;long&lt;/td&gt;
      &lt;td&gt;0L&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;short&lt;/td&gt;
      &lt;td&gt;(short)0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;char&lt;/td&gt;
      &lt;td&gt;‘\u0000’&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;byte&lt;/td&gt;
      &lt;td&gt;(byte)0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;boolean&lt;/td&gt;
      &lt;td&gt;false&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;float&lt;/td&gt;
      &lt;td&gt;0.0f&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;double&lt;/td&gt;
      &lt;td&gt;0.0d&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;reference&lt;/td&gt;
      &lt;td&gt;null&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;解析&quot;&gt;解析&lt;/h3&gt;

&lt;p&gt;虚拟机将常量池内的符号引用替换为直接引用&lt;/p&gt;

&lt;h3 id=&quot;初始化&quot;&gt;初始化&lt;/h3&gt;

&lt;p&gt;根据程序员通过程序制定的主观计划去初始化类变量和其他资源，或者可以说，初始化阶段是执行类构造器&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clinit&amp;gt;()&lt;/code&gt;方法的过程。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clinit&amp;gt;()&lt;/code&gt;是由编译器自动收集类中的所有类变量的赋值动作和静态语句块(static{})中的语句合并产生的。静态语句块只能访问到定义在静态语句块之前的变量，定义在它之后的变量，在前面的静态语句块可以赋值，但是不能访问。&lt;/p&gt;

&lt;h2 id=&quot;双亲委派模型&quot;&gt;双亲委派模型&lt;/h2&gt;

</description>
        <pubDate>Sun, 26 May 2019 00:00:00 +0000</pubDate>
        <link>http://viaductooor.github.io/blog/2019/05/26/JVM/</link>
        <guid isPermaLink="true">http://viaductooor.github.io/blog/2019/05/26/JVM/</guid>
        
        <category>java</category>
        
        
        <category>java</category>
        
      </item>
    
      <item>
        <title>Wordlist - Harry Potter IV</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#phrases&quot; id=&quot;markdown-toc-phrases&quot;&gt;Phrases&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#an-assortment-of&quot; id=&quot;markdown-toc-an-assortment-of&quot;&gt;an assortment of&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#words&quot; id=&quot;markdown-toc-words&quot;&gt;Words&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#contagious--germ&quot; id=&quot;markdown-toc-contagious--germ&quot;&gt;contagious &amp;amp; germ&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#cactus&quot; id=&quot;markdown-toc-cactus&quot;&gt;cactus&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#bummer&quot; id=&quot;markdown-toc-bummer&quot;&gt;bummer&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#raccoon&quot; id=&quot;markdown-toc-raccoon&quot;&gt;raccoon&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#collude&quot; id=&quot;markdown-toc-collude&quot;&gt;collude&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#oppression&quot; id=&quot;markdown-toc-oppression&quot;&gt;oppression&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#gloss&quot; id=&quot;markdown-toc-gloss&quot;&gt;gloss&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#vociferous&quot; id=&quot;markdown-toc-vociferous&quot;&gt;vociferous&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#badger&quot; id=&quot;markdown-toc-badger&quot;&gt;badger&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#rind&quot; id=&quot;markdown-toc-rind&quot;&gt;rind&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#plait&quot; id=&quot;markdown-toc-plait&quot;&gt;plait&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#satin&quot; id=&quot;markdown-toc-satin&quot;&gt;satin&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#opal&quot; id=&quot;markdown-toc-opal&quot;&gt;opal&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#steed&quot; id=&quot;markdown-toc-steed&quot;&gt;steed&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#mast&quot; id=&quot;markdown-toc-mast&quot;&gt;mast&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#resurrect&quot; id=&quot;markdown-toc-resurrect&quot;&gt;resurrect&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#disembarking&quot; id=&quot;markdown-toc-disembarking&quot;&gt;disembarking&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#sleek&quot; id=&quot;markdown-toc-sleek&quot;&gt;sleek&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#unctuous&quot; id=&quot;markdown-toc-unctuous&quot;&gt;unctuous&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#&quot; id=&quot;markdown-toc-&quot;&gt;—&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#crane&quot; id=&quot;markdown-toc-crane&quot;&gt;crane&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#imminent&quot; id=&quot;markdown-toc-imminent&quot;&gt;imminent&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#crest&quot; id=&quot;markdown-toc-crest&quot;&gt;crest&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#interrogate&quot; id=&quot;markdown-toc-interrogate&quot;&gt;interrogate&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#lousy&quot; id=&quot;markdown-toc-lousy&quot;&gt;lousy&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#reverie&quot; id=&quot;markdown-toc-reverie&quot;&gt;reverie&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#plastered&quot; id=&quot;markdown-toc-plastered&quot;&gt;plastered&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#lethal&quot; id=&quot;markdown-toc-lethal&quot;&gt;lethal&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#antenna&quot; id=&quot;markdown-toc-antenna&quot;&gt;antenna&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#harness&quot; id=&quot;markdown-toc-harness&quot;&gt;harness&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#enmity&quot; id=&quot;markdown-toc-enmity&quot;&gt;enmity&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#paranoid&quot; id=&quot;markdown-toc-paranoid&quot;&gt;paranoid&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#appal&quot; id=&quot;markdown-toc-appal&quot;&gt;appal&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#crude&quot; id=&quot;markdown-toc-crude&quot;&gt;crude&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#benign&quot; id=&quot;markdown-toc-benign&quot;&gt;benign&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#secretion&quot; id=&quot;markdown-toc-secretion&quot;&gt;secretion&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#riddle&quot; id=&quot;markdown-toc-riddle&quot;&gt;riddle&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#foe&quot; id=&quot;markdown-toc-foe&quot;&gt;foe&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#prompt&quot; id=&quot;markdown-toc-prompt&quot;&gt;prompt&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#intact&quot; id=&quot;markdown-toc-intact&quot;&gt;intact&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#headstone&quot; id=&quot;markdown-toc-headstone&quot;&gt;headstone&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#-1&quot; id=&quot;markdown-toc--1&quot;&gt;—&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#immortality&quot; id=&quot;markdown-toc-immortality&quot;&gt;immortality&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#affinity&quot; id=&quot;markdown-toc-affinity&quot;&gt;affinity&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#wasp&quot; id=&quot;markdown-toc-wasp&quot;&gt;wasp&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#sear&quot; id=&quot;markdown-toc-sear&quot;&gt;sear&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;phrases&quot;&gt;Phrases&lt;/h1&gt;

&lt;h2 id=&quot;an-assortment-of&quot;&gt;an assortment of&lt;/h2&gt;

&lt;p&gt;各种各样的&lt;/p&gt;

&lt;h1 id=&quot;words&quot;&gt;Words&lt;/h1&gt;

&lt;h2 id=&quot;contagious--germ&quot;&gt;contagious &amp;amp; germ&lt;/h2&gt;

&lt;p&gt;contagious 蔓延的，有传染性的&lt;/p&gt;

&lt;p&gt;germ 病菌&lt;/p&gt;

&lt;p&gt;Rumours were flying from student to student like highly &lt;strong&gt;contagious&lt;/strong&gt; &lt;strong&gt;germs&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;cactus&quot;&gt;cactus&lt;/h2&gt;

&lt;p&gt;仙人掌&lt;/p&gt;

&lt;p&gt;… Professor McGonagall barked at the end of one particularly difficult lesson, during which Neville had accidentally transplanted his own ears onto a &lt;strong&gt;cactus&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;bummer&quot;&gt;bummer&lt;/h2&gt;

&lt;p&gt;扫兴，失望&lt;/p&gt;

&lt;p&gt;What a &lt;strong&gt;bummer&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;It’s a &lt;strong&gt;bummer&lt;/strong&gt; all right, but if he won’t talk to us in person, we’ll have to send him the letter after all. Or we’ll stuff it into his hand, he can’t avoid us for ever.&lt;/p&gt;

&lt;h2 id=&quot;raccoon&quot;&gt;raccoon&lt;/h2&gt;

&lt;p&gt;浣熊&lt;/p&gt;

&lt;p&gt;‘I asked McGonagall how the champions are chosen but she wasn’t telling,’ said George bitterly.’She just told me to shut up and get on with Transfiguring my &lt;strong&gt;raccoon&lt;/strong&gt;.’&lt;/p&gt;

&lt;h2 id=&quot;collude&quot;&gt;collude&lt;/h2&gt;

&lt;p&gt;勾结，串通&lt;/p&gt;

&lt;p&gt;‘House-elves!’ said Hermione loudly and proving Harry right. ‘Not once, in over a thousand pages, does Hogwarts: A History mention that we are all &lt;strong&gt;colluding&lt;/strong&gt; in the &lt;strong&gt;oppression&lt;/strong&gt; of a hundred slaves!’&lt;/p&gt;

&lt;h2 id=&quot;oppression&quot;&gt;oppression&lt;/h2&gt;

&lt;p&gt;压迫&lt;/p&gt;

&lt;h2 id=&quot;gloss&quot;&gt;gloss&lt;/h2&gt;

&lt;p&gt;n. 光彩，假象
v. 使光彩，掩盖&lt;/p&gt;

&lt;p&gt;A Highly Biased and Selective History of Hogwarts, Which &lt;strong&gt;Glosses&lt;/strong&gt; Over the Nastier Aspects of the School.&lt;/p&gt;

&lt;h2 id=&quot;vociferous&quot;&gt;vociferous&lt;/h2&gt;

&lt;p&gt;吵闹的，叫嚣的&lt;/p&gt;

&lt;p&gt;They seemed to have made Hermione more &lt;strong&gt;vociferous&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;badger&quot;&gt;badger&lt;/h2&gt;

&lt;p&gt;n. 獾
v. 纠缠，烦扰&lt;/p&gt;

&lt;p&gt;She had been &lt;strong&gt;badgering&lt;/strong&gt; Harry and Ron ever since.&lt;/p&gt;

&lt;h2 id=&quot;rind&quot;&gt;rind&lt;/h2&gt;

&lt;p&gt;（橘子等水果、干酪、熏肉等的）皮&lt;/p&gt;

&lt;p&gt;Harry pulled off Sirius’s reply and offered Hedwig his bacon &lt;strong&gt;rinds&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;plait&quot;&gt;plait&lt;/h2&gt;

&lt;p&gt;辫子&lt;/p&gt;

&lt;p&gt;Parvati scowled and removed a large ornamental butterfly from the end of her &lt;strong&gt;plait&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;satin&quot;&gt;satin&lt;/h2&gt;

&lt;p&gt;纺缎子&lt;/p&gt;

&lt;p&gt;She was dressed from head to foot in black &lt;strong&gt;satin&lt;/strong&gt;, and many magnificent &lt;strong&gt;opals&lt;/strong&gt; gleamed at her throat and on her thick fingers.&lt;/p&gt;

&lt;h2 id=&quot;opal&quot;&gt;opal&lt;/h2&gt;

&lt;p&gt;蛋白石&lt;/p&gt;

&lt;h2 id=&quot;steed&quot;&gt;steed&lt;/h2&gt;

&lt;p&gt;骏马&lt;/p&gt;

&lt;p&gt;My &lt;strong&gt;steeds&lt;/strong&gt; require-er-forceful ‘andling.&lt;/p&gt;

&lt;h2 id=&quot;mast&quot;&gt;mast&lt;/h2&gt;

&lt;p&gt;桅杆&lt;/p&gt;

&lt;p&gt;‘It’s a &lt;strong&gt;mast&lt;/strong&gt;!’ he said to Ron and Hermione.&lt;/p&gt;

&lt;h2 id=&quot;resurrect&quot;&gt;resurrect&lt;/h2&gt;

&lt;p&gt;使复活，使再现&lt;/p&gt;

&lt;p&gt;… as though it was &lt;strong&gt;resurrected&lt;/strong&gt; wreck.&lt;/p&gt;

&lt;h2 id=&quot;disembarking&quot;&gt;disembarking&lt;/h2&gt;

&lt;p&gt;着陆&lt;/p&gt;

&lt;p&gt;People were &lt;strong&gt;disembarking&lt;/strong&gt;; they could see their silhouettes passing the lights in the ship’s portholes.&lt;/p&gt;

&lt;h2 id=&quot;sleek&quot;&gt;sleek&lt;/h2&gt;

&lt;p&gt;油亮的&lt;/p&gt;

&lt;p&gt;But the man who was leading them up to the castle was wearing furs of a different sort; &lt;strong&gt;sleek&lt;/strong&gt; and silver, like his hair.&lt;/p&gt;

&lt;h2 id=&quot;unctuous&quot;&gt;unctuous&lt;/h2&gt;

&lt;p&gt;油腻的，虚情假意的&lt;/p&gt;

&lt;p&gt;Karkaroff had a fruity, &lt;strong&gt;unctuous&lt;/strong&gt; voice.&lt;/p&gt;

&lt;h1&gt;—&lt;/h1&gt;

&lt;h2 id=&quot;crane&quot;&gt;crane&lt;/h2&gt;

&lt;p&gt;crane one’s neck 伸长脖子&lt;/p&gt;

&lt;p&gt;Like everyone else in the Hall, judging by the constantly &lt;strong&gt;craning&lt;/strong&gt; necks, the impatient expressions on every face, …&lt;/p&gt;

&lt;h2 id=&quot;imminent&quot;&gt;imminent&lt;/h2&gt;

&lt;p&gt;迫近的，即将到来的&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;imminent&lt;/strong&gt; arrival at their house of an assortment of wizards was making the Dursleys uptight and irritable.&lt;/p&gt;

&lt;h2 id=&quot;crest&quot;&gt;crest&lt;/h2&gt;

&lt;p&gt;（家族）饰章&lt;/p&gt;

&lt;p&gt;They were all wearing the same uniform; a tea-towel stamped with the Hogwarts &lt;strong&gt;crest&lt;/strong&gt;…&lt;/p&gt;

&lt;h2 id=&quot;interrogate&quot;&gt;interrogate&lt;/h2&gt;

&lt;p&gt;审问，质问&lt;/p&gt;

&lt;p&gt;His voice, which had been so calm throughout the &lt;strong&gt;interrogation&lt;/strong&gt; of Barty Crouch, shook very slightly for the first time.&lt;/p&gt;

&lt;h2 id=&quot;lousy&quot;&gt;lousy&lt;/h2&gt;

&lt;p&gt;令人厌恶的，糟糕的&lt;/p&gt;

&lt;p&gt;You &lt;strong&gt;lousy&lt;/strong&gt; biased scumbag, you gave Krum ten!&lt;/p&gt;

&lt;h2 id=&quot;reverie&quot;&gt;reverie&lt;/h2&gt;

&lt;p&gt;幻想，遐想&lt;/p&gt;

&lt;p&gt;Mr Crouch seemed to come out of a deep &lt;strong&gt;reverie&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;plastered&quot;&gt;plastered&lt;/h2&gt;

&lt;p&gt;用灰泥抹&lt;/p&gt;

&lt;p&gt;In Professor Lockhart’s day, the walls had been &lt;strong&gt;plastered&lt;/strong&gt; with beaming, winking pictures of Professor Lockhart himself.&lt;/p&gt;

&lt;h2 id=&quot;lethal&quot;&gt;lethal&lt;/h2&gt;

&lt;p&gt;致命的&lt;/p&gt;

&lt;p&gt;Hagrid simply loved monstrous creatures – the more &lt;strong&gt;lethal&lt;/strong&gt;, the better.&lt;/p&gt;

&lt;h2 id=&quot;antenna&quot;&gt;antenna&lt;/h2&gt;

&lt;p&gt;触须&lt;/p&gt;

&lt;p&gt;Look very closely, and you’ll notice the markings around her &lt;strong&gt;antennae&lt;/strong&gt; are exactly like those foul glasses she wears.&lt;/p&gt;

&lt;h2 id=&quot;harness&quot;&gt;harness&lt;/h2&gt;

&lt;p&gt;缰绳&lt;/p&gt;

&lt;p&gt;Harry could see Hagrid helping Madame Maxime to back two of the giant horses into their &lt;strong&gt;harness&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;enmity&quot;&gt;enmity&lt;/h2&gt;

&lt;p&gt;敌意，仇恨&lt;/p&gt;

&lt;p&gt;Lord Voldemort’s gift for spreading discord and &lt;strong&gt;enmity&lt;/strong&gt; is very great.&lt;/p&gt;

&lt;h2 id=&quot;paranoid&quot;&gt;paranoid&lt;/h2&gt;

&lt;p&gt;偏执的，多疑的&lt;/p&gt;

&lt;p&gt;‘Talk about &lt;strong&gt;paranoid&lt;/strong&gt; …’ Ron glanced nervously over his shoulder to check that Moody was definitely out of earshot, and went on, …&lt;/p&gt;

&lt;h2 id=&quot;appal&quot;&gt;appal&lt;/h2&gt;

&lt;p&gt;使惊吓&lt;/p&gt;

&lt;p&gt;They were mouthing soundlessly at Dumbledore, apparently too &lt;strong&gt;appalled&lt;/strong&gt; to speak.&lt;/p&gt;

&lt;h2 id=&quot;crude&quot;&gt;crude&lt;/h2&gt;

&lt;p&gt;粗糙的，天然的，未加工的&lt;/p&gt;

&lt;p&gt;a cluster of &lt;strong&gt;crude&lt;/strong&gt; stones&lt;/p&gt;

&lt;h2 id=&quot;benign&quot;&gt;benign&lt;/h2&gt;

&lt;p&gt;善良的，温和的；良性的&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;benign&lt;/strong&gt; tumour&lt;/p&gt;

&lt;p&gt;There was no &lt;strong&gt;benign&lt;/strong&gt; smile upon Dumbledore’s face, no twinkle in the eyes behind the spectacles.&lt;/p&gt;

&lt;h2 id=&quot;secretion&quot;&gt;secretion&lt;/h2&gt;

&lt;p&gt;分泌物&lt;/p&gt;

&lt;p&gt;He could see some sort of thick, gluey &lt;strong&gt;secretion&lt;/strong&gt; from the spider’s pincers on his tore robes.&lt;/p&gt;

&lt;h2 id=&quot;riddle&quot;&gt;riddle&lt;/h2&gt;

&lt;p&gt;谜语&lt;/p&gt;

&lt;p&gt;Not unless you can answer my &lt;strong&gt;riddle&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;foe&quot;&gt;foe&lt;/h2&gt;

&lt;p&gt;敌人&lt;/p&gt;

&lt;p&gt;But the blood of a &lt;strong&gt;foe&lt;/strong&gt; … Wormtail would have had me use any wizard, would you not, Wormtail?&lt;/p&gt;

&lt;h2 id=&quot;prompt&quot;&gt;prompt&lt;/h2&gt;

&lt;p&gt;迅速的，立刻的&lt;/p&gt;

&lt;p&gt;I see you all, whole and healthy, with your powers &lt;strong&gt;intact&lt;/strong&gt; - such &lt;strong&gt;prompt&lt;/strong&gt; appearances! - and I ask myself … why did this band of wizards never come to (find me when I was faintest?)&lt;/p&gt;

&lt;h2 id=&quot;intact&quot;&gt;intact&lt;/h2&gt;

&lt;p&gt;完整的&lt;/p&gt;

&lt;h2 id=&quot;headstone&quot;&gt;headstone&lt;/h2&gt;

&lt;p&gt;墓碑&lt;/p&gt;

&lt;p&gt;It stopped beside a towering marble &lt;strong&gt;headstone&lt;/strong&gt; only six feet from them.&lt;/p&gt;

&lt;h1 id=&quot;-1&quot;&gt;—&lt;/h1&gt;

&lt;h2 id=&quot;immortality&quot;&gt;immortality&lt;/h2&gt;

&lt;p&gt;不朽&lt;/p&gt;

&lt;p&gt;What I was, even I do not know … I, who have gone further than anybody along the path that leads to &lt;strong&gt;immortality&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;affinity&quot;&gt;affinity&lt;/h2&gt;

&lt;p&gt;喜爱&lt;/p&gt;

&lt;p&gt;Wormtail has a curious &lt;strong&gt;affinity&lt;/strong&gt; with rats, do you not, Wormtail?&lt;/p&gt;

&lt;h2 id=&quot;wasp&quot;&gt;wasp&lt;/h2&gt;

&lt;p&gt;黄蜂&lt;/p&gt;

&lt;p&gt;‘Good training for when we’re all Aurors,’ said Ron excitedly, attempting the impedient Jinx on a &lt;strong&gt;wasp&lt;/strong&gt; that had buzzed into the room.&lt;/p&gt;

&lt;h2 id=&quot;sear&quot;&gt;sear&lt;/h2&gt;

&lt;p&gt;灼烧&lt;/p&gt;

&lt;p&gt;Wormtail screamed, screamed as though every nerve in his body was on fire, the screaming filled Harry’s ears as the scar on his forehead &lt;strong&gt;seared&lt;/strong&gt; with pain.&lt;/p&gt;

</description>
        <pubDate>Tue, 07 May 2019 00:00:00 +0000</pubDate>
        <link>http://viaductooor.github.io/blog/2019/05/07/Learn-English-Harry-Potter-IV/</link>
        <guid isPermaLink="true">http://viaductooor.github.io/blog/2019/05/07/Learn-English-Harry-Potter-IV/</guid>
        
        <category>english</category>
        
        
        <category>private</category>
        
      </item>
    
      <item>
        <title>Wordlist - Harry Potter III</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#phrases&quot; id=&quot;markdown-toc-phrases&quot;&gt;Phrases&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#words&quot; id=&quot;markdown-toc-words&quot;&gt;Words&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#attic&quot; id=&quot;markdown-toc-attic&quot;&gt;attic&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#grotesque&quot; id=&quot;markdown-toc-grotesque&quot;&gt;grotesque&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#gag&quot; id=&quot;markdown-toc-gag&quot;&gt;gag&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#whine&quot; id=&quot;markdown-toc-whine&quot;&gt;whine&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#kneel&quot; id=&quot;markdown-toc-kneel&quot;&gt;kneel&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#ponder&quot; id=&quot;markdown-toc-ponder&quot;&gt;ponder&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#bluff&quot; id=&quot;markdown-toc-bluff&quot;&gt;bluff&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#fathomless&quot; id=&quot;markdown-toc-fathomless&quot;&gt;fathomless&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#caption&quot; id=&quot;markdown-toc-caption&quot;&gt;caption&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#fledged&quot; id=&quot;markdown-toc-fledged&quot;&gt;fledged&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#wane&quot; id=&quot;markdown-toc-wane&quot;&gt;wane&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#-haunted&quot; id=&quot;markdown-toc--haunted&quot;&gt;★ haunted&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#-derisive&quot; id=&quot;markdown-toc--derisive&quot;&gt;★ derisive&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#surly&quot; id=&quot;markdown-toc-surly&quot;&gt;surly&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#convulsive&quot; id=&quot;markdown-toc-convulsive&quot;&gt;convulsive&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#trance&quot; id=&quot;markdown-toc-trance&quot;&gt;trance&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#&quot; id=&quot;markdown-toc-&quot;&gt;—&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;phrases&quot;&gt;Phrases&lt;/h1&gt;

&lt;h1 id=&quot;words&quot;&gt;Words&lt;/h1&gt;

&lt;h2 id=&quot;attic&quot;&gt;attic&lt;/h2&gt;

&lt;p&gt;阁楼，顶楼&lt;/p&gt;

&lt;p&gt;In fact, it didn’t look like a classroom at all; more like a cross between someone’s &lt;strong&gt;attic&lt;/strong&gt; and an old-fashioned teashop.&lt;/p&gt;

&lt;h2 id=&quot;grotesque&quot;&gt;grotesque&lt;/h2&gt;

&lt;p&gt;古怪的，荒谬的&lt;/p&gt;

&lt;p&gt;He(Snape) was pulled into a standing position, head still lolling unpleasantly, like a &lt;strong&gt;grotesque&lt;/strong&gt; puppet.&lt;/p&gt;

&lt;h2 id=&quot;gag&quot;&gt;gag&lt;/h2&gt;

&lt;p&gt;阻塞，塞物于（口中）&lt;/p&gt;

&lt;p&gt;Thin cords shot from Lupin’s wand this time, and next moment, Pettigrew was wriggling on the floor, bound and &lt;strong&gt;gagged&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;whine&quot;&gt;whine&lt;/h2&gt;

&lt;p&gt;啜泣，哀诉&lt;/p&gt;

&lt;p&gt;Stop whining, child!&lt;/p&gt;

&lt;p&gt;… &lt;strong&gt;whined&lt;/strong&gt; Pettigrew.&lt;/p&gt;

&lt;h2 id=&quot;kneel&quot;&gt;kneel&lt;/h2&gt;

&lt;p&gt;跪下，跪着&lt;/p&gt;

&lt;p&gt;kneel-knelt-knelt&lt;/p&gt;

&lt;p&gt;Pettigrew &lt;strong&gt;knelt&lt;/strong&gt;, trembling uncontrollably, and turned his head slowly towards Harry.&lt;/p&gt;

&lt;h2 id=&quot;ponder&quot;&gt;ponder&lt;/h2&gt;

&lt;p&gt;仔细思考，沉思&lt;/p&gt;

&lt;p&gt;He seemed to be &lt;strong&gt;pondering&lt;/strong&gt; his answer.&lt;/p&gt;

&lt;h2 id=&quot;bluff&quot;&gt;bluff&lt;/h2&gt;

&lt;p&gt;欺骗，吓唬&lt;/p&gt;

&lt;p&gt;I thought it was the perfect plan … a &lt;strong&gt;bluff&lt;/strong&gt; … Voldemort would be sure to come after me, would never dream they’d use a weak, talentless thing like Pettigrew.&lt;/p&gt;

&lt;h2 id=&quot;fathomless&quot;&gt;fathomless&lt;/h2&gt;

&lt;p&gt;深不可测的&lt;/p&gt;

&lt;p&gt;Black’s face looked more skull-like than ever as he stared at Pettigrew with his &lt;strong&gt;fathomless&lt;/strong&gt; eyes.&lt;/p&gt;

&lt;h2 id=&quot;caption&quot;&gt;caption&lt;/h2&gt;

&lt;p&gt;（报刊等的）标题&lt;/p&gt;

&lt;p&gt;And the &lt;strong&gt;caption&lt;/strong&gt; said the boy would be going back to Hogwarts.&lt;/p&gt;

&lt;h2 id=&quot;fledged&quot;&gt;fledged&lt;/h2&gt;

&lt;p&gt;羽翼丰满的，成熟的&lt;/p&gt;

&lt;p&gt;Before the Wolfsbane Potion was discovered, however, I became a fully &lt;strong&gt;fledged&lt;/strong&gt; monster once a month.&lt;/p&gt;

&lt;h2 id=&quot;wane&quot;&gt;wane&lt;/h2&gt;

&lt;p&gt;（月亮）亏&lt;/p&gt;

&lt;p&gt;I am able to curl up in my office, a harmless wolf, and wait for the moon to &lt;strong&gt;wane&lt;/strong&gt; again.&lt;/p&gt;

&lt;h2 id=&quot;-haunted&quot;&gt;★ haunted&lt;/h2&gt;

&lt;p&gt;闹鬼的&lt;/p&gt;

&lt;p&gt;‘No one there …'’The place is &lt;strong&gt;haunted&lt;/strong&gt;!’&lt;/p&gt;

&lt;p&gt;‘Very &lt;strong&gt;haunted&lt;/strong&gt; up there, isn’t it?’&lt;/p&gt;

&lt;h2 id=&quot;-derisive&quot;&gt;★ derisive&lt;/h2&gt;

&lt;p&gt;/diraisiv/&lt;/p&gt;

&lt;p&gt;嘲笑的，嘲弄的&lt;/p&gt;

&lt;p&gt;Ahead they could see Malfoy, who was walking with Crabbe and Goyle, and kept looking back, laughing &lt;strong&gt;derisively&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;surly&quot;&gt;surly&lt;/h2&gt;

&lt;p&gt;/’sə:li/&lt;/p&gt;

&lt;p&gt;脾气坏的&lt;/p&gt;

&lt;p&gt;A bunch of &lt;strong&gt;surly&lt;/strong&gt; security trolls had been hired to guard her.&lt;/p&gt;

&lt;h2 id=&quot;convulsive&quot;&gt;convulsive&lt;/h2&gt;

&lt;p&gt;突然的，发狂的&lt;/p&gt;

&lt;p&gt;He pointed at Black, whose face twitched &lt;strong&gt;convulsively&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;trance&quot;&gt;trance&lt;/h2&gt;

&lt;p&gt;出神，恍惚&lt;/p&gt;

&lt;p&gt;Slowly, in a kind of horrified &lt;strong&gt;trance&lt;/strong&gt;, Harry, Ron and Hermione set off silently around Hagrid’s house.&lt;/p&gt;

&lt;h1&gt;—&lt;/h1&gt;

</description>
        <pubDate>Mon, 06 May 2019 00:00:00 +0000</pubDate>
        <link>http://viaductooor.github.io/blog/2019/05/06/Learn-English-Harry-Potter-III/</link>
        <guid isPermaLink="true">http://viaductooor.github.io/blog/2019/05/06/Learn-English-Harry-Potter-III/</guid>
        
        <category>english</category>
        
        
        <category>private</category>
        
      </item>
    
      <item>
        <title>Algorithm - Climbing Leaderboard</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#问题描述&quot; id=&quot;markdown-toc-问题描述&quot;&gt;问题描述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#解题思路&quot; id=&quot;markdown-toc-解题思路&quot;&gt;解题思路&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#算法复杂度&quot; id=&quot;markdown-toc-算法复杂度&quot;&gt;算法复杂度&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#代码&quot; id=&quot;markdown-toc-代码&quot;&gt;代码&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href=&quot;https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem&quot;&gt;Problem&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;问题描述&quot;&gt;问题描述&lt;/h2&gt;

&lt;p&gt;Alice is playing an arcade game and wants to climb to the top of the leaderboard and wants to track her ranking. The game uses Dense Ranking, so its leaderboard works like this:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The player with the highest score is ranked number 1 on the leaderboard.&lt;/li&gt;
  &lt;li&gt;Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, the four players on the leaderboard have high scores of 100, 90, 90 and 80. Those players will have ranks 1, 2, 2, and 3 respectively. If Alice’s scores 70, 80 and 105, her rankings after each game are 4, 3, and 1.&lt;/p&gt;

&lt;p&gt;Complete the climbingLeaderboard function in the editor below. It should return an integer array where each element &lt;code class=&quot;highlighter-rouge&quot;&gt;res[j]&lt;/code&gt; represents Alice’s rank after the jth game.&lt;/p&gt;

&lt;h2 id=&quot;解题思路&quot;&gt;解题思路&lt;/h2&gt;

&lt;p&gt;因为相同的得分排名相同，而且不会因为得分相同导致后面的排名顺延，所以可以考虑先将初始得分数组和Alice得分数组中的相同的分数去掉。&lt;/p&gt;

&lt;p&gt;因为初始得分是递减的，而Alice的得分是递增的，所以可以设置两个指针来比较这两个数组中的元素，其中pscore指向score数组中的第一个元素，移动方向为从前往后；palice指向alice数组中的最后一个元素，移动方向为从后往前。算法步骤为：&lt;/p&gt;

&lt;p&gt;（0）将初始得分的数组记为&lt;code class=&quot;highlighter-rouge&quot;&gt;scores&lt;/code&gt;，将Alice得分的数组记为&lt;code class=&quot;highlighter-rouge&quot;&gt;alice&lt;/code&gt;；&lt;code class=&quot;highlighter-rouge&quot;&gt;pscore = 0; palice = alice.length - 1;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;（1）从&lt;code class=&quot;highlighter-rouge&quot;&gt;pscore&lt;/code&gt;指向的元素开始往后找，找到第一个比&lt;code class=&quot;highlighter-rouge&quot;&gt;alice[palice]&lt;/code&gt;小或者与其相等的元素，将其位置（&lt;code class=&quot;highlighter-rouge&quot;&gt;pscore + 1&lt;/code&gt;，因为第一名下标为0）记录为&lt;code class=&quot;highlighter-rouge&quot;&gt;alice[palice]&lt;/code&gt;的最终排名；如果找不到这样的一个元素，表示&lt;code class=&quot;highlighter-rouge&quot;&gt;scores&lt;/code&gt;中所有元素都比&lt;code class=&quot;highlighter-rouge&quot;&gt;alice[palice]&lt;/code&gt;大，因此&lt;code class=&quot;highlighter-rouge&quot;&gt;alice[palice]&lt;/code&gt;的排名为&lt;code class=&quot;highlighter-rouge&quot;&gt;scores.length + 1&lt;/code&gt;；&lt;/p&gt;

&lt;p&gt;（2）如果&lt;code class=&quot;highlighter-rouge&quot;&gt;palice == alice.length-1&lt;/code&gt;，查找完毕，执行（3）；否则&lt;code class=&quot;highlighter-rouge&quot;&gt;palice ++&lt;/code&gt;，回到（1）。&lt;/p&gt;

&lt;p&gt;（3）遍历初始的未去重alice数组，根据上面记录下的每一个分数的排名来得到数组中各个分数的排名，返回排名数组。&lt;/p&gt;

&lt;h2 id=&quot;算法复杂度&quot;&gt;算法复杂度&lt;/h2&gt;

&lt;p&gt;去重部分复杂度为&lt;em&gt;O(n)&lt;/em&gt;，查找部分复杂度为&lt;em&gt;O(m+n)&lt;/em&gt;。&lt;/p&gt;

&lt;h2 id=&quot;代码&quot;&gt;代码&lt;/h2&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;java.io.*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;java.math.*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;java.security.*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;java.text.*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;java.util.*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;java.util.concurrent.*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;java.util.regex.*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;deleteDuplicate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newScores&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newLength&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++){&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;newScores&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;newLength&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++;&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]!=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]){&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;newScores&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newLength&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;newLength&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++;&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Arrays&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;copyOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newScores&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newLength&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;// Complete the climbingLeaderboard function below.&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;climbingLeaderboard&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dscore&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deleteDuplicate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dalice&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deleteDuplicate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HashMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pa&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dalice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pa&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dalice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pa&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&amp;gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dscore&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]){&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dalice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pa&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;pa&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;--;&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dscore&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dalice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pa&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dscore&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;pa&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;--;&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;ps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++;&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rank&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++){&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;rank&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]);&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rank&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scanner&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scanner&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scanner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IOException&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;BufferedWriter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bufferedWriter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BufferedWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FileWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;OUTPUT_PATH&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)));&lt;/span&gt;

        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scoresCount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scanner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;nextInt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;scanner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;skip&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;(\r\n|[\n\r\u2028\u2029\u0085])?&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scoresCount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scoresItems&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scanner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;nextLine&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;scanner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;skip&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;(\r\n|[\n\r\u2028\u2029\u0085])?&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scoresCount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scoresItem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;parseInt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scoresItems&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]);&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scoresItem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;aliceCount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scanner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;nextInt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;scanner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;skip&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;(\r\n|[\n\r\u2028\u2029\u0085])?&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alice&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;aliceCount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;aliceItems&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scanner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;nextLine&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;scanner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;skip&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;(\r\n|[\n\r\u2028\u2029\u0085])?&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;aliceCount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;aliceItem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;parseInt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;aliceItems&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]);&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;alice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;aliceItem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;climbingLeaderboard&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;bufferedWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;valueOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]));&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;bufferedWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;\n&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;bufferedWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;newLine&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;bufferedWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;scanner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Mon, 22 Apr 2019 00:00:00 +0000</pubDate>
        <link>http://viaductooor.github.io/blog/2019/04/22/Algorithm-ClimbingLeaderboard/</link>
        <guid isPermaLink="true">http://viaductooor.github.io/blog/2019/04/22/Algorithm-ClimbingLeaderboard/</guid>
        
        <category>algorithm</category>
        
        <category>hackerrank</category>
        
        
        <category>algorithm</category>
        
      </item>
    
      <item>
        <title>Wordlist - Harry Potter II</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#phrases&quot; id=&quot;markdown-toc-phrases&quot;&gt;Phrases&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#to-the-hilt&quot; id=&quot;markdown-toc-to-the-hilt&quot;&gt;to the hilt&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#buck-up&quot; id=&quot;markdown-toc-buck-up&quot;&gt;buck up&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#dung-beetle&quot; id=&quot;markdown-toc-dung-beetle&quot;&gt;dung beetle&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#the-end-of-ones-tether&quot; id=&quot;markdown-toc-the-end-of-ones-tether&quot;&gt;the end of one’s tether&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#crop-up&quot; id=&quot;markdown-toc-crop-up&quot;&gt;crop up&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#on-all-fours&quot; id=&quot;markdown-toc-on-all-fours&quot;&gt;on all fours&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#on-tenterhooks&quot; id=&quot;markdown-toc-on-tenterhooks&quot;&gt;on tenterhooks&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#butt-in&quot; id=&quot;markdown-toc-butt-in&quot;&gt;butt in&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#bee-in-ones-bonnet&quot; id=&quot;markdown-toc-bee-in-ones-bonnet&quot;&gt;bee in one’s bonnet&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#nip-off&quot; id=&quot;markdown-toc-nip-off&quot;&gt;nip off&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#pore-over&quot; id=&quot;markdown-toc-pore-over&quot;&gt;pore over&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#keel-over&quot; id=&quot;markdown-toc-keel-over&quot;&gt;keel over&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#draw-level-with&quot; id=&quot;markdown-toc-draw-level-with&quot;&gt;draw level with&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#words&quot; id=&quot;markdown-toc-words&quot;&gt;Words&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#cuff&quot; id=&quot;markdown-toc-cuff&quot;&gt;cuff&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#frail&quot; id=&quot;markdown-toc-frail&quot;&gt;frail&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#wring&quot; id=&quot;markdown-toc-wring&quot;&gt;wring&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#incensed&quot; id=&quot;markdown-toc-incensed&quot;&gt;incensed&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#torrent&quot; id=&quot;markdown-toc-torrent&quot;&gt;torrent&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#recede&quot; id=&quot;markdown-toc-recede&quot;&gt;recede&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#slit&quot; id=&quot;markdown-toc-slit&quot;&gt;slit&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#serene&quot; id=&quot;markdown-toc-serene&quot;&gt;serene&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#abject-hem&quot; id=&quot;markdown-toc-abject-hem&quot;&gt;abject, hem&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tumble&quot; id=&quot;markdown-toc-tumble&quot;&gt;tumble&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#hoodwink&quot; id=&quot;markdown-toc-hoodwink&quot;&gt;hoodwink&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#ordeal&quot; id=&quot;markdown-toc-ordeal&quot;&gt;ordeal&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#frame&quot; id=&quot;markdown-toc-frame&quot;&gt;frame&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#-hollow&quot; id=&quot;markdown-toc--hollow&quot;&gt;★ hollow&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#pillar&quot; id=&quot;markdown-toc-pillar&quot;&gt;pillar&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#skirt&quot; id=&quot;markdown-toc-skirt&quot;&gt;skirt&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#blunder&quot; id=&quot;markdown-toc-blunder&quot;&gt;blunder&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#pixie--pixy&quot; id=&quot;markdown-toc-pixie--pixy&quot;&gt;pixie &amp;amp; pixy&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#belch&quot; id=&quot;markdown-toc-belch&quot;&gt;belch&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#hearty&quot; id=&quot;markdown-toc-hearty&quot;&gt;hearty&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#volley&quot; id=&quot;markdown-toc-volley&quot;&gt;volley&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#venomous&quot; id=&quot;markdown-toc-venomous&quot;&gt;venomous&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tray&quot; id=&quot;markdown-toc-tray&quot;&gt;tray&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#earmuff&quot; id=&quot;markdown-toc-earmuff&quot;&gt;earmuff&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#compost&quot; id=&quot;markdown-toc-compost&quot;&gt;compost&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tuft&quot; id=&quot;markdown-toc-tuft&quot;&gt;tuft&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tentative&quot; id=&quot;markdown-toc-tentative&quot;&gt;tentative&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#twinge&quot; id=&quot;markdown-toc-twinge&quot;&gt;twinge&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#velvet&quot; id=&quot;markdown-toc-velvet&quot;&gt;velvet&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#bloke&quot; id=&quot;markdown-toc-bloke&quot;&gt;bloke&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#compartment&quot; id=&quot;markdown-toc-compartment&quot;&gt;compartment&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#hex&quot; id=&quot;markdown-toc-hex&quot;&gt;hex&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#gnome&quot; id=&quot;markdown-toc-gnome&quot;&gt;gnome&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#-foul&quot; id=&quot;markdown-toc--foul&quot;&gt;★ foul&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#simper&quot; id=&quot;markdown-toc-simper&quot;&gt;simper&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#scrub&quot; id=&quot;markdown-toc-scrub&quot;&gt;scrub&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#stumble&quot; id=&quot;markdown-toc-stumble&quot;&gt;stumble&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#fishy&quot; id=&quot;markdown-toc-fishy&quot;&gt;fishy&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#haphazard&quot; id=&quot;markdown-toc-haphazard&quot;&gt;haphazard&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#aghast&quot; id=&quot;markdown-toc-aghast&quot;&gt;aghast&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#-tread&quot; id=&quot;markdown-toc--tread&quot;&gt;★ tread&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#-implore&quot; id=&quot;markdown-toc--implore&quot;&gt;★ implore&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#commotion&quot; id=&quot;markdown-toc-commotion&quot;&gt;commotion&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#irresolute&quot; id=&quot;markdown-toc-irresolute&quot;&gt;irresolute&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#acne&quot; id=&quot;markdown-toc-acne&quot;&gt;acne&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#-disperse&quot; id=&quot;markdown-toc--disperse&quot;&gt;★ disperse&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#chuck&quot; id=&quot;markdown-toc-chuck&quot;&gt;chuck&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#squid&quot; id=&quot;markdown-toc-squid&quot;&gt;squid&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#surge&quot; id=&quot;markdown-toc-surge&quot;&gt;surge&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#writhe&quot; id=&quot;markdown-toc-writhe&quot;&gt;writhe&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#wrinkled&quot; id=&quot;markdown-toc-wrinkled&quot;&gt;wrinkled&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tartan&quot; id=&quot;markdown-toc-tartan&quot;&gt;tartan&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#blunt&quot; id=&quot;markdown-toc-blunt&quot;&gt;blunt&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#lilac&quot; id=&quot;markdown-toc-lilac&quot;&gt;lilac&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#whack&quot; id=&quot;markdown-toc-whack&quot;&gt;whack&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#morale&quot; id=&quot;markdown-toc-morale&quot;&gt;morale&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#trickle&quot; id=&quot;markdown-toc-trickle&quot;&gt;trickle&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#derisive&quot; id=&quot;markdown-toc-derisive&quot;&gt;derisive&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#shred&quot; id=&quot;markdown-toc-shred&quot;&gt;shred&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#shatter&quot; id=&quot;markdown-toc-shatter&quot;&gt;shatter&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#malevolent&quot; id=&quot;markdown-toc-malevolent&quot;&gt;malevolent&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#scarper&quot; id=&quot;markdown-toc-scarper&quot;&gt;scarper&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tamper&quot; id=&quot;markdown-toc-tamper&quot;&gt;tamper&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#bait&quot; id=&quot;markdown-toc-bait&quot;&gt;bait&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#undaunted&quot; id=&quot;markdown-toc-undaunted&quot;&gt;undaunted&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#abashed&quot; id=&quot;markdown-toc-abashed&quot;&gt;abashed&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#remnants&quot; id=&quot;markdown-toc-remnants&quot;&gt;remnants&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#wheezy&quot; id=&quot;markdown-toc-wheezy&quot;&gt;wheezy&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#divination&quot; id=&quot;markdown-toc-divination&quot;&gt;divination&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#vulture&quot; id=&quot;markdown-toc-vulture&quot;&gt;vulture&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#prop&quot; id=&quot;markdown-toc-prop&quot;&gt;prop&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#throb&quot; id=&quot;markdown-toc-throb&quot;&gt;throb&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#wary&quot; id=&quot;markdown-toc-wary&quot;&gt;wary&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#plaque&quot; id=&quot;markdown-toc-plaque&quot;&gt;plaque&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#custard&quot; id=&quot;markdown-toc-custard&quot;&gt;custard&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#scalp&quot; id=&quot;markdown-toc-scalp&quot;&gt;scalp&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tingle&quot; id=&quot;markdown-toc-tingle&quot;&gt;tingle&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#dregs&quot; id=&quot;markdown-toc-dregs&quot;&gt;dregs&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#cub&quot; id=&quot;markdown-toc-cub&quot;&gt;cub&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#blundering&quot; id=&quot;markdown-toc-blundering&quot;&gt;blundering&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;phrases&quot;&gt;Phrases&lt;/h1&gt;

&lt;h2 id=&quot;to-the-hilt&quot;&gt;to the hilt&lt;/h2&gt;

&lt;p&gt;完全地，彻底地&lt;/p&gt;

&lt;p&gt;Harry threw his whole weight behind the sword and drove it &lt;strong&gt;to the hilt&lt;/strong&gt; into the roof of the serpent’s mouth.&lt;/p&gt;

&lt;h2 id=&quot;buck-up&quot;&gt;buck up&lt;/h2&gt;

&lt;p&gt;振奋起来&lt;/p&gt;

&lt;p&gt;“Harry, &lt;strong&gt;buck&lt;/strong&gt; up there, you need a decent breakfast.”&lt;/p&gt;

&lt;h2 id=&quot;dung-beetle&quot;&gt;dung beetle&lt;/h2&gt;

&lt;p&gt;屎壳郎&lt;/p&gt;

&lt;p&gt;He knew it was only their terror that he might turn them all into &lt;strong&gt;dung beetles&lt;/strong&gt; that stopped them locking him in the cupboard.&lt;/p&gt;

&lt;h2 id=&quot;the-end-of-ones-tether&quot;&gt;the end of one’s tether&lt;/h2&gt;

&lt;p&gt;山穷水尽&lt;/p&gt;

&lt;p&gt;The car, however, had &lt;strong&gt;reached the end of its tether&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;tether: 栓绳，范围&lt;/p&gt;

&lt;h2 id=&quot;crop-up&quot;&gt;crop up&lt;/h2&gt;

&lt;p&gt;突然出现&lt;/p&gt;

&lt;p&gt;Harry had been wondering when his name was going to &lt;strong&gt;crop up&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;on-all-fours&quot;&gt;on all fours&lt;/h2&gt;

&lt;p&gt;四脚着地&lt;/p&gt;

&lt;p&gt;Malfoy was &lt;strong&gt;on all fours&lt;/strong&gt;, banging the ground with his fist.&lt;/p&gt;

&lt;h2 id=&quot;on-tenterhooks&quot;&gt;on tenterhooks&lt;/h2&gt;

&lt;p&gt;提心吊胆，焦虑不安&lt;/p&gt;

&lt;p&gt;He (Nearly Headless Nick) watched Harry &lt;strong&gt;on tenterhooks&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;butt-in&quot;&gt;butt in&lt;/h2&gt;

&lt;p&gt;插嘴，插手&lt;/p&gt;

&lt;p&gt;‘I’ll make it,’ Lockhart &lt;strong&gt;butted in&lt;/strong&gt;. ‘I must have done it a hundred times.’&lt;/p&gt;

&lt;h2 id=&quot;bee-in-ones-bonnet&quot;&gt;bee in one’s bonnet&lt;/h2&gt;

&lt;p&gt;（某人）奇思异想，头脑发热&lt;/p&gt;

&lt;p&gt;—— Then a small voice said in his ear, ‘&lt;strong&gt;Bee in your bonnet&lt;/strong&gt;, Harry Potter?’&lt;/p&gt;

&lt;p&gt;—— ‘Er, yes,’ Harry muttered. ‘Er - sorry to bother you - I wanted to ask -‘&lt;/p&gt;

&lt;h2 id=&quot;nip-off&quot;&gt;nip off&lt;/h2&gt;

&lt;p&gt;很快走开&lt;/p&gt;

&lt;p&gt;—— ‘Oh, get out of the way, Percy’, said Fred. ‘Harry’s in a hurry.’&lt;/p&gt;

&lt;p&gt;—— ‘Yeah, he’s &lt;strong&gt;nipping off&lt;/strong&gt; to the Chamber of Secrets for a cup of tea with his fanged servant,’ said George, chortling.&lt;/p&gt;

&lt;h2 id=&quot;pore-over&quot;&gt;pore over&lt;/h2&gt;

&lt;p&gt;用心研读&lt;/p&gt;

&lt;p&gt;“It could affect our whole future,” she told Harry and Ron, as they &lt;strong&gt;pored over&lt;/strong&gt; lists of new subjects, marking them with ticks.&lt;/p&gt;

&lt;h2 id=&quot;keel-over&quot;&gt;keel over&lt;/h2&gt;

&lt;p&gt;倒下&lt;/p&gt;

&lt;p&gt;… as the basilisk &lt;strong&gt;keeled over&lt;/strong&gt; sideways and fell twitching, to the floor.&lt;/p&gt;

&lt;h2 id=&quot;draw-level-with&quot;&gt;draw level with&lt;/h2&gt;

&lt;p&gt;与……比肩&lt;/p&gt;

&lt;p&gt;Then, as he &lt;strong&gt;drew level with&lt;/strong&gt; the last pair of pillars, a statue high as the chamber itself loomed in to view, standing against the back wall.&lt;/p&gt;

&lt;p&gt;Trying to &lt;strong&gt;draw level with&lt;/strong&gt; Asia in chipmaking would be futile（徒劳的）.&lt;/p&gt;

&lt;h1 id=&quot;words&quot;&gt;Words&lt;/h1&gt;

&lt;h2 id=&quot;cuff&quot;&gt;cuff&lt;/h2&gt;

&lt;p&gt;拍打，掌击&lt;/p&gt;

&lt;p&gt;Hagrid turning up at half past three, &lt;strong&gt;cuffing&lt;/strong&gt; Harry on the shoulder.&lt;/p&gt;

&lt;h2 id=&quot;frail&quot;&gt;frail&lt;/h2&gt;

&lt;p&gt;虚弱的，衰弱的&lt;/p&gt;

&lt;p&gt;A wizened, &lt;strong&gt;frail-looking&lt;/strong&gt; wizard, bald except for a few wisps of white hair, was reading a letter by candlelight.&lt;/p&gt;

&lt;h2 id=&quot;wring&quot;&gt;wring&lt;/h2&gt;

&lt;p&gt;绞，拧&lt;/p&gt;

&lt;p&gt;Justin hurried over from the Hufflepuff table to &lt;strong&gt;wring&lt;/strong&gt; his hand and apologise endlessly for suspecting Harry.&lt;/p&gt;

&lt;h2 id=&quot;incensed&quot;&gt;incensed&lt;/h2&gt;

&lt;p&gt;极为愤怒的&lt;/p&gt;

&lt;p&gt;With a last, &lt;strong&gt;incensed&lt;/strong&gt; stare at the pair of them, he (Lucius Malfoy) swung his cloak around him and hurried out of sight.&lt;/p&gt;

&lt;p&gt;Her ladyship was highly &lt;strong&gt;incensed&lt;/strong&gt;. – Pride and Prejudice&lt;/p&gt;

&lt;h2 id=&quot;torrent&quot;&gt;torrent&lt;/h2&gt;

&lt;p&gt;洪流，（感情的）爆发&lt;/p&gt;

&lt;p&gt;Ink spurted out of the diary in &lt;strong&gt;torrents&lt;/strong&gt;, streaming over Harry’s hands, flooding the floor.&lt;/p&gt;

&lt;h2 id=&quot;recede&quot;&gt;recede&lt;/h2&gt;

&lt;p&gt;逐渐模糊，变得渺茫&lt;/p&gt;

&lt;p&gt;He could hear Dobby’s squeals of pain &lt;strong&gt;receding&lt;/strong&gt; around the corner.&lt;/p&gt;

&lt;h2 id=&quot;slit&quot;&gt;slit&lt;/h2&gt;

&lt;p&gt;缝隙，裂缝&lt;/p&gt;

&lt;p&gt;Mr Malfoy went even paler than usual, but his eyes were still &lt;strong&gt;slits&lt;/strong&gt; of fury.&lt;/p&gt;

&lt;h2 id=&quot;serene&quot;&gt;serene&lt;/h2&gt;

&lt;p&gt;安详的，宁静的&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;serene: si’ri:n&lt;/li&gt;
  &lt;li&gt;serenity: si’reniti&lt;/li&gt;
  &lt;li&gt;serenely: si’rinli&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;‘Well, you see, Lucius,’ said Dumbledore, smiling &lt;strong&gt;serenely&lt;/strong&gt;, ‘the other eleven goveners contacted me today.’&lt;/p&gt;

&lt;h2 id=&quot;abject-hem&quot;&gt;abject, hem&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;abject: 绝望无助的&lt;/li&gt;
  &lt;li&gt;hem: 边缘&lt;/li&gt;
  &lt;li&gt;crouch: 蜷伏，蹲伏&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Dobby went scurrying in after him, crouching at the &lt;strong&gt;hem&lt;/strong&gt; of his cloak, a look of &lt;strong&gt;abject&lt;/strong&gt; terror on his face&lt;/p&gt;

&lt;h2 id=&quot;tumble&quot;&gt;tumble&lt;/h2&gt;

&lt;p&gt;翻跟斗，打滚&lt;/p&gt;

&lt;p&gt;Both of the wrestlers tried to &lt;strong&gt;tumble&lt;/strong&gt; the adversary with all their strength.&lt;/p&gt;

&lt;h2 id=&quot;hoodwink&quot;&gt;hoodwink&lt;/h2&gt;

&lt;p&gt;欺骗，蒙蔽&lt;/p&gt;

&lt;p&gt;Older and wiser wizards than she have been &lt;strong&gt;hoodwinked&lt;/strong&gt; by Lord Voldemort.&lt;/p&gt;

&lt;h2 id=&quot;ordeal&quot;&gt;ordeal&lt;/h2&gt;

&lt;p&gt;严峻的考验，苦难的经历&lt;/p&gt;

&lt;p&gt;This has been a terrible &lt;strong&gt;ordeal&lt;/strong&gt; for her.&lt;/p&gt;

&lt;h2 id=&quot;frame&quot;&gt;frame&lt;/h2&gt;

&lt;p&gt;设计陷害&lt;/p&gt;

&lt;p&gt;And you &lt;strong&gt;framed&lt;/strong&gt; him, didn’t you?&lt;/p&gt;

&lt;h2 id=&quot;-hollow&quot;&gt;★ hollow&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;adj. 凹的，下陷的；中空的；声音空洞的；虚假的&lt;/li&gt;
  &lt;li&gt;n. 凹陷，盆地，山谷&lt;/li&gt;
  &lt;li&gt;v. 挖空&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;hollow&lt;/strong&gt; eye sockets of the stone snakes seemed to be following him.&lt;/p&gt;

&lt;p&gt;They had reached the rim of a vast &lt;strong&gt;hollow&lt;/strong&gt;, a hollow which had been cleared of trees.&lt;/p&gt;

&lt;h2 id=&quot;pillar&quot;&gt;pillar&lt;/h2&gt;

&lt;p&gt;柱子&lt;/p&gt;

&lt;p&gt;towering stone &lt;strong&gt;pillars&lt;/strong&gt; entwined with more carved serpents&lt;/p&gt;

&lt;h2 id=&quot;skirt&quot;&gt;skirt&lt;/h2&gt;

&lt;p&gt;绕过，回避&lt;/p&gt;

&lt;p&gt;He was tired of people &lt;strong&gt;skirting&lt;/strong&gt; around him in the corridors.&lt;/p&gt;

&lt;h2 id=&quot;blunder&quot;&gt;blunder&lt;/h2&gt;

&lt;p&gt;跌跌撞撞地走&lt;/p&gt;

&lt;p&gt;Goyle &lt;strong&gt;blundered&lt;/strong&gt; around, his hands over his eyes.&lt;/p&gt;

&lt;h2 id=&quot;pixie--pixy&quot;&gt;pixie &amp;amp; pixy&lt;/h2&gt;

&lt;p&gt;小精灵，小妖精&lt;/p&gt;

&lt;p&gt;freshly caught Cornish &lt;strong&gt;pixies&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;belch&quot;&gt;belch&lt;/h2&gt;

&lt;p&gt;打嗝&lt;/p&gt;

&lt;p&gt;Instead he gave an almighty &lt;strong&gt;belch&lt;/strong&gt; and severl slugs dribbled out of his mouth onto his lap.&lt;/p&gt;

&lt;h2 id=&quot;hearty&quot;&gt;hearty&lt;/h2&gt;

&lt;p&gt;（虚假地装出）热情亲切的&lt;/p&gt;

&lt;p&gt;He gave a &lt;strong&gt;hearty&lt;/strong&gt; wink and strode off.&lt;/p&gt;

&lt;h2 id=&quot;volley&quot;&gt;volley&lt;/h2&gt;

&lt;p&gt;齐射，连发&lt;/p&gt;

&lt;p&gt;‘Write home for another one,’ Harry suggested, as the wand let off a &lt;strong&gt;volley&lt;/strong&gt; of bangs like a firecracker.&lt;/p&gt;

&lt;h2 id=&quot;venomous&quot;&gt;venomous&lt;/h2&gt;

&lt;p&gt;有毒的；恶毒的，狠毒的&lt;/p&gt;

&lt;h2 id=&quot;tray&quot;&gt;tray&lt;/h2&gt;

&lt;p&gt;托盘，盘子&lt;/p&gt;

&lt;p&gt;She pointed to a row of deep &lt;strong&gt;trays&lt;/strong&gt; as she spoke and everyone shuffled forward for a better look.&lt;/p&gt;

&lt;h2 id=&quot;earmuff&quot;&gt;earmuff&lt;/h2&gt;

&lt;p&gt;（御寒用的）耳套&lt;/p&gt;

&lt;p&gt;About twenty pairs of different-coloured &lt;strong&gt;earmuffs&lt;/strong&gt; were lying on the bench.&lt;/p&gt;

&lt;h2 id=&quot;compost&quot;&gt;compost&lt;/h2&gt;

&lt;p&gt;堆肥，混合肥料&lt;/p&gt;

&lt;p&gt;… plunged the Mandrake into it, burying him in dark, damp &lt;strong&gt;compost&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;tuft&quot;&gt;tuft&lt;/h2&gt;

&lt;p&gt;一簇，一束&lt;/p&gt;

&lt;p&gt;tufts of grass&lt;/p&gt;

&lt;p&gt;A hundred or so &lt;strong&gt;tufty&lt;/strong&gt; little plants, purplish green in colour, were growing there in rows.&lt;/p&gt;

&lt;h2 id=&quot;tentative&quot;&gt;tentative&lt;/h2&gt;

&lt;p&gt;试探性的&lt;/p&gt;

&lt;p&gt;I’m - I’m Colin Creevey,’ he said breathlessly, taking a &lt;strong&gt;tentative&lt;/strong&gt; step forward.&lt;/p&gt;

&lt;h2 id=&quot;twinge&quot;&gt;twinge&lt;/h2&gt;

&lt;p&gt;剧痛，阵痛&lt;/p&gt;

&lt;p&gt;Professor Sprouts arms were full of bandages, and with another &lt;strong&gt;twinge&lt;/strong&gt; of guilt, Harry spotted the Whomping Willow in the distance,…&lt;/p&gt;

&lt;h2 id=&quot;velvet&quot;&gt;velvet&lt;/h2&gt;

&lt;p&gt;丝绒，天鹅绒&lt;/p&gt;

&lt;p&gt;They entered the familiar, circular room, with its five four-posters hung with red &lt;strong&gt;velvet&lt;/strong&gt; and its high, narrow windows.&lt;/p&gt;

&lt;h2 id=&quot;bloke&quot;&gt;bloke&lt;/h2&gt;

&lt;p&gt;人，家伙&lt;/p&gt;

&lt;p&gt;He was asking that &lt;strong&gt;bloke&lt;/strong&gt; from the Daily Prophet if he’d be able to work the fight into his report.&lt;/p&gt;

&lt;h2 id=&quot;compartment&quot;&gt;compartment&lt;/h2&gt;

&lt;p&gt;分隔间，单间&lt;/p&gt;

&lt;p&gt;… with a fat pack of toffees in the glove &lt;strong&gt;compartment&lt;/strong&gt;, …&lt;/p&gt;

&lt;h2 id=&quot;hex&quot;&gt;hex&lt;/h2&gt;

&lt;p&gt;不祥之物，恶毒的诅咒&lt;/p&gt;

&lt;p&gt;And old Mundungus Fletcher tried to put a &lt;strong&gt;hex&lt;/strong&gt; on me when I had my back turned …&lt;/p&gt;

&lt;h2 id=&quot;gnome&quot;&gt;gnome&lt;/h2&gt;

&lt;p&gt;/noum/&lt;/p&gt;

&lt;p&gt;侏儒&lt;/p&gt;

&lt;p&gt;You’re going to &lt;strong&gt;de-gnome&lt;/strong&gt; the garden for me.&lt;/p&gt;

&lt;h2 id=&quot;-foul&quot;&gt;★ foul&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;难闻的，恶臭的&lt;/li&gt;
  &lt;li&gt;肮脏的，污秽的&lt;/li&gt;
  &lt;li&gt;邪恶的，不正当的&lt;/li&gt;
  &lt;li&gt;犯规的&lt;/li&gt;
  &lt;li&gt;（天气等）恶劣的&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Dudley put on a &lt;strong&gt;foul&lt;/strong&gt;, simpering smile.&lt;/p&gt;

&lt;h2 id=&quot;simper&quot;&gt;simper&lt;/h2&gt;

&lt;p&gt;傻笑&lt;/p&gt;

&lt;p&gt;He &lt;strong&gt;simpered&lt;/strong&gt; his explanation.&lt;/p&gt;

&lt;h2 id=&quot;scrub&quot;&gt;scrub&lt;/h2&gt;

&lt;p&gt;用力擦，擦亮&lt;/p&gt;

&lt;p&gt;… (Harry) started &lt;strong&gt;scrubbing&lt;/strong&gt; the kitchen clean.&lt;/p&gt;

&lt;h2 id=&quot;stumble&quot;&gt;stumble&lt;/h2&gt;

&lt;p&gt;绊倒&lt;/p&gt;

&lt;p&gt;Dudley &lt;strong&gt;stumbled&lt;/strong&gt; backwards at once, a look of panic on his fat face.&lt;/p&gt;

&lt;h2 id=&quot;fishy&quot;&gt;fishy&lt;/h2&gt;

&lt;p&gt;可疑的，靠不住的&lt;/p&gt;

&lt;p&gt;‘Very &lt;strong&gt;fishy&lt;/strong&gt;,’ said Fred finally.&lt;/p&gt;

&lt;h2 id=&quot;haphazard&quot;&gt;haphazard&lt;/h2&gt;

&lt;p&gt;/,hap’hazard/&lt;/p&gt;

&lt;p&gt;随意的，无计划的&lt;/p&gt;

&lt;p&gt;Mrs Weasley was clattering around, cooking breakfast a little &lt;strong&gt;haphazardly&lt;/strong&gt;, throwing dirty looks at her sons as she threw sausages into the frying pan.&lt;/p&gt;

&lt;h2 id=&quot;aghast&quot;&gt;aghast&lt;/h2&gt;

&lt;p&gt;吓呆的&lt;/p&gt;

&lt;p&gt;Hermione looked &lt;strong&gt;aghast&lt;/strong&gt; at the news.&lt;/p&gt;

&lt;h2 id=&quot;-tread&quot;&gt;★ tread&lt;/h2&gt;

&lt;p&gt;行走于；踩踏，踩碎&lt;/p&gt;

&lt;p&gt;Harry walked over to the bed, open-mouthed, &lt;strong&gt;treading&lt;/strong&gt; on a few loose pages of &lt;em&gt;Travels with Trolls&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;-implore&quot;&gt;★ implore&lt;/h2&gt;

&lt;p&gt;恳求；乞求；哀求&lt;/p&gt;

&lt;p&gt;‘I never,’ said Hagrid, looking &lt;strong&gt;imploringly&lt;/strong&gt; at Dumbledore, ‘You know I never, Professor Dumbledore, sir…’&lt;/p&gt;

&lt;h2 id=&quot;commotion&quot;&gt;commotion&lt;/h2&gt;

&lt;p&gt;混乱；骚乱&lt;/p&gt;

&lt;p&gt;‘What’s all this &lt;strong&gt;commotion&lt;/strong&gt;?’&lt;/p&gt;

&lt;h2 id=&quot;irresolute&quot;&gt;irresolute&lt;/h2&gt;

&lt;p&gt;犹豫不决的&lt;/p&gt;

&lt;p&gt;Harry stood, &lt;strong&gt;irresolute&lt;/strong&gt;, trying to catch the voice again, but people were now emerging from the Great Hall behind him, talking loudly.&lt;/p&gt;

&lt;h2 id=&quot;acne&quot;&gt;acne&lt;/h2&gt;

&lt;p&gt;/’akni/&lt;/p&gt;

&lt;p&gt;粉刺，痤疮&lt;/p&gt;

&lt;p&gt;The moment their (Mandrake) &lt;strong&gt;acne&lt;/strong&gt; clears up, they’ll be ready for repotting (移栽) again.&lt;/p&gt;

&lt;h2 id=&quot;-disperse&quot;&gt;★ disperse&lt;/h2&gt;

&lt;p&gt;驱散，疏散&lt;/p&gt;

&lt;p&gt;Percy Weasley did his best to &lt;strong&gt;disperse&lt;/strong&gt; the crowd.&lt;/p&gt;

&lt;h2 id=&quot;chuck&quot;&gt;chuck&lt;/h2&gt;

&lt;p&gt;抛，扔&lt;/p&gt;

&lt;p&gt;I don’t know why you don’t &lt;strong&gt;chuck&lt;/strong&gt; it, Harry.&lt;/p&gt;

&lt;h2 id=&quot;squid&quot;&gt;squid&lt;/h2&gt;

&lt;p&gt;鱿鱼&lt;/p&gt;

&lt;p&gt;Maybe he got thirty O.W.Ls or saved a teacher from the giant &lt;strong&gt;squid&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;surge&quot;&gt;surge&lt;/h2&gt;

&lt;p&gt;汹涌，奔腾&lt;/p&gt;

&lt;p&gt;He put up her card with a fresh &lt;strong&gt;surge&lt;/strong&gt; of guilt, thinking about Mr Weasley’s car.&lt;/p&gt;

&lt;h2 id=&quot;writhe&quot;&gt;writhe&lt;/h2&gt;

&lt;p&gt;/raithe/&lt;/p&gt;

&lt;p&gt;（因痛苦）打滚&lt;/p&gt;

&lt;p&gt;Immediately, his insides started &lt;strong&gt;writhing&lt;/strong&gt; as though he’d just swallowed live snakes.&lt;/p&gt;

&lt;h2 id=&quot;wrinkled&quot;&gt;wrinkled&lt;/h2&gt;

&lt;p&gt;起皱的&lt;/p&gt;

&lt;p&gt;Harry looked down in time to see a tiny, &lt;strong&gt;wrinkled&lt;/strong&gt;, new-born bird poke its head out of the ashes.&lt;/p&gt;

&lt;h2 id=&quot;tartan&quot;&gt;tartan&lt;/h2&gt;

&lt;p&gt;（苏格兰高地人的）格子呢&lt;/p&gt;

&lt;p&gt;There was thick &lt;strong&gt;tartan&lt;/strong&gt; scarf bound around his head, and his nose was unusually purple.&lt;/p&gt;

&lt;h2 id=&quot;blunt&quot;&gt;blunt&lt;/h2&gt;

&lt;p&gt;钝的，不锋利的&lt;/p&gt;

&lt;p&gt;Getting hit forty-five times in the neck with a &lt;strong&gt;blunt&lt;/strong&gt; axe would qualify you to join the Headless Hunt.&lt;/p&gt;

&lt;h2 id=&quot;lilac&quot;&gt;lilac&lt;/h2&gt;

&lt;p&gt;/’lailək/&lt;/p&gt;

&lt;p&gt;丁香花；淡紫色&lt;/p&gt;

&lt;p&gt;Harry gave a huge jump and a large &lt;strong&gt;lilac&lt;/strong&gt; blot appeared on Veronica Smethley’s letter.&lt;/p&gt;

&lt;h2 id=&quot;whack&quot;&gt;whack&lt;/h2&gt;

&lt;p&gt;重打，猛击&lt;/p&gt;

&lt;p&gt;Everyone filed out of the classroom except him and Ron, who was &lt;strong&gt;whacking&lt;/strong&gt; his wand furiously on the  desk.&lt;/p&gt;

&lt;h2 id=&quot;morale&quot;&gt;morale&lt;/h2&gt;

&lt;p&gt;精神面貌，斗志&lt;/p&gt;

&lt;p&gt;You know, what the school needs now is a &lt;strong&gt;morale-booster&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;trickle&quot;&gt;trickle&lt;/h2&gt;

&lt;p&gt;滴&lt;/p&gt;

&lt;p&gt;Thick, pearly tears were &lt;strong&gt;trickling&lt;/strong&gt; down the glossy feathers.&lt;/p&gt;

&lt;h2 id=&quot;derisive&quot;&gt;derisive&lt;/h2&gt;

&lt;p&gt;嘲笑的，嘲弄的&lt;/p&gt;

&lt;p&gt;He gave a short, &lt;strong&gt;derisive&lt;/strong&gt; laugh.&lt;/p&gt;

&lt;h2 id=&quot;shred&quot;&gt;shred&lt;/h2&gt;

&lt;p&gt;些许，最少量（常用否定）&lt;/p&gt;

&lt;p&gt;There is not a &lt;strong&gt;shred&lt;/strong&gt; of evidence that Slytherin ever built so much as a secret broom cupboard.&lt;/p&gt;

&lt;h2 id=&quot;shatter&quot;&gt;shatter&lt;/h2&gt;

&lt;p&gt;使破碎，使粉碎&lt;/p&gt;

&lt;p&gt;Cream splattered the windows and walls as the dish &lt;strong&gt;shattered&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;malevolent&quot;&gt;malevolent&lt;/h2&gt;

&lt;p&gt;恶意的，恶毒&lt;/p&gt;

&lt;p&gt;A bad idea, Professor Lockhart,’ said Snape, gliding overr like a large and &lt;strong&gt;malevolent&lt;/strong&gt; bat.&lt;/p&gt;

&lt;h2 id=&quot;scarper&quot;&gt;scarper&lt;/h2&gt;

&lt;p&gt;逃走，溜掉&lt;/p&gt;

&lt;p&gt;Ginny jumped up as though her chair had just been electrified, gave Percy a fleeting, frightened look, and &lt;strong&gt;scarpered&lt;/strong&gt; away.&lt;/p&gt;

&lt;h2 id=&quot;tamper&quot;&gt;tamper&lt;/h2&gt;

&lt;p&gt;篡改；贿赂&lt;/p&gt;

&lt;p&gt;‘Someone’s &lt;strong&gt;tampered&lt;/strong&gt; with this Bludger,’ Fred grunted, swinging his bat with all his might at it as it launched a new attack on Harry.&lt;/p&gt;

&lt;h2 id=&quot;bait&quot;&gt;bait&lt;/h2&gt;

&lt;p&gt;n. 诱饵&lt;/p&gt;

&lt;p&gt;v. 诱惑&lt;/p&gt;

&lt;p&gt;‘Just &lt;strong&gt;Muggle-baiting&lt;/strong&gt;,’ sighed Mr Weasley.&lt;/p&gt;

&lt;p&gt;Worms make excellent fish &lt;strong&gt;bait&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The open purse was a &lt;strong&gt;bait&lt;/strong&gt; for the hungry young man.&lt;/p&gt;

&lt;h2 id=&quot;undaunted&quot;&gt;undaunted&lt;/h2&gt;

&lt;p&gt;顽强的，无畏的&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Undaunted&lt;/strong&gt;, Hermione shoved her hand back into her bag and pulled out what appeared to be a bright red eraser.&lt;/p&gt;

&lt;h2 id=&quot;abashed&quot;&gt;abashed&lt;/h2&gt;

&lt;p&gt;难堪的，为难的，局促不安的&lt;/p&gt;

&lt;p&gt;… said Harry under his breath, pleased to see Draco looking both &lt;strong&gt;abashed&lt;/strong&gt; and angry.&lt;/p&gt;

&lt;h2 id=&quot;remnants&quot;&gt;remnants&lt;/h2&gt;

&lt;p&gt;They had been in the changing room so long that the sun was up properly now, although &lt;strong&gt;remnants&lt;/strong&gt; of mist hung over the grass in the stadium.&lt;/p&gt;

&lt;h2 id=&quot;wheezy&quot;&gt;wheezy&lt;/h2&gt;

&lt;p&gt;喘息的，吱吱响的&lt;/p&gt;

&lt;p&gt;‘My subject is History of Magic,’ he said in his dry, &lt;strong&gt;wheezy&lt;/strong&gt; voice.&lt;/p&gt;

&lt;h2 id=&quot;divination&quot;&gt;divination&lt;/h2&gt;

&lt;p&gt;占卜术&lt;/p&gt;

&lt;p&gt;It’s never too early to think about the future, so I’d recommend &lt;strong&gt;Divination&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;vulture&quot;&gt;vulture&lt;/h2&gt;

&lt;p&gt;秃鹫&lt;/p&gt;

&lt;p&gt;Madam Pince, the librarian, was a thin, irritable woman who looked like an underfed &lt;strong&gt;vulture&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;prop&quot;&gt;prop&lt;/h2&gt;

&lt;p&gt;支撑；支持&lt;/p&gt;

&lt;p&gt;Harry and Ron sat down at the Gryffindor table next to Hermione, who had her copy of Voyages with Vampires &lt;strong&gt;propped&lt;/strong&gt; open against a milk jug.&lt;/p&gt;

&lt;h2 id=&quot;throb&quot;&gt;throb&lt;/h2&gt;

&lt;p&gt;抽动，抽痛；跳动&lt;/p&gt;

&lt;p&gt;Steam was billowing from under the crumpled bonnet; Hedwig was shrieking in terror, a golf-ball-sized lump was &lt;strong&gt;throbbing&lt;/strong&gt; on Harry’s head where he had hit (the tree).&lt;/p&gt;

&lt;h2 id=&quot;wary&quot;&gt;wary&lt;/h2&gt;

&lt;p&gt;小心的，留神的，谨慎的&lt;/p&gt;

&lt;p&gt;He cast a &lt;strong&gt;wary&lt;/strong&gt; eye around the sleeping witches and wizards on the walls.&lt;/p&gt;

&lt;h2 id=&quot;plaque&quot;&gt;plaque&lt;/h2&gt;

&lt;p&gt;饰板，匾&lt;/p&gt;

&lt;p&gt;… a small &lt;strong&gt;plaque&lt;/strong&gt; on it, saying ‘Ronald’s room’.&lt;/p&gt;

&lt;h2 id=&quot;custard&quot;&gt;custard&lt;/h2&gt;

&lt;p&gt;牛奶蛋糊&lt;/p&gt;

&lt;p&gt;Come, Severus, there’s a delicious-looking &lt;strong&gt;custard tart&lt;/strong&gt; I want to sample.&lt;/p&gt;

&lt;h2 id=&quot;scalp&quot;&gt;scalp&lt;/h2&gt;

&lt;p&gt;头皮&lt;/p&gt;

&lt;p&gt;… it lifted the hair on Harry’s &lt;strong&gt;scalp&lt;/strong&gt; and made his heart feel as though it was swelling to twice its normal size.&lt;/p&gt;

&lt;h2 id=&quot;tingle&quot;&gt;tingle&lt;/h2&gt;

&lt;p&gt;感到刺痛&lt;/p&gt;

&lt;p&gt;Every nerve in Harry’s body was &lt;strong&gt;tingling&lt;/strong&gt; unpleasantly.&lt;/p&gt;

&lt;h2 id=&quot;dregs&quot;&gt;dregs&lt;/h2&gt;

&lt;p&gt;【用作复数】渣滓，糟粕&lt;/p&gt;

&lt;p&gt;the dregs of society&lt;/p&gt;

&lt;p&gt;If he knew what he means to us, to the lowly, the enslaved, us &lt;strong&gt;dregs&lt;/strong&gt; of the magical world!&lt;/p&gt;

&lt;h2 id=&quot;cub&quot;&gt;cub&lt;/h2&gt;

&lt;p&gt;幼兽&lt;/p&gt;

&lt;p&gt;(Tom Riddle) parentless but so brave, school prefect, model student; on the other hand, big, &lt;strong&gt;blundering&lt;/strong&gt; Hagrid, in trouble every other week, trying to raise werewolf &lt;strong&gt;cubs&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;blundering&quot;&gt;blundering&lt;/h2&gt;

&lt;p&gt;笨拙的，愚蠢的&lt;/p&gt;

</description>
        <pubDate>Mon, 01 Apr 2019 00:00:00 +0000</pubDate>
        <link>http://viaductooor.github.io/blog/2019/04/01/Learn-English-Harry-Potter-II/</link>
        <guid isPermaLink="true">http://viaductooor.github.io/blog/2019/04/01/Learn-English-Harry-Potter-II/</guid>
        
        <category>english</category>
        
        
        <category>private</category>
        
      </item>
    
  </channel>
</rss>
