j360-tools Java知识点持续更新
[![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
[![Build Status](https://travis-ci.org/xuminwlt/j360-tools.svg?branch=master)](https://travis-ci.org/xuminwlt/j360-tools)
**Java体系微服务架构和运维优化, 学而思, 思而学, 分享一下工作中使用的技术点, 会持续更新**
## 书单篇
<a href="https://github.com/xuminwlt/j360-book-list">我的书单: 2019夏至篇</a>
## 基础篇
1. <a href="#1.1">在String中使用KMP算法计算部分操作</a>
2. <a href="#1.1">JMH验证</a>
3. <a href="#1.3">线程池如何用好/shutdown pool时的规则, shutdown() && shutdownnow(), kill和钩子引用</a>
4. <a href="#1.4">二叉堆实现有序队列</a>
5. <a href="#1.5">解读ThreadLocal</a>
6. <a href="#1.6">Java中的排序场景,Collections.sort, TreeMap</a>
7. <a href="#1.7">线程生命周期各状态在jstack中的解读</a>
8. <a href="#1.8">String.intern, Long, Integer等对象池在jvm中使用</a>
9. <a href="#1.9">finalize,phantomReference使用</a>
10. <a href="#1.10">验证hash、一致性hash的分布(murmurhash)</a>
11. <a href="#1.11">算法:sort/search/rate limit/sliding window</a>
12. <a href="#1.12">索引,B-tree、invert index</a>
13. <a href="#1.13">对象头、指针、锁、类</a>
14. <a href="#1.14">SecureRandom seed相关</a>
15. <a href="#1.15">异常</a>
16. <a href="#1.16">异步 & Future, 并发异步读提升接口性能</a>
17. <a href="#1.17">ForkJoin工作原理</a>
18. <a href="#1.18">nio对照阻塞io写法,Selector工作原理</a>
## 提高篇
1. 事务,分布式事务,Innodb实现
- <a href="./docs/Transaction.md">事务,分布式事务,Innodb实现,分布式事务案例分析</a>
- <a href="./docs/Transaction-Fescar.md">分布式事务Alibaba Fescar实现</a>
2. 一致性hash分片、扩容与缩服
3. CAP
4. redis & cluster
5. JVM <a href="VmFlagsFinal.md">VMFlags</a>, Hotspot/JPDA/JVMTI相关 参考:
- https://www.ibm.com/developerworks/cn/java/j-lo-jpda3/index.html
- http://calvin1978.blogcn.com/articles/jvmoption-7.html
6. Netty/Selector在工程中的高效使用 <a href="./docs/netty.md">Netty案例</a>
7. <a href="./docs/log4j2.md">Log4j2的高效使用</a>
8. <a href="./docs/HikariCP.md">HikariCP 解读、译文</a>
9. Web服务会话管理实现
## 运维篇
1. coredump segmentfault https://www.cnblogs.com/lidabo/p/5014710.html
2. 句柄、TCP/内核相关调整 ulimit -n
3. netstat 状态解读
4. crontab 简单搞定定时器,备份日志、mysql、任务执行调度
5. Mysql连接池
6. Java服务化shell
7. 日志利器sed、awk
8. <a href="docs/nginx.md">Nginx常用配置及优化手段</a>
9. <a href="docs/redis.md">redis: master/slave,cluster</a>
## 常用中间件工具篇
1. elasticsearch
2. canal
3. flume/fluentd
4. <a href="./docs/TRACING.md">zipkin/brave/Skywalking</a>
5. Azkaban
6. Apollo
7. Zabbix/Kibana/Grafana
8. ActiveMQ
9. <a href="./docs/rocketmq.md">RocketMQ v4.3.0</a>
10. Dubbo
11. Sharding-Sphere
12. Saga <a href="https://github.com/apache/servicecomb-pack">servicecomb-pack</a>
13. Kafka
14. <a href="./docs/seata.md">Seata</a>
## 容器篇
1. docker/docker compose
2. k8s
3. istio
## 书单
https://github.com/xuminwlt/j360-book-list
## 业务篇
1. Timeline
2. 消息、推送系统
3. 用户
4. 支付
5. 计数器
## 问题排查篇
1. 慢接口
2. GC
3. Mysql死锁
## 开发小技巧篇
1. 分布式锁
2. Master-Slave
3. 限流
4. 热点数据
-------------------------------------------------------------------------------
基础篇
-------------------------------------------------------------------------------
### <a name="1.1">1. KMP算法+JMH验证</a>
```
# Measurement: 2 iterations, 1 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: me.j360.tools.ToolsBenchmark.indexOfKMPLongText
...
Result "me.j360.tools.ToolsBenchmark.indexOfStringLongText":
1171034.703 ops/ms
# Run complete. Total time: 00:00:18
Benchmark Mode Cnt Score Error Units
ToolsBenchmark.indexOfKMP thrpt 2 22165.252 ops/ms
ToolsBenchmark.indexOfKMPLongText thrpt 2 1343.742 ops/ms
ToolsBenchmark.indexOfString thrpt 2 2957587.750 ops/ms
ToolsBenchmark.indexOfStringLongText thrpt 2 1171034.703 ops/ms
```
### <a name="1.3">3. 理解线程池,用好线程池</a>
从使用到深入了解自定义线程池ThreadPoolExecutor,先了解最完整的构造方法参数,一共7个参数
```
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @param threadFactory the factory to use when the executor
* creates a new thread
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* @throws IllegalArgumentException if one of the following holds:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue}
* or {@code threadFactory} or {@code handler} is null
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
```
- 参数列表
核心线程数:默认参照cpu的数量
最大线程数:线程池所能容纳的最大线程数
空闲存活时间:非核心线程的空闲存活时间
时间单位
队列实例:任务队列,其中常用的有三种队列,SynchronousQueue,LinkedBlockingDeque,ArrayBlockingQueue
线程工厂:尽量自定义线程工厂,默认DefaultThreadFactory
拒绝策略:默认丢弃,选择jdk自带的四选一策略。
- 内部属性
ctl:记录每个线程的状态字段,使用29+3位的Integer类型记录,当数量更大时,未来会考虑使用AtomicLong类型
- 参数大小特别说明
1. ** 当线程池小于corePoolSize时,新提交任务将创建一个新线程执行任务,即使此时线程池中存在空闲线程。 **
2. 当线程池达到corePoolSize时,新提交任务将被放入workQueue中,等待线程池中�