Skip to content

Commit 01d9794

Browse files
committed
update conc01
1 parent 48d0adf commit 01d9794

21 files changed

+677
-0
lines changed

03concurrency/0301/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>java0.03concurrency</groupId>
8+
<artifactId>0301</artifactId>
9+
<version>1.0</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>8</source>
17+
<target>8</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
22+
23+
24+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package java0.conc0301;
2+
3+
public class DaemonThread {
4+
5+
public static void main(String[] args) {
6+
Runnable task = new Runnable() {
7+
@Override
8+
public void run() {
9+
try {
10+
Thread.sleep(5000);
11+
} catch (InterruptedException e) {
12+
e.printStackTrace();
13+
}
14+
Thread t = Thread.currentThread();
15+
System.out.println("当前线程:" + t.getName());
16+
}
17+
};
18+
Thread thread = new Thread(task);
19+
thread.setName("test-thread-1");
20+
thread.setDaemon(true);
21+
thread.start();
22+
}
23+
24+
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package java0.conc0301;
2+
3+
public class Runner1 implements Runnable {
4+
5+
@Override
6+
public void run() {
7+
for (int i = 0; i < 100; i++) {
8+
System.out.println("进入Runner1运行状态——————————" + i);
9+
}
10+
}
11+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package java0.conc0301;
2+
3+
public class Runner2 implements Runnable {
4+
5+
@Override
6+
public void run() {
7+
for (int i = 0; i < 100; i++) {
8+
System.out.println("进入Runner2运行状态——————————" + i);
9+
}
10+
11+
boolean result = Thread.currentThread().isInterrupted();
12+
13+
boolean result1 = Thread.interrupted(); // 重置状态
14+
15+
boolean result3 = Thread.currentThread().isInterrupted();
16+
17+
System.out.println("Runner2.run result ===>" + result);
18+
System.out.println("Runner2.run result1 ===>" + result1);
19+
System.out.println("Runner2.run result3 ===>" + result3);
20+
}
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
package java0.conc0301;
3+
4+
public class RunnerMain {
5+
6+
public static void main(String[] args) {
7+
8+
Runner1 runner1 = new Runner1();
9+
Thread thread1 = new Thread(runner1);
10+
11+
Runner2 runner2 = new Runner2();
12+
Thread thread2 = new Thread(runner2);
13+
14+
thread1.start();
15+
thread2.start();
16+
17+
thread2.interrupt();
18+
19+
System.out.println(Thread.activeCount());
20+
21+
// Thread.currentThread().getThreadGroup().list();
22+
// System.out.println(Thread.currentThread().getThreadGroup().getParent().activeGroupCount());
23+
// Thread.currentThread().getThreadGroup().getParent().list();
24+
}
25+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package java0.conc0301;
2+
3+
public class ThreadA extends Thread {
4+
5+
public void run() {
6+
super.run();
7+
try {
8+
Thread.sleep(500);
9+
} catch (InterruptedException e) {
10+
e.printStackTrace();
11+
}
12+
System.out.println("这是线程A");
13+
}
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package java0.conc0301;
2+
3+
public class ThreadB implements Runnable {
4+
5+
@Override
6+
public void run() {
7+
try {
8+
Thread.sleep(500);
9+
} catch (InterruptedException e) {
10+
e.printStackTrace();
11+
}
12+
System.out.println("这是线程B");
13+
14+
Thread currentThread = Thread.currentThread();
15+
String currentThreadName = currentThread.getName();
16+
17+
System.out.println("这是线程的名称:" + currentThreadName);
18+
System.out.println("返回当前线程" + currentThreadName + "的线程组中活动线程的数量:" + Thread.activeCount());
19+
System.out.println("返回该线程" + currentThreadName + "的标识符:" + currentThread.getId());
20+
System.out.println("返回该线程" + currentThreadName + "的优先级:" + currentThread.getPriority());
21+
System.out.println("返回该线程" + currentThreadName + "的状态:" + currentThread.getState());
22+
System.out.println("返回该线程" + currentThreadName + "所属的线程组:" + currentThread.getThreadGroup());
23+
System.out.println("测试该线程" + currentThreadName + "是否处于活跃状态:" + currentThread.isAlive());
24+
System.out.println("测试该线程" + currentThreadName + "是否为守护线程:" + currentThread.isDaemon());
25+
}
26+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package java0.conc0301;
2+
3+
import java.util.concurrent.Callable;
4+
5+
public class ThreadC implements Callable<String> {
6+
7+
@Override
8+
public String call() throws Exception {
9+
Thread.sleep(500);
10+
System.out.println("这是线程C");
11+
return "线程C";
12+
}
13+
14+
15+
16+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package java0.conc0301;
2+
3+
import java.util.concurrent.ExecutionException;
4+
import java.util.concurrent.FutureTask;
5+
6+
public class ThreadMain {
7+
8+
public static void main(String[] args) {
9+
10+
ThreadA threadA = new ThreadA();
11+
threadA.start();
12+
System.out.println("这是主线程:");
13+
14+
ThreadB threadB = new ThreadB();
15+
new Thread(threadB).start();
16+
System.out.println("这是主线程:");
17+
18+
ThreadC threadC = new ThreadC();
19+
FutureTask<String> futureTask = new FutureTask<>(threadC);
20+
new Thread(futureTask).start();
21+
System.out.println("这是主线程:begin!");
22+
try {
23+
System.out.println("得到的返回结果是:" + futureTask.get());
24+
} catch (InterruptedException e) {
25+
e.printStackTrace();
26+
} catch (ExecutionException e) {
27+
e.printStackTrace();
28+
}
29+
30+
}
31+
32+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package java0.conc0301;
2+
3+
public class ThreadMain2 {
4+
5+
public static void main(String[] args) {
6+
7+
ThreadB threadB = new ThreadB();
8+
for (int i = 0; i < 5; i++) {
9+
new Thread(threadB, "线程名称:(" + i + ")").start();
10+
}
11+
12+
try {
13+
Thread.sleep(10000);
14+
} catch (InterruptedException e) {
15+
e.printStackTrace();
16+
}
17+
18+
//返回对当前正在执行的线程对象的引用
19+
Thread threadMain = Thread.currentThread();
20+
System.out.println("这是主线程:");
21+
System.out.println("返回当前线程组中活动线程的数目:" + Thread.activeCount());
22+
System.out.println("主线程的名称:" + threadMain.getName());
23+
System.out.println("返回该线程的标识符:" + threadMain.getId());
24+
System.out.println("返回线程的优先级:" + threadMain.getPriority());
25+
System.out.println("返回线程的状态:" + threadMain.getState());
26+
System.out.println("返回该线程所属的线程组:" + threadMain.getThreadGroup());
27+
System.out.println("测试线程是否为守护线程:" + threadMain.isDaemon());
28+
29+
30+
// try {
31+
// Thread.sleep(10000);
32+
// } catch (InterruptedException e) {
33+
// e.printStackTrace();
34+
// }
35+
36+
}
37+
38+
39+
}

0 commit comments

Comments
 (0)