first commit
This commit is contained in:
commit
3c709917e1
29
.gitignore
vendored
Normal file
29
.gitignore
vendored
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
### IntelliJ IDEA ###
|
||||||
|
out/
|
||||||
|
!**/src/main/**/out/
|
||||||
|
!**/src/test/**/out/
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
bin/
|
||||||
|
!**/src/main/**/bin/
|
||||||
|
!**/src/test/**/bin/
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
3
.idea/.gitignore
generated
vendored
Normal file
3
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
6
.idea/misc.xml
generated
Normal file
6
.idea/misc.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/java_test.iml" filepath="$PROJECT_DIR$/java_test.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
11
java_test.iml
Normal file
11
java_test.iml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
5
src/Main.java
Normal file
5
src/Main.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("Hello, World!");
|
||||||
|
}
|
||||||
|
}
|
21
src/MultiThreadDemo.java
Normal file
21
src/MultiThreadDemo.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import java.lang.management.ThreadInfo;
|
||||||
|
import java.lang.management.ThreadMXBean;
|
||||||
|
|
||||||
|
public class MultiThreadDemo {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ThreadMXBean threadMXBean = java.lang.management.ManagementFactory.getThreadMXBean();
|
||||||
|
|
||||||
|
// 不需要获取同步的 monitor 和 synchronizer 信息,仅获取线程和线程堆栈信息
|
||||||
|
ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false);
|
||||||
|
// 遍历线程信息,仅打印线程 ID 和线程名称信息
|
||||||
|
for (ThreadInfo threadInfo : threadInfos) {
|
||||||
|
|
||||||
|
System.out.println("[" + threadInfo.getThreadId() + "] "
|
||||||
|
+ threadInfo.getThreadName()
|
||||||
|
+ "; Thread state: " + threadInfo.getThreadState()
|
||||||
|
+ " ; Is daemon: " + threadInfo.isDaemon()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
32
src/ProcessDemo.java
Normal file
32
src/ProcessDemo.java
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
public class ProcessDemo {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
Process p = Runtime.getRuntime().exec(new String[]{"ls","-l", "/home/admin"});
|
||||||
|
|
||||||
|
// 读取标准输出
|
||||||
|
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
||||||
|
String line;
|
||||||
|
while ((line = stdInput.readLine()) != null) {
|
||||||
|
System.out.println("标准输出: " + line);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取错误输出
|
||||||
|
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
|
||||||
|
while ((line = stdError.readLine()) != null) {
|
||||||
|
System.out.println("错误输出: " + line);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 等待进程结束
|
||||||
|
int exitCode = p.waitFor();
|
||||||
|
|
||||||
|
System.out.println("进程结束,退出码: " + exitCode);
|
||||||
|
} catch (IOException | InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
42
src/ThreadLocalDemo.java
Normal file
42
src/ThreadLocalDemo.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
public class ThreadLocalDemo {
|
||||||
|
|
||||||
|
public ThreadLocal<Integer> localVar = new ThreadLocal<>();
|
||||||
|
private Integer integer = 0;
|
||||||
|
|
||||||
|
public void add() {
|
||||||
|
integer = integer + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int get() {
|
||||||
|
return integer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ThreadLocalDemo threadLocalDemo = new ThreadLocalDemo();
|
||||||
|
|
||||||
|
threadLocalDemo.localVar.set(threadLocalDemo.integer);
|
||||||
|
|
||||||
|
Thread t1 = new Thread(threadLocalDemo::add, "t1");
|
||||||
|
|
||||||
|
Thread t2 = new Thread( () -> {
|
||||||
|
threadLocalDemo.add();
|
||||||
|
threadLocalDemo.localVar.set(threadLocalDemo.get());
|
||||||
|
System.out.println("value in new thread: " + threadLocalDemo.get());
|
||||||
|
System.out.println("value in new thread localVar: " + threadLocalDemo.localVar.get());
|
||||||
|
}, "t2");
|
||||||
|
|
||||||
|
t1.start();
|
||||||
|
t2.start();
|
||||||
|
|
||||||
|
try {
|
||||||
|
t1.join();
|
||||||
|
System.out.println("value in main thread: " + threadLocalDemo.get());
|
||||||
|
System.out.println("value in main thread localVar: " + threadLocalDemo.localVar.get());
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
66
src/ThreadShareVariable.java
Normal file
66
src/ThreadShareVariable.java
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import java.util.stream.DoubleStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 类ThreadShareVariable演示了线程间共享变量的使用和潜在风险
|
||||||
|
*/
|
||||||
|
public class ThreadShareVariable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 共享变量i,多个线程会访问和修改这个变量
|
||||||
|
*/
|
||||||
|
public Integer i = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置共享变量i的值
|
||||||
|
*
|
||||||
|
* @param i 新的变量值
|
||||||
|
*/
|
||||||
|
public void setI(int i) {
|
||||||
|
this.i = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取共享变量i的值
|
||||||
|
*
|
||||||
|
* @return 当前的变量值
|
||||||
|
*/
|
||||||
|
public int getI() {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 主函数演示了如何在多线程环境下使用共享变量,并展示了线程局部变量的用法
|
||||||
|
*
|
||||||
|
* @param args 命令行参数
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ThreadShareVariable threadShareVariable = new ThreadShareVariable();
|
||||||
|
|
||||||
|
// 使用ThreadLocal以线程为单位隔离变量i的值,避免共享变量的并发问题
|
||||||
|
ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(threadShareVariable::getI);
|
||||||
|
|
||||||
|
// 新建一个线程,该线程会修改共享变量i的值
|
||||||
|
Thread thread = new Thread(() -> {
|
||||||
|
threadShareVariable.setI(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 新建一个线程,该线程会打印共享变量i的值
|
||||||
|
new Thread(() -> {
|
||||||
|
System.out.println("print i in new thread: " + threadShareVariable.getI());
|
||||||
|
|
||||||
|
}).start();
|
||||||
|
|
||||||
|
thread.start();
|
||||||
|
try {
|
||||||
|
// 等待线程thread执行完毕
|
||||||
|
thread.join();
|
||||||
|
// 打印共享变量i的值,预期为1
|
||||||
|
System.out.println(threadShareVariable.getI());
|
||||||
|
// 打印ThreadLocal中的值,此处的值是最原始的共享变量值0
|
||||||
|
System.out.println("value in ThreadLocal: " + threadLocal.get());
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
40
src/WeakReferenceExample.java
Normal file
40
src/WeakReferenceExample.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import java.lang.ref.WeakReference;
|
||||||
|
|
||||||
|
public class WeakReferenceExample {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Object obj = new Object();
|
||||||
|
|
||||||
|
//创建一个弱引用,指向该对象
|
||||||
|
WeakReference<Object> weakRef = new WeakReference<>(obj);
|
||||||
|
|
||||||
|
//手动调用垃圾回收
|
||||||
|
System.gc();
|
||||||
|
|
||||||
|
//检查弱引用是否被回收
|
||||||
|
if (weakRef.get() == null) {
|
||||||
|
System.out.println("Weak reference has been collected.");
|
||||||
|
} else {
|
||||||
|
System.out.println("Weak reference still exists.");
|
||||||
|
}
|
||||||
|
|
||||||
|
//清除强引用
|
||||||
|
obj = null;
|
||||||
|
|
||||||
|
if (weakRef.get() == null) {
|
||||||
|
System.out.println("Weak reference has been collected.");
|
||||||
|
} else {
|
||||||
|
System.out.println("Weak reference still exists.");
|
||||||
|
}
|
||||||
|
|
||||||
|
//再次手动调用垃圾回收
|
||||||
|
System.gc();
|
||||||
|
|
||||||
|
//再次检查弱引用是否被回收
|
||||||
|
if (weakRef.get() == null) {
|
||||||
|
System.out.println("Weak reference has been collected.");
|
||||||
|
} else {
|
||||||
|
System.out.println("Weak reference still exists.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user