Java 简史

Java 8、9、10的新特性

Java 10

JDK 10 发布于 2018年3月20日。主要更新是局部变量推断和垃圾回收及编译算法提高。 JDK 10是个短期的版本,将要于今天九月份发布的JDK 11是个LTS版本。

主要特点:

  • Local variable type inference: 使用初始化语句推段变量类型
  • G1 支持并行化: 提高 worst-case 延迟
  • 优化 class-data sharing
  • 试验版just-in-time compier:Graal
  • Docker awareness:在linux 平台,JVM可以知道 Container-specifi-information
  • 三个新的JVM参数,用户Docker可以控制系统内存
  • jShell REPL tool 启动优化
  • 不可变集合实例化:Set.copyof,Map.copyOf,Collectors类的新方法:toUnmodifiableList/toUnmodifiableSet/toUnmodifableMap
  • GC 接口更新,用于更方便地增加新的collector
  • G1并行化
  • HotSpot支持把对象堆分配到指定的内存设备上
  • Grall
  • JDK 代码库整理
  • application class-data sharing
  • thread-local handshakes?
  • 默认官方根证书

Oracle在下个版本中声明的特性:

Java 9

Java 9发布于2017年9月21日。最重要的更新是Java平台模块系统的引入
IBM Blog

Java平台模块系统

  • Project Jigsaw把模块化开发实践引入到了Java平台
  • 通过jlink工具,创建出只包含所依赖的JDK模块的自定义运行时镜像
  • 模块的重要特征是其工件(artifact)的根目录中包含了一个描述模块的module-info.class 文件(由module-info.java编译),模块的内容包括:
    • 模块导出的包
    • 模块的依赖关系 requires or requires transitive
    • 服务的提供和使用 ServiceLocator or uses or provides with
    • getModule()
  • 工件的格式是JAR文件或者JMOD文件
  • JVM提供了相应的API和模块系统进行交互
1
2
3
4
5
6
module com.mycompany.sample { //模块声明定义
exports com.mycompany.sample;
requires com.mycompany.common;
provides com.mycompany.common.DemoService with
com.mycompany.sample.DemoServiceImpl;
}

Jshell

Jshell提供的功能类似于NodeJS和Python中的Read-Evaluation-PrintLoop 功能。

  • 基本的表达式计算
  • 创建Java 类和方法
  • 执行代码片段
  • 详细介绍请执行 jshell => /help /list

集合、Stream、Optional、进程

  • List.of()\Set.of()\Map.of()\Map.ofEntries()等工厂方法返回不可变集合
  • Stream新增API:filtering、flatMapping todo
  • Optional 类新增API: ifPresentOrElse、or、stream
  • 进程API:接口ProcessHandle可以对原生进程进行管理
  • 平台日志API:System.LoggerFinder 来管理日志记录器实现

    1
    2
    3
    4
    5
    6
    public class Main { 
    private static final System.Logger LOGGER = System.getLogger("Main");
    public static void main(final String[] args) {
    LOGGER.log(Level.INFO, "Run!");
    }
    }
  • Reactive Streams: java.util.concurrent.Flow, 核心接口:

    • Flow.Publisher
    • Flow.Subscriber
    • Flow.Subscription
    • Flow.Processor
  • 变量句柄: VariableHandler java.lang.invoke.VarHandle
  • 改进方法句柄: java.lang.MethodHandles
  • CompletableFuture orTimeout
  • Java8引入的JS引擎Nashorn增强:实现了ES6的新特性
  • IO: readAllBytes,readNBytes,transferTo
  • 新增SHA-3 哈希算法,java.security.SecureRandom生成使用DRBG算法的强随机数
1
2
3
4
5
6
7
8
import org.apache.commons.codec.binary.Hex; 
public class SHA3 {
    public static void main(final String[] args) throws NoSuchAlgorithmException {
        final MessageDigest instance = MessageDigest.getInstance("SHA3-224");
        final byte[] digest = instance.digest("".getBytes());
        System.out.println(Hex.encodeHexString(digest));
    }
}
  • 统一JVM日志: -Xlog控制JVM日志输出的标签、级别、修饰符、输出目标
  • 新增jcmd调用的诊断命令
  • 接口支持私有方法

Java 8

Java 8 发布于2014年3月18日。

  • 函数式接口: @FunctionalInterface
  • Lambda表达式
  • 接口增强:
    • default 修饰的非抽象方法:可以在子类中直接使用
    • 增加静态方法,可以直接通过接口调用
  • Streaming API:实现集合的并行处理和函数式操作,根据操作返回结果的不同,流式操作分为中间操作和最终操作,前者返回流本身,后者返回一特定类型的结果,实现了集合的过滤、排序、映射等

    • stream.sequential() 返回串行流
    • int count= (int) ((Stream) list.stream().sequential()).sorted().count();

    • stream.parallel() 返回并行流

    • int count = (int)((Stream) list.stream().parallel()).sorted().count();
    • 中间操作:filter/sorted/map/distinct/subStream list.stream() .filter((s) -> s.startsWith("s")) .forEach(System.out::println);
    • 终止操作:forEach/toArray/findFirst/anyMatch
  • 注解的更新:
    • 增加了类型注解:ElementType.TYPE_USE/ElementType.TPYE_PARAMETER
    • 使用Checker Framework等第三方工具在编译期检查出运行时错误
    • 支持重复注解:@Repeatable(XX.class)
  • 安全增强:
    • 基于AES的加密算法:PEBWithSHA256AndAES_128
    • 客户端默认启动TLS1.2和TLS1.1、系统属性jdk.tls.client.protocols
    • Keystore增强
    • 支持安全的随机数生成器:SecureRandom.getInstanceStrong
    • JSSE服务端支持SNI
    • 默认禁用安全性差的加密算法:例如DES相关的Kerberos5
  • NIO改进:
    • 改进了java.nio.charset.Charset实现
    • 优化了String.getBytes性能
    • 新增API:BufferedReader.line()/File.lines(Path,Charset)/File.list(Path)/File.walk(Path,int,FileVisitOption)/Find.find(…)
  • 全球化功能:
    • 支持新的Unicode 6.2.0标准
    • 新增日历和本地化API
    • 改进了日期时间的管理
  • IDE:
    • Eclipse Kepler 4.3.2 默认支持Java8

即时通讯框架T-IO学习

t-io学习笔记

常见应用场景

  • IM(官方提供了im例子,含web端)
  • 实时监控
  • 推送服务(已内置API)
  • RPC
  • 游戏
  • 物联网(已有很多案例)
  • 其它实时通讯类型的场景,不一一列举

用t-io做的web im

maven坐标

1
2
3
4
5
<dependency>
<groupId>org.t-io</groupId>
<artifactId>tio-core</artifactId>
<version>1.7.2.v20170705-RELEASE</version>
</dependency>

对开发人员极体贴的内置功能

  • 内置心跳检测
  • 内置心跳发送
  • 各种便捷的绑定API
  • 各种便捷的发送API
  • 一行代码拥有自动重连功能
  • 各项消息统计等功能,全部一键内置搞定,省却各种烦恼

各种传送门

了解代码目录结构

所有工程都是maven工程,后续目录有可能稍有变动,不定期更新

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
├─bin----------------脚本目录(方便快速操作)
│ clean.bat----------------清空所有工程的target目录
│ clean.sh
│ deploy.bat----------------作者用来发布到maven中心仓库的脚本,放出来主要是供大家参考
│ deploy.sh
│ dist-examples.bat----------------把所有的例子打包到dist目录,方便用户直接执行
│ dist-examples.sh
│ install.bat----------------安装工程到本地仓库
│ install.sh
| start-http-server.bat----------------启动tio-httpserver
│ start-helloworld-client.bat----------------启动helloworld的客户端
│ start-helloworld-client.sh
│ start-helloworld-server.bat----------------启动helloworld的服务端
│ start-helloworld-server.sh
│ start-im-client.bat----------------启动im的客户端
│ start-im-client.sh
│ start-im-server.bat----------------启动im的服务端
│ start-im-server.sh
│ start-showcase-client.bat----------------启动showcase的客户端
│ start-showcase-client.sh
│ start-showcase-server.bat----------------启动showcase的服务端
│ start-showcase-server.sh
├─docs
│ │
│ ├─blog----------------本人博客草稿(大部分博客是在线编辑,所以此处就没有了)
│ │
│ ├─performance----------------一些性能测试截图(随着版本的增多,有些截图已经过时,但仍保留)
│ │
│ ├─release----------------新版本发布时的log

├─dist----------------成品
│ └─examples----------------用t-io写的例子成品
│ ├─helloworld
│ │ ├─client----------------helloworld的客户端
│ │ └─server----------------helloworld的服务端
│ ├─im
│ │ ├─client----------------im的客户端
│ │ └─server----------------im的服务端
│ └─showcase
│ ├─client----------------showcase的客户端
│ └─server----------------showcase的服务端
└─src
├─core----------------t-io的核心代码
├─zoo----------------t-io的生态圈
│ ├─http----------------用t-io实现的http服务器
│ ├─websocket----------------用t-io实现的websocket服务器
├─example----------------用t-io写的例子的源代码
│ ├─parent----------------例子的maven parent
│ ├─helloworld----------------helloworld的源代码
│ │ ├─client
│ │ ├─common
│ │ └─server
│ ├─im----------------im的源代码
│ │ ├─client
│ │ ├─common
│ │ └─server
│ └─showcase----------------showcase的源代码,这个例子是为了帮助用户学习t-io专门写的
│ ├─client
│ ├─common
│ └─server
└─parent----------------maven工程的parent

学习万能的helloworld例子

花30分钟看一下t-io官方提供的helloworld,了解一下TCP编程的大概流程,文档传送门: t-io的hello world

学习用于进阶的showcase例子

showcase一词是从springside借来的,放这很应景,天蓬元帅就是这样学习的,可以和他交流,他后面会出详细的教程。

InAction

  • 执行测试用例失败: mvn -Dmaven.test.skip=false -Dtest=DemoTest -DfailIfNoTests=false test

HuTool

提供常用的工具,设计哲学:

  • 方法优于对象,使用静态方法,无需创建对象
  • 自动识别优于用户定义
  • 适配器模式: 最大范围的做到兼容
  • 可选依赖: jar都是optional的,使用者需要自己添加依赖

常用工具推荐

  • WatchMonitor:跟踪一个文件,监听其变化
  • 配置文件加载工具:ClassPathResource

列一下推荐的国产开源软件

  1. https://www.oschina.net/p/weixin-java-tools-new (使用一年)
  2. http://layim.layui.com(2017年5月11号开始使用)
  3. https://www.oschina.net/p/ztree (使用五年以上吧)
  4. https://www.oschina.net/p/echarts (使用两年以上吧)
  5. http://git.oschina.net/tywo45/talent-validate (使用十年了,开源出来有五年以上吧,原来是博客开源,现在移到开源中国了)
  6. https://www.oschina.net/p/hutool(懒 人必备,强烈推荐,使用两个月)
  7. https://www.oschina.net/p/t-io(使用五年了,开源出来半年)
  8. https://www.oschina.net/p/druid(使用三年以上吧)
  9. https://www.oschina.net/p/dubbo(使用两年)
  10. https://git.oschina.net/jfinal/jfinal-weixin(使用一年左右)
  11. https://www.oschina.net/p/fastjson(使用三年以上)