feat: JMHExample01
This commit is contained in:
parent
e90294d7d6
commit
823db4204d
|
@ -1,6 +1,12 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="Encoding">
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/multithreading1/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/multithreading1/src/main/resources" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/multithreading1/src/main/resources-filtered" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/multithreading_init/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/multithreading_init/src/main/resources" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/multithreading_init/src/main/resources-filtered" charset="UTF-8" />
|
||||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
<file url="file://$PROJECT_DIR$/src/main/resources-filtered" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/src/main/resources-filtered" charset="UTF-8" />
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>cn.bunny</groupId>
|
||||||
|
<artifactId>MultiThread</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>multithreading1</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>multithreading1</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
|
@ -0,0 +1,53 @@
|
||||||
|
package cn.bunny;
|
||||||
|
|
||||||
|
import org.openjdk.jmh.annotations.*;
|
||||||
|
import org.openjdk.jmh.runner.Runner;
|
||||||
|
import org.openjdk.jmh.runner.RunnerException;
|
||||||
|
import org.openjdk.jmh.runner.options.Options;
|
||||||
|
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
// Benchmark Mode Cnt Score Error Units
|
||||||
|
// JMHExample01.arrayListAdd avgt 10 0.085 ± 0.364 us/op
|
||||||
|
// JMHExample01.linkedListAdd avgt 10 0.007 ± 0.001 us/op
|
||||||
|
@BenchmarkMode({Mode.AverageTime})
|
||||||
|
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
||||||
|
@State(Scope.Thread)
|
||||||
|
@Measurement(iterations = 5)// 度量5个批次
|
||||||
|
@Warmup(iterations = 3)// 预热3个批次
|
||||||
|
public class JMHExample01 {
|
||||||
|
private final static String DATA = "DUMMY DATA";
|
||||||
|
|
||||||
|
private List<String> arrayList;
|
||||||
|
private List<String> linkedList;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws RunnerException {
|
||||||
|
final Options opt = new OptionsBuilder().include(JMHExample01.class.getSimpleName())
|
||||||
|
.forks(1)
|
||||||
|
.measurementIterations(10)// 度量执行的批次为10,在这个5个批次中对基准方法执行与调用将会纳入统计
|
||||||
|
.warmupIterations(10)// 在真正的度量之前,首先会对代码进行10个批次的预热,是代码运行达到JVM已经优化的效果
|
||||||
|
.build();
|
||||||
|
new Runner(opt).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Setup(Level.Iteration)
|
||||||
|
public void setUp() {
|
||||||
|
this.arrayList = new ArrayList<>();
|
||||||
|
this.linkedList = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public List<String> arrayListAdd() {
|
||||||
|
this.arrayList.add(DATA);
|
||||||
|
return this.arrayList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public List<String> linkedListAdd() {
|
||||||
|
this.linkedList.add(DATA);
|
||||||
|
return this.linkedList;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package cn.bunny;
|
||||||
|
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit test for simple App.
|
||||||
|
*/
|
||||||
|
public class AppTest
|
||||||
|
extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create the test case
|
||||||
|
*
|
||||||
|
* @param testName name of the test case
|
||||||
|
*/
|
||||||
|
public AppTest( String testName )
|
||||||
|
{
|
||||||
|
super( testName );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the suite of tests being tested
|
||||||
|
*/
|
||||||
|
public static Test suite()
|
||||||
|
{
|
||||||
|
return new TestSuite( AppTest.class );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rigourous Test :-)
|
||||||
|
*/
|
||||||
|
public void testApp()
|
||||||
|
{
|
||||||
|
assertTrue( true );
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>cn.bunny</groupId>
|
||||||
|
<artifactId>MultiThread</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>multithreading_init</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>multithreading_init</name>
|
||||||
|
<url>https://maven.apache.org</url>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
|
@ -0,0 +1,41 @@
|
||||||
|
package init;
|
||||||
|
|
||||||
|
public class DeadlockExample {
|
||||||
|
private static final Object lock1 = new Object();
|
||||||
|
private static final Object lock2 = new Object();
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Thread thread1 = new Thread(() -> {
|
||||||
|
synchronized (lock1) {
|
||||||
|
System.out.println("Thread 1: Holding lock 1...");
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
System.out.println("Thread 1: Waiting for lock 2...");
|
||||||
|
synchronized (lock2) {
|
||||||
|
System.out.println("Thread 1: Holding lock 1 and lock 2...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Thread thread2 = new Thread(() -> {
|
||||||
|
synchronized (lock2) {
|
||||||
|
System.out.println("Thread 2: Holding lock 2...");
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
System.out.println("Thread 2: Waiting for lock 1...");
|
||||||
|
synchronized (lock1) {
|
||||||
|
System.out.println("Thread 2: Holding lock 1 and lock 2...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
thread1.start();
|
||||||
|
thread2.start();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,89 @@
|
||||||
|
package subtitle;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class StarTrekWithDiscovery {
|
||||||
|
// 字幕文件目录
|
||||||
|
private static final Path SUBTITLE_PATH = Path.of("G:\\媒体\\电视剧\\星际迷航系列\\星际迷航:发现号系列\\字幕");
|
||||||
|
|
||||||
|
// 输出文件目录
|
||||||
|
private static final Path OUTPUT_PATH = Path.of("F:\\学习\\代码\\Java\\完成字幕");
|
||||||
|
|
||||||
|
// 匹配字幕中英文正则表达式
|
||||||
|
private static final Pattern LINE_PATTERN = Pattern.compile("Dialogue: .*,,(?<chinese>.*?)\\\\.*?}(?<english>.*)");
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
// 先删除所有文件
|
||||||
|
deleteFolder(new File(OUTPUT_PATH.toString()));
|
||||||
|
|
||||||
|
// 递归查找需要文件内容
|
||||||
|
try (Stream<Path> walk = Files.walk(SUBTITLE_PATH)) {
|
||||||
|
walk.filter(file -> file.getFileName().toString().endsWith("ass"))
|
||||||
|
.forEach(StarTrekWithDiscovery::processFile);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("Error processing files", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件进程
|
||||||
|
*
|
||||||
|
* @param file 文件
|
||||||
|
*/
|
||||||
|
private static void processFile(Path file) {
|
||||||
|
// 相对路径
|
||||||
|
Path relativePath = SUBTITLE_PATH.relativize(file.getParent());
|
||||||
|
|
||||||
|
// 输出文件目录
|
||||||
|
Path outputDir = OUTPUT_PATH.resolve(relativePath);
|
||||||
|
|
||||||
|
try (Stream<String> lines = Files.lines(file, StandardCharsets.UTF_8)) {
|
||||||
|
Files.createDirectories(outputDir);
|
||||||
|
|
||||||
|
String content = lines.map(line -> {
|
||||||
|
// 匹配需要的字幕内容
|
||||||
|
Matcher matcher = LINE_PATTERN.matcher(line);
|
||||||
|
|
||||||
|
if (matcher.find()) {
|
||||||
|
String chinese = matcher.group("chinese");
|
||||||
|
String english = matcher.group("english");
|
||||||
|
return chinese + " " + english;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
})
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.collect(Collectors.joining("\n"));
|
||||||
|
|
||||||
|
Files.writeString(outputDir.resolve(file.getFileName()), content, StandardCharsets.UTF_8);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("Error processing file: " + file, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除文件
|
||||||
|
*
|
||||||
|
* @param folder 文件目录
|
||||||
|
*/
|
||||||
|
public static void deleteFolder(File folder) {
|
||||||
|
if (folder.isDirectory()) {
|
||||||
|
File[] files = folder.listFiles();
|
||||||
|
if (files != null) {
|
||||||
|
for (File file : files) {
|
||||||
|
deleteFolder(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
boolean delete = folder.delete();
|
||||||
|
System.out.println("删除文件" + folder.getName() + ":" + (delete ? "成功" : "失败"));
|
||||||
|
}
|
||||||
|
}
|
21
pom.xml
21
pom.xml
|
@ -7,12 +7,17 @@
|
||||||
<groupId>cn.bunny</groupId>
|
<groupId>cn.bunny</groupId>
|
||||||
<artifactId>MultiThread</artifactId>
|
<artifactId>MultiThread</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>pom</packaging>
|
||||||
<licenses>
|
<licenses>
|
||||||
<license/>
|
<license/>
|
||||||
</licenses>
|
</licenses>
|
||||||
<developers>
|
<developers>
|
||||||
<developer/>
|
<developer/>
|
||||||
</developers>
|
</developers>
|
||||||
|
<modules>
|
||||||
|
<module>multithreading1</module>
|
||||||
|
<module>multithreading_init</module>
|
||||||
|
</modules>
|
||||||
<scm>
|
<scm>
|
||||||
<connection/>
|
<connection/>
|
||||||
<developerConnection/>
|
<developerConnection/>
|
||||||
|
@ -32,19 +37,27 @@
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<version>1.18.36</version>
|
<version>1.18.36</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
|
<!-- slf4j -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
<artifactId>slf4j-api</artifactId>
|
<artifactId>slf4j-api</artifactId>
|
||||||
<version>2.0.16</version>
|
<version>2.0.16</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
<artifactId>slf4j-simple</artifactId>
|
<artifactId>slf4j-simple</artifactId>
|
||||||
<version>2.0.16</version>
|
<version>2.0.16</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- jmh -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
|
<artifactId>jmh-core</artifactId>
|
||||||
|
<version>1.19</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
|
<artifactId>jmh-generator-annprocess</artifactId>
|
||||||
|
<version>1.19</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
Loading…
Reference in New Issue