每周写一个 ARTS:Algorithm 是一道算法题,Review 是读一篇英文文章,Technique/Tips 是分享一个小技术,Share 是分享一个观点。
https://leetcode-cn.com/problems/reverse-linked-list/ 反转链表
https://leetcode-cn.com/problems/swap-nodes-in-pairs/ 两两反转链表
https://leetcode-cn.com/problems/linked-list-cycle/ 环形链表
https://leetcode-cn.com/problems/linked-list-cycle-ii/ 环形链表 2
总结一下,链表的题最容易出 NullPointException 了,遍历时的条件比较难确定。
写完链表相关的题,想想能不能过一下的测试用例
This is part 1 of 2 articles explaining the basic concepts of REST.
What you should know before reading this article:
You should have some understanding of what is HTTP and what is an API.
REST is an architectural style(建筑风格 - 编码规范?), or design pattern, for APIs.
Who invented REST?
REST was defined by Roy Fielding, a computer scientist. He presented the REST principles in his PhD (博士 论文) in 2000.
Before we dive(潜水) into(深入) what makes an API RESTful and what** constraints (约束)**and rules you should follow if you want to create RESTful APIs, let’s explain 2 key terms(术语):
Now let’s get back to REST.
A RESTful web application exposes information about itself in the form of information about its resources. It also enables the client to take actions on those resources, such as create new resources (i.e. create a new user) or change existing resources (i.e. edit a post).
In order for your APIs to be RESTful, you have to follow a set of **constraints **when you write them. The REST set of constraints will make your APIs easier to use and also easier to discover, meaning a developer who is just starting to use your APIs will have an easier time learning how to do so.(易用,易读,易理解)
REST stands for REpresentational State Transfer(代表性的状态转移).
It means when a RESTful API is called, the server will transfer to the client a representation of the state of the requested resource.
服务端会响应客户端请求的资源,转换资源对于的状态
For example, when a developer calls Instagram API to fetch a specific user (the resource), the API will return the state of that user, including their name, the number of posts that user posted on Instagram so far, how many followers they have, and more.
The representation of the state can be in a JSON format, and probably for most APIs this is indeed the case. It can also be in XML or HTML format.
资源的状态一般是 JSON 格式,也可以是 xml 或者 html 形式。
What the server does when you, the client, call one of its APIs depends on 2 things that you need to provide to the server:
For example, fetching a specific Twitter user, using Twitter’s RESTful API, will require a URL that identify that user and the HTTP method GET.
Another example, this URL: www.twitter.com/jk_rowling has the unique identifier for J. K. Rowling’s Twitter user, which is her username, jk_rowling. Twitter uses the username as the identifier, and indeed Twitter usernames are unique — there are no 2 Twitter users with the same username.
The HTTP method GET indicates that we want to get the state of that user.
Continue to Part 2 to learn about the 6 REST constraints.
RESTful 是一中编程风格,具有 RESTful 风格的 API 可读性更好,对于开发者也更容易维护。
那么如何定位服务器上的资源状态呢?
In computer science, a pool is a collection of resources that are kept[clarification needed] ready to use, rather than acquired on use and released[clarification needed] afterwards.
通过维护一个池资源,提供
当然维护这么一个池也是需要消耗资源和复杂的管理工作的,只是频繁的创建销毁资源,使用池是更加高效的。对于计算机的资源,大家都是非常敏感的,引入的各种优化手段,也都是为了最大化地利用资源,个人认为 **Pool **也算是一种优化手段。
和普通的池化资源不同,Java 的线程池采用的是 生产者-消费者模型,线程池扮演生产者的角色,为任务创建线程。待执行的任务提交给线程池,消耗(占用)线程池中的资源(线程)。
通过 ThreadPoolExecutor 创建一个线程池,先看一下其参数最全的构造方法。
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
重点看一下队列和拒绝策略的选择
线程池的服务对象是未来会提交的任务,合理配置线程池的目的也是提高执行任务的效率。
不同的任务类型需要不同配置的线程池,这也就取决于任务的性质,故我们来分析一下任务的性质
重点分析一下 CPU 和 I/O 密集型任务如何配置线程数:
CPU 密集型的任务,就是说大部分时间都在进行 CPU 计算,而很少的等待其他必要条件,例如等待 I/O。那么对于一个 8 核的 CPU 来说,创建 8 个线程来利用每个核去进行 CPU 计算(而不会因为等待 I/O 让 CPU 处于空闲状态,最大化利用 CPU 资源)。这种情况下创建过多线程只会增加上下文切换的成本。实际上一般会把线程数设置为 CPU 核数 + 1,多的一个线程来应对意外状况。
I/O 密集型的任务,线程就可能频繁让出 CPU 资源去等待 I/O,所以 CPU 处于空闲状态下的概率就会比较大。那么就需要创建比较多的线程去填补这个空闲时间,就可能提高 CPU 的利用率。一般把线程数设置为 2 * CPU 核数。
(参考内容《Java 并发编程的艺术》《Java 并发编程实战》)
因为本人是个程序员,所以先推荐一个我频繁使用的知识付费产品 “极客时间 App”。
知识付费好像是最近逐渐火起来的概念,其中我认为的标志性事件和产品 “得到 App”。
知识付费,就是人们付费,直接获取互联网上的优质内容,减少筛选的时间。因为互联网上的资料参差不齐,虽然其中不乏有优质内容,但是筛选需要花太多时间了。
这里我们讨论的肯定都是优质内容,那我们怎么判断内容好坏呢?
对于生产者:
对于消费者: