[update] java12-example 示例完成
java13-example 示例完成
This commit is contained in:
26
demo2/java13-example/README.md
Normal file
26
demo2/java13-example/README.md
Normal file
@@ -0,0 +1,26 @@
|
||||
# Java 13 新特性示例
|
||||
|
||||
本项目展示了Java 13的核心新特性及代码示例。
|
||||
|
||||
## 特性列表
|
||||
|
||||
### 1. 文本块(Text Blocks)- 预览功能
|
||||
- **说明**:使用`"""`定义多行字符串,避免转义字符,提高可读性
|
||||
- **示例位置**:`src/main/java/features/TextBlocksExample.java`
|
||||
|
||||
### 2. Switch表达式增强 - 预览功能
|
||||
- **说明**:JDK 12预览功能的改进版本,支持yield返回值
|
||||
- **示例位置**:`src/main/java/features/SwitchExpressionsExample.java`
|
||||
- **启用方式**:编译/运行时需添加`--enable-preview`
|
||||
|
||||
### 3. Socket API 重新实现
|
||||
- **说明**:使用NIO实现更简单、更维护的Socket API
|
||||
- **示例位置**:`src/main/java/features/SocketAPIExample.java`
|
||||
|
||||
### 4. ZGC(Z Garbage Collector)增强
|
||||
- **说明**:将ZGC的使用范围从Linux扩展到macOS和Windows(实验性)
|
||||
- **启用方式**:JVM参数`-XX:+UseZGC`
|
||||
|
||||
### 5. 动态CDS Archives
|
||||
- **说明**:简化了类数据共享的使用,提高启动性能
|
||||
- **使用方式**:通过`-XX:ArchiveClassesAtExit`和`-XX:SharedArchiveFile`参数使用
|
||||
@@ -24,6 +24,17 @@
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
<compilerArgs>
|
||||
<arg>--enable-preview</arg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.0.0-M7</version>
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
27
demo2/java13-example/src/main/java/Java13Main.java
Normal file
27
demo2/java13-example/src/main/java/Java13Main.java
Normal file
@@ -0,0 +1,27 @@
|
||||
import features.DynamicCDSExample;
|
||||
import features.SocketAPIExample;
|
||||
import features.SwitchExpressionsExample;
|
||||
import features.TextBlocksExample;
|
||||
import features.ZGCExample;
|
||||
|
||||
/**
|
||||
* @author maxiaofeng
|
||||
* @date 2025/7/18 10:06
|
||||
*/
|
||||
public class Java13Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Java 13 新特性示例");
|
||||
System.out.println("预览功能需要添加 --enable-preview 参数才能运行");
|
||||
System.out.println("==================");
|
||||
System.out.println("1. Text Blocks - 文本块");
|
||||
TextBlocksExample.exec();
|
||||
System.out.println("2. Switch Expressions - Switch表达式");
|
||||
SwitchExpressionsExample.exec();
|
||||
System.out.println("3. Socket API - Socket API重新实现");
|
||||
SocketAPIExample.exec();
|
||||
System.out.println("4. ZGC - Z垃圾收集器增强");
|
||||
ZGCExample.exec();
|
||||
System.out.println("5. Dynamic CDS Archives - 动态类数据共享");
|
||||
DynamicCDSExample.exec();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
/**
|
||||
* @author maxiaofeng
|
||||
* @date 2025/7/18 10:06
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("hello java8.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package features;
|
||||
|
||||
public class DynamicCDSExample {
|
||||
public static void exec() {
|
||||
System.out.println("Dynamic CDS Archives 示例");
|
||||
System.out.println("类数据共享(CDS)可以提高JVM启动速度和减少内存使用");
|
||||
System.out.println();
|
||||
System.out.println("Java 13 增强:");
|
||||
System.out.println("- 简化了CDS的使用");
|
||||
System.out.println("- 支持动态创建归档文件");
|
||||
System.out.println();
|
||||
System.out.println("使用方式:");
|
||||
System.out.println("1. 运行应用时创建归档文件:");
|
||||
System.out.println(" java -XX:ArchiveClassesAtExit=my_archive.jsa MyApplication");
|
||||
System.out.println("2. 使用归档文件运行应用:");
|
||||
System.out.println(" java -XX:SharedArchiveFile=my_archive.jsa MyApplication");
|
||||
System.out.println();
|
||||
System.out.println("优势:");
|
||||
System.out.println("- 减少启动时间");
|
||||
System.out.println("- 减少内存占用");
|
||||
System.out.println("- 多个JVM实例可以共享只读类数据");
|
||||
|
||||
// 演示加载一些常用类
|
||||
try {
|
||||
Class<?> stringClass = Class.forName("java.lang.String");
|
||||
Class<?> listClass = Class.forName("java.util.List");
|
||||
Class<?> hashMapClass = Class.forName("java.util.HashMap");
|
||||
|
||||
System.out.println("\n加载的类:");
|
||||
System.out.println("- " + stringClass.getName());
|
||||
System.out.println("- " + listClass.getName());
|
||||
System.out.println("- " + hashMapClass.getName());
|
||||
} catch (ClassNotFoundException e) {
|
||||
System.out.println("类加载异常: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package features;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.StandardSocketOptions;
|
||||
import java.nio.channels.ServerSocketChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
|
||||
public class SocketAPIExample {
|
||||
public static void exec() {
|
||||
try {
|
||||
// 创建服务器Socket通道 (使用NIO实现)
|
||||
ServerSocketChannel serverSocket = ServerSocketChannel.open();
|
||||
serverSocket.bind(new InetSocketAddress(8080));
|
||||
serverSocket.setOption(StandardSocketOptions.SO_REUSEADDR, true);
|
||||
|
||||
System.out.println("服务器启动在端口 8080");
|
||||
|
||||
// 演示新API - 获取本地地址
|
||||
System.out.println("服务器地址: " + serverSocket.getLocalAddress());
|
||||
|
||||
// 模拟客户端连接
|
||||
new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(1000); // 等待服务器启动
|
||||
SocketChannel client = SocketChannel.open();
|
||||
client.connect(new InetSocketAddress("localhost", 8080));
|
||||
System.out.println("客户端连接成功");
|
||||
client.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}).start();
|
||||
|
||||
// 接受连接
|
||||
SocketChannel clientSocket = serverSocket.accept();
|
||||
System.out.println("接受客户端连接: " + clientSocket.getRemoteAddress());
|
||||
|
||||
// 关闭连接
|
||||
clientSocket.close();
|
||||
serverSocket.close();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package features;
|
||||
|
||||
public class SwitchExpressionsExample {
|
||||
public static void exec() {
|
||||
// 传统switch语句
|
||||
int day = 3;
|
||||
String dayType;
|
||||
switch (day) {
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
case 4:
|
||||
case 5:
|
||||
dayType = "Weekday";
|
||||
break;
|
||||
case 6:
|
||||
case 7:
|
||||
dayType = "Weekend";
|
||||
break;
|
||||
default:
|
||||
dayType = "Invalid";
|
||||
}
|
||||
System.out.println("传统switch: " + dayType);
|
||||
|
||||
// Java 13 switch表达式 (需要--enable-preview参数)
|
||||
String result = switch (day) {
|
||||
case 1, 2, 3, 4, 5 -> "Weekday";
|
||||
case 6, 7 -> "Weekend";
|
||||
default -> "Invalid";
|
||||
};
|
||||
System.out.println("Switch表达式: " + result);
|
||||
|
||||
// 使用yield返回值 (需要--enable-preview参数)
|
||||
String dayName = switch (day) {
|
||||
case 1: yield "Monday";
|
||||
case 2: yield "Tuesday";
|
||||
case 3: yield "Wednesday";
|
||||
case 4: yield "Thursday";
|
||||
case 5: yield "Friday";
|
||||
case 6: yield "Saturday";
|
||||
case 7: yield "Sunday";
|
||||
default: yield "Invalid day";
|
||||
};
|
||||
System.out.println("Day name: " + dayName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package features;
|
||||
|
||||
public class TextBlocksExample {
|
||||
public static void exec() {
|
||||
// 传统方式定义多行字符串
|
||||
String jsonOld = "{\n" +
|
||||
" \"name\": \"Java 13\",\n" +
|
||||
" \"version\": 13,\n" +
|
||||
" \"preview\": true\n" +
|
||||
"}";
|
||||
|
||||
// 使用文本块(预览功能)
|
||||
String json = """
|
||||
{
|
||||
"name": "Java 13",
|
||||
"version": 13,
|
||||
"preview": true
|
||||
}
|
||||
""";
|
||||
|
||||
System.out.println("传统方式:");
|
||||
System.out.println(jsonOld);
|
||||
System.out.println("\n文本块方式:");
|
||||
System.out.println(json);
|
||||
|
||||
// 文本块的其他用法
|
||||
String html = """
|
||||
<html>
|
||||
<body>
|
||||
<p>Hello, Java 13!</p>
|
||||
</body>
|
||||
</html>
|
||||
""";
|
||||
|
||||
System.out.println("\nHTML示例:");
|
||||
System.out.println(html);
|
||||
}
|
||||
}
|
||||
32
demo2/java13-example/src/main/java/features/ZGCExample.java
Normal file
32
demo2/java13-example/src/main/java/features/ZGCExample.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package features;
|
||||
|
||||
public class ZGCExample {
|
||||
public static void exec() {
|
||||
System.out.println("ZGC (Z Garbage Collector) 示例");
|
||||
System.out.println("ZGC是为低延迟应用设计的垃圾收集器");
|
||||
System.out.println("特点:");
|
||||
System.out.println("1. 暂停时间不超过10ms");
|
||||
System.out.println("2. 暂停时间不会随堆大小或活跃对象数量增加而增加");
|
||||
System.out.println("3. 支持TB级别的堆大小");
|
||||
System.out.println();
|
||||
System.out.println("使用方式:");
|
||||
System.out.println("在JVM启动参数中添加: -XX:+UseZGC");
|
||||
System.out.println();
|
||||
System.out.println("注意:");
|
||||
System.out.println("在Java 13中,ZGC在macOS和Windows上仍为实验性功能");
|
||||
System.out.println("生产环境建议在Linux上使用");
|
||||
|
||||
// 演示内存分配压力
|
||||
System.out.println("开始分配大对象...");
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
// 创建大对象以触发垃圾回收
|
||||
byte[] array = new byte[1024 * 1024]; // 1MB
|
||||
// 短暂使用后丢弃引用,使其可被回收
|
||||
}
|
||||
System.out.println("对象分配完成");
|
||||
|
||||
// 获取垃圾收集器信息
|
||||
String gcName = System.getProperty("java.vm.name");
|
||||
System.out.println("当前JVM: " + gcName);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user