Skip to content

Commit 8838fe5

Browse files
committed
Add Stage 3 Lesson 1
1 parent 11a2cb9 commit 8838fe5

File tree

16 files changed

+470
-2
lines changed

16 files changed

+470
-2
lines changed

「一入 Java 深似海 」/代码/segmentfault/deep-in-java/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<modules>
1313
<module>stage-1</module>
1414
<module>stage-2</module>
15+
<module>stage-3</module>
1516
</modules>
1617

1718
<build>

「一入 Java 深似海 」/代码/segmentfault/deep-in-java/stage-2/lesson-4/src/main/java/com/segmentfault/deep/in/java/collection/algorithm/MergeSort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private void merge(Comparable[] values, int low, int mid, int high) {
2626
Comparable[] a2 = new Comparable[n2];
2727

2828
//把 values[0...mid] 内容复制给 a1
29-
System.arraycopy(values, 0, a1, 0, n1);
29+
System.arraycopy(values, low, a1, 0, n1);
3030
//把 values[mid+1...high] 内容复制给 a2
3131
System.arraycopy(values, mid + 1, a2, 0, n2);
3232

「一入 Java 深似海 」/代码/segmentfault/deep-in-java/stage-2/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<module>lesson-1</module>
1717
<module>lesson-2</module>
1818
<module>lesson-3</module>
19-
<module>stage-2-lesson-4</module>
19+
<module>lesson-4</module>
2020
</modules>
2121

2222
<build>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>deep-in-java</artifactId>
7+
<groupId>com.segmentfault</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>stage-3</artifactId>
13+
<name>「一入 Java 深似海 」系列 :: 第三期</name>
14+
<modules>
15+
<module>stage-3-lesson-1</module>
16+
</modules>
17+
<packaging>pom</packaging>
18+
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<version>3.8.0</version>
25+
<configuration>
26+
<source>11</source>
27+
<target>11</target>
28+
</configuration>
29+
</plugin>
30+
</plugins>
31+
</build>
32+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>stage-3</artifactId>
7+
<groupId>com.segmentfault</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>stage-3-lesson-1</artifactId>
13+
14+
15+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.segmentfault.deep.in.java.process;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.lang.management.ManagementFactory;
6+
import java.lang.management.OperatingSystemMXBean;
7+
8+
public class ChildProcessDemo {
9+
10+
public static void main(String[] args) throws IOException {
11+
12+
// IDEA(主进程) -> 启动 ChildProcessDemo -> Windows 计算器(calc)
13+
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
14+
15+
if (operatingSystemMXBean.getName().startsWith("Windows")) {
16+
// 启动计算器
17+
Runtime.getRuntime().exec("calc");
18+
19+
// Process process = Runtime.getRuntime().exec("dir");
20+
//
21+
// InputStream inputStream = process.getInputStream();
22+
// int data = 0;
23+
// while ((data = inputStream.read()) > -1) {
24+
// System.out.print(data);
25+
// }
26+
}
27+
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.segmentfault.deep.in.java.process;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.OutputStream;
6+
import java.util.concurrent.TimeoutException;
7+
8+
/**
9+
* https://github.com/mercyblitz/confucius-commons
10+
*/
11+
public class ProcessExecutor {
12+
13+
private static final long waitForTimeInSecond = Long.getLong("process.executor.wait.for", 1);
14+
private final String command;
15+
private final String arguments;
16+
private final Runtime runtime = Runtime.getRuntime();
17+
private boolean finished;
18+
19+
/**
20+
* Constructor
21+
*
22+
* @param processName
23+
* command
24+
* @param arguments
25+
* process arguments
26+
*/
27+
public ProcessExecutor(String processName, String... arguments) {
28+
StringBuilder argumentsBuilder = new StringBuilder();
29+
if (arguments != null) {
30+
for (String argument : arguments) {
31+
argumentsBuilder.append(" ").append(argument);
32+
}
33+
}
34+
this.arguments = argumentsBuilder.toString();
35+
this.command = processName + this.arguments;
36+
}
37+
38+
/**
39+
* Execute current process.
40+
* <p/>
41+
* // * @param inputStream input stream keeps output stream from process
42+
*
43+
* @param outputStream
44+
* output stream for process normal or error input stream.
45+
* @throws IOException
46+
* if process execution is failed.
47+
*/
48+
public void execute(OutputStream outputStream) throws IOException {
49+
try {
50+
this.execute(outputStream, Long.MAX_VALUE);
51+
} catch (TimeoutException e) {
52+
}
53+
}
54+
55+
/**
56+
* Execute current process.
57+
* <p/>
58+
* // * @param inputStream input stream keeps output stream from process
59+
*
60+
* @param outputStream
61+
* output stream for process normal or error input stream.
62+
* @param timeoutInMilliseconds
63+
* milliseconds timeout
64+
* @throws IOException
65+
* if process execution is failed.
66+
* @throws TimeoutException
67+
* if the execution is timeout over specified <code>timeoutInMilliseconds</code>
68+
*/
69+
public void execute(OutputStream outputStream, long timeoutInMilliseconds) throws IOException, TimeoutException {
70+
Process process = runtime.exec(command);
71+
long startTime = System.currentTimeMillis();
72+
long endTime = -1L;
73+
InputStream processInputStream = process.getInputStream();
74+
InputStream processErrorInputStream = process.getErrorStream();
75+
// OutputStream processOutputStream = process.getOutputStream();
76+
int exitValue = -1;
77+
while (!finished) {
78+
long costTime = endTime - startTime;
79+
if (costTime > timeoutInMilliseconds) {
80+
finished = true;
81+
process.destroy();
82+
String message = String.format("Execution is timeout[%d ms]!", timeoutInMilliseconds);
83+
throw new TimeoutException(message);
84+
}
85+
try {
86+
while (processInputStream.available() > 0) {
87+
outputStream.write(processInputStream.read());
88+
}
89+
while (processErrorInputStream.available() > 0) {
90+
outputStream.write(processErrorInputStream.read());
91+
}
92+
exitValue = process.exitValue();
93+
if (exitValue != 0) {
94+
throw new IOException();
95+
}
96+
finished = true;
97+
} catch (IllegalThreadStateException e) {
98+
// Process is not finished yet;
99+
// Sleep a little to save on CPU cycles
100+
waitFor(waitForTimeInSecond);
101+
endTime = System.currentTimeMillis();
102+
} finally {
103+
}
104+
}
105+
}
106+
107+
/**
108+
* Wait for specified seconds
109+
*
110+
* @param seconds
111+
* specified seconds
112+
*/
113+
private void waitFor(long seconds) {
114+
try {
115+
Thread.sleep(seconds * 1000);
116+
} catch (InterruptedException e) {
117+
Thread.interrupted();
118+
}
119+
}
120+
121+
/**
122+
* Check current process finish or not.
123+
*
124+
* @return <code>true</code> if current process finished
125+
*/
126+
public boolean isFinished() {
127+
return finished;
128+
}
129+
130+
public static void main(String[] args) throws IOException, TimeoutException {
131+
ProcessExecutor executor = new ProcessExecutor("java","-version");
132+
executor.execute(System.out, 2000);
133+
}
134+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.segmentfault.deep.in.java.process;
2+
3+
import java.lang.management.ManagementFactory;
4+
import java.lang.management.RuntimeMXBean;
5+
6+
public class ProcessIdDemo {
7+
8+
public static void main(String[] args) {
9+
10+
// Java 9 之前的实现
11+
getProcessIdBeforeJava9();
12+
getProcessIdInJava9();
13+
getProcessIdInJava10();
14+
}
15+
16+
private static void getProcessIdInJava10() {
17+
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
18+
System.out.println("[Java 10 + 的方法] 当前进程 ID : " + runtimeMXBean.getPid());
19+
}
20+
21+
private static void getProcessIdInJava9() {
22+
long pid = ProcessHandle.current().pid();
23+
System.out.println("[Java 9 + 的方法] 当前进程 ID : " + pid);
24+
}
25+
26+
private static void getProcessIdBeforeJava9() {
27+
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
28+
String name = runtimeMXBean.getName();
29+
String pid = name.substring(0, name.indexOf("@"));
30+
System.out.println("[Java 9 之前的方法] 当前进程 ID : " + pid);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.segmentfault.deep.in.java.process;
2+
3+
import java.lang.management.*;
4+
import java.time.Instant;
5+
import java.time.LocalDate;
6+
import java.time.ZoneId;
7+
8+
public class ProcessInfoDemo {
9+
10+
public static void main(String[] args) {
11+
12+
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
13+
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
14+
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
15+
16+
long pid = ProcessHandle.current().pid();
17+
System.out.println("[Java 9 + 的方法] 当前进程 ID : " + pid);
18+
19+
Instant instant = Instant.ofEpochMilli(runtimeMXBean.getStartTime());
20+
LocalDate localDate = LocalDate.ofInstant(instant, ZoneId.systemDefault());
21+
System.out.println("当前进程启动时间:" + localDate);
22+
System.out.println("当前进程上线时间:" + runtimeMXBean.getUptime());
23+
System.out.println("当前进程线程数量:" + threadMXBean.getThreadCount());
24+
25+
ManagementFactory.getMemoryManagerMXBeans().forEach(memoryManagerMXBean -> {
26+
27+
});
28+
29+
System.exit(9);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.segmentfault.deep.in.java.thread;
2+
3+
public class DumpThreadDemo {
4+
5+
public static void main(String[] args) {
6+
7+
// Throwable API
8+
new Throwable("Stack trace").printStackTrace(System.out);
9+
10+
// Thread API
11+
Thread.dumpStack();
12+
13+
// Java 9 StackWalker API
14+
StackWalker stackWalker = StackWalker.getInstance();
15+
stackWalker.forEach(System.out::println);
16+
}
17+
}

0 commit comments

Comments
 (0)