并行还是并发?

并发:同一时间应对多件事情的能力

并行:同一时间动手做多件事情的能力

并发与并行

Read more »

代理模式

动机

在某些情况下,一个客户不想或者不能直接引用一个对象,此时可以通过一个称之为“代理”的第三者来实现 间接引用。代理对象可以在客户端和目标对象之间起到 中介的作用,并且可以通过代理对象去掉客户不能看到的内容和服务或者添加客户需要的额外服务

Read more »

概述

Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs fromHashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return trueimmediately prior to the invocation.)

Read more »

ConcurrentMap接口说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public interface ConcurrentMap<K, V> extends Map<K, V> {
V putIfAbsent(K key, V value); // 如果指定key没有与任何值绑定,则它与value绑定
/* 等同于
if (!map.containsKey(key))
return map.put(key, value);
else
return map.get(key);
*/
boolean remove(Object key, Object value); // 只有当该key与value相映射时候删除掉该元素
/* 等同于
if (map.containsKey(key) && map.get(key).equals(value)) {
map.remove(key);
return true;
} else return false;
*/
boolean replace(K key, V oldValue, V newValue); // 当key的值为oldValue时用newValue代替
/* 等同于
if (map.containsKey(key) && map.get(key).equals(oldValue)) {
map.put(key, newValue);
return true;
} else return false;
*/
V replace(K key, V value); // 当map中包含有key时用value代替
/* 等同于
if (map.containsKey(key)) {
return map.put(key, value);
} else return null;
*/
}
Read more »

63章 其他备选的 IO 模型

整体概览

实际上 I/O 多路复用,信号驱动 I/O 以及 epool 都是用来实现同一个目标的技术——同时检查多个文件描述符,看它们是否准备好了执行 I/O 操作(准确地说,是看 I/O 系统调用是否可以非阻塞地执行)。文件描述符就绪状态的转化是通过一些 I/O 事件来触发的,比如输入数据到达,套接字连接建立完成,或者是之前满载的套接字发送缓冲区在 TCP 将队列中的数据传送到对端之后由了剩余空间。同时检查多个文件描述符在类似网络服务器的应用中很有用处,或者是那么必须同事检查终端以及管道或套接字输入的应用程序。

需要注意的是这些技术都不会执行实际的 I/O 操作。它们只是告诉我们某个文件描述符已经处于就绪状态了,这时需要调用其他的系统调用来完成实际的 I/O 操作。

Read more »

1
2
3
4
5
6
7
8

// 常量列表
DEFAULT_INITIAL_CAPACITY = 1 << 4; //The capacity is the number of buckets in the hash table
MAXIMUM_CAPACITY = 1 << 30;
/*
**The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.
*/
DEFAULT_LOAD_FACTOR = 0.75f; //填充因子
Read more »

本篇是个人动态规划笔记。

应用动态规划方法:

  1. 刻画一个最优解的结构特征
  2. 递归地定义最优解的值
  3. 计算最优解的值,通常采用自底向上的方法
  4. 利用计算出的信息构造一个最优解
Read more »
0%