Skip to content

Commit 97e01f9

Browse files
committed
🔖 线程操作示例
1 parent 38f7d2d commit 97e01f9

11 files changed

Lines changed: 220 additions & 185 deletions
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.github.dunwu.javase.concurrent.thread;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.Callable;
5+
import java.util.concurrent.ExecutionException;
6+
import java.util.concurrent.FutureTask;
7+
8+
/**
9+
* @author Zhang Peng
10+
* @date 2018/5/22
11+
*/
12+
public class CallableAndFutureDemo {
13+
14+
public static void main(String[] args) {
15+
Callable<Integer> callable = () -> new Random().nextInt(100);
16+
FutureTask<Integer> future = new FutureTask<>(callable);
17+
new Thread(future).start();
18+
try {
19+
Thread.sleep(1000);// 可能做一些事情
20+
System.out.println(future.get());
21+
} catch (InterruptedException | ExecutionException e) {
22+
e.printStackTrace();
23+
}
24+
}
25+
}
Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
package io.github.dunwu.javase.concurrent.thread;
22

3-
class MyThread implements Runnable{ // 实现Runnable接口
4-
public void run(){ // 覆写run()方法
5-
for(int i=0;i<3;i++){
6-
System.out.println(Thread.currentThread().getName()
7-
+ "运行,i = " + i) ; // 取得当前线程的名字
8-
}
9-
}
10-
};
11-
public class ThreadAliveDemo{
12-
public static void main(String[] args){
13-
MyThread mt = new MyThread() ; // 实例化Runnable子类对象
14-
Thread t = new Thread(mt,"线程"); // 实例化Thread对象
15-
System.out.println("线程开始执行之前 --> " + t.isAlive()) ; // 判断是否启动
16-
t.start() ; // 启动线程
17-
System.out.println("线程开始执行之后 --> " + t.isAlive()) ; // 判断是否启动
18-
for(int i=0;i<3;i++){
19-
System.out.println(" main运行 --> " + i) ;
20-
}
21-
// 以下的输出结果不确定
22-
System.out.println("代码执行之后 --> " + t.isAlive()) ; // 判断是否启动
233

24-
}
25-
};
4+
public class ThreadAliveDemo {
5+
6+
public static void main(String[] args) {
7+
MyThread mt = new MyThread(); // 实例化Runnable子类对象
8+
Thread t = new Thread(mt, "线程"); // 实例化Thread对象
9+
System.out.println("线程开始执行之前 --> " + t.isAlive()); // 判断是否启动
10+
t.start(); // 启动线程
11+
System.out.println("线程开始执行之后 --> " + t.isAlive()); // 判断是否启动
12+
for (int i = 0; i < 3; i++) {
13+
System.out.println(" main运行 --> " + i);
14+
}
15+
// 以下的输出结果不确定
16+
System.out.println("代码执行之后 --> " + t.isAlive()); // 判断是否启动
17+
18+
}
19+
20+
static class MyThread implements Runnable {
21+
22+
@Override
23+
public void run() {
24+
for (int i = 0; i < 3; i++) {
25+
System.out.println(Thread.currentThread().getName() + "运行,i = " + i);
26+
}
27+
}
28+
}
29+
}

‎codes/concurrent/src/main/java/io/github/dunwu/javase/concurrent/thread/ThreadDaemonDemo.java‎

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,28 @@
22

33
/**
44
* 守护线程示例
5-
* 什么是守护线程
6-
* 守护线程(Daemon Thread)是在后台执行并且不会阻止 JVM 终止的线程。 与守护线程(Daemon Thread)相反的,叫用户线程(User
7-
* Thread),也就是非守护线程。
8-
* 为什么要用守护线程
9-
* 守护线程的优先级比较低,用于为系统中的其它对象和线程提供服务。典型的应用就是垃圾回收器。
10-
* 如何创建守护线程
11-
* 使用 thread.setDaemon(true) 可以设置 thread 线程为守护线程。
12-
* 注意点:
13-
* 正在运行的用户线程无法设置为守护线程,所以 thread.setDaemon(true)必须在 thread.start()之前设置,否则会抛出
14-
* llegalThreadStateException 异常; 一个守护线程创建的子线程依然是守护线程。 不要认为所有的应用都可以分配给 Daemon 来进行服务,比如读写操作或者计算逻辑。
5+
* <p>
6+
* 什么是守护线程 守护线程(Daemon Thread)是在后台执行并且不会阻止 JVM 终止的线程。 与守护线程(Daemon Thread)相反的,叫用户线程(User Thread),也就是非守护线程。
7+
* <p>
8+
* 为什么要用守护线程 守护线程的优先级比较低,用于为系统中的其它对象和线程提供服务。典型的应用就是垃圾回收器。
9+
* <p>
10+
* 如何创建守护线程 使用 thread.setDaemon(true) 可以设置 thread 线程为守护线程。
11+
* <p>
12+
* 注意点: 正在运行的用户线程无法设置为守护线程,所以 thread.setDaemon(true)必须在 thread.start()之前设置,否则会抛出 llegalThreadStateException 异常;
13+
* 一个守护线程创建的子线程依然是守护线程。 不要认为所有的应用都可以分配给 Daemon 来进行服务,比如读写操作或者计算逻辑。
1514
* @author Zhang Peng
1615
* @date 2018/1/18
1716
*/
1817
public class ThreadDaemonDemo {
1918

20-
static class ThreadDaemon implements Runnable {
19+
public static void main(String[] args) {
20+
Thread t = new Thread(new MyThread(), "线程");
21+
t.setDaemon(true); // 此线程在后台运行
22+
System.out.println("线程 t 是否是守护进程:" + t.isDaemon());
23+
t.start(); // 启动线程
24+
}
25+
26+
static class MyThread implements Runnable {
2127

2228
@Override
2329
public void run() {
@@ -26,12 +32,4 @@ public void run() {
2632
}
2733
}
2834
}
29-
30-
public static void main(String[] args) {
31-
ThreadDaemon mt = new ThreadDaemon(); // 实例化Runnable子类对象
32-
Thread t = new Thread(mt, "线程"); // 实例化Thread对象
33-
t.setDaemon(true); // 此线程在后台运行
34-
System.out.println("线程 t 是否是守护进程:" + t.isDaemon());
35-
t.start(); // 启动线程
36-
}
3735
}
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,9 @@
11
package io.github.dunwu.javase.concurrent.thread;
22

3-
class ThreadInterrupt implements Runnable { // 实现Runnable接口
4-
public void run() { // 覆写run()方法
5-
System.out.println("1、进入run()方法");
6-
try {
7-
Thread.sleep(10000); // 线程休眠10秒
8-
System.out.println("2、已经完成了休眠");
9-
} catch (InterruptedException e) {
10-
System.out.println("3、休眠被终止");
11-
return; // 返回调用处
12-
}
13-
System.out.println("4、run()方法正常结束");
14-
}
15-
};
16-
17-
18-
19-
203
public class ThreadInterruptDemo {
4+
215
public static void main(String[] args) {
22-
ThreadInterrupt mt = new ThreadInterrupt(); // 实例化Runnable子类对象
6+
MyThread mt = new MyThread(); // 实例化Runnable子类对象
237
Thread t = new Thread(mt, "线程"); // 实例化Thread对象
248
t.start(); // 启动线程
259
try {
@@ -29,4 +13,20 @@ public static void main(String[] args) {
2913
}
3014
t.interrupt(); // 中断线程执行
3115
}
32-
};
16+
17+
static class MyThread implements Runnable {
18+
19+
@Override
20+
public void run() {
21+
System.out.println("1、进入run()方法");
22+
try {
23+
Thread.sleep(10000); // 线程休眠10秒
24+
System.out.println("2、已经完成了休眠");
25+
} catch (InterruptedException e) {
26+
System.out.println("3、休眠被终止");
27+
return; // 返回调用处
28+
}
29+
System.out.println("4、run()方法正常结束");
30+
}
31+
}
32+
}
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
package io.github.dunwu.javase.concurrent.thread;
22

33
/**
4-
* thread.join() 示例
5-
* join() 方法会使当前线程等待调用 join() 方法的线程结束后才能继续执行。
4+
* thread.join() 示例 join() 方法会使当前线程等待调用 join() 方法的线程结束后才能继续执行。
65
* @author Zhang Peng
76
* @date 2018/1/18
87
* @see ThreadSleepDemo
98
* @see ThreadYieldDemo
109
*/
1110
public class ThreadJoinDemo {
1211

13-
static class ThreadJoin implements Runnable {
14-
15-
@Override
16-
public void run() {
17-
for (int i = 0; i < 50; i++) {
18-
System.out.println(Thread.currentThread().getName() + " 运行,i = " + i); // 取得当前线程的名字
19-
}
20-
}
21-
}
22-
2312
public static void main(String[] args) {
24-
ThreadJoin mt = new ThreadJoin(); // 实例化Runnable子类对象
13+
MyThread mt = new MyThread(); // 实例化Runnable子类对象
2514
Thread t = new Thread(mt, "mythread"); // 实例化Thread对象
2615
t.start(); // 启动线程
2716
for (int i = 0; i < 50; i++) {
@@ -35,4 +24,14 @@ public static void main(String[] args) {
3524
System.out.println("Main 线程运行 --> " + i);
3625
}
3726
}
38-
};
27+
28+
static class MyThread implements Runnable {
29+
30+
@Override
31+
public void run() {
32+
for (int i = 0; i < 50; i++) {
33+
System.out.println(Thread.currentThread().getName() + " 运行,i = " + i); // 取得当前线程的名字
34+
}
35+
}
36+
}
37+
}
Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
package io.github.dunwu.javase.concurrent.thread;
22

3-
class ThreadName implements Runnable { // 实现Runnable接口
4-
@Override
5-
public void run() {
6-
for (int i = 0; i < 3; i++) {
7-
System.out.println(Thread.currentThread().getName() + "运行,i = " + i); // 取得当前线程的名字
8-
}
9-
}
10-
};
11-
12-
13-
14-
3+
/**
4+
* get/set 线程名称示例
5+
* @author Zhang Peng
6+
*/
157
public class ThreadNameDemo {
8+
169
public static void main(String[] args) {
17-
ThreadName mt = new ThreadName(); // 实例化Runnable子类对象
10+
MyThread mt = new MyThread(); // 实例化Runnable子类对象
1811
new Thread(mt).start(); // 系统自动设置线程名称
1912
new Thread(mt, "线程-A").start(); // 手工设置线程名称
20-
new Thread(mt, "线程-B").start(); // 手工设置线程名称
21-
new Thread(mt).start(); // 系统自动设置线程名称
22-
new Thread(mt).start(); // 系统自动设置线程名称
13+
Thread t = new Thread(mt); // 手工设置线程名称
14+
t.setName("线程-B");
15+
t.start();
16+
}
17+
18+
static class MyThread implements Runnable {
19+
20+
@Override
21+
public void run() {
22+
for (int i = 0; i < 3; i++) {
23+
System.out.println(Thread.currentThread().getName() + "运行,i = " + i); // 取得当前线程的名字
24+
}
25+
}
2326
}
24-
};
27+
}

‎codes/concurrent/src/main/java/io/github/dunwu/javase/concurrent/thread/ThreadPriorityDemo.java‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
* <p>
66
* Java 中的线程优先级如何控制?
77
* <p>
8-
* Java 中的线程优先级的范围是 [1,10],一般来说,高优先级的线程在运行时会具有优先权。可以通过
9-
* thread.setPriority(Thread.MAX_PRIORITY) 的方式设置,默认优先级为 5。
8+
* Java 中的线程优先级的范围是 [1,10],一般来说,高优先级的线程在运行时会具有优先权。可以通过 thread.setPriority(Thread.MAX_PRIORITY) 的方式设置,默认优先级为 5。
109
* <p>
1110
* 高优先级的Java线程一定先执行吗?
1211
* <p>
13-
* 即使设置了线程的优先级,也无法保证高优先级的线程一定先执行。
14-
* 原因:这是因为线程优先级依赖于操作系统的支持,然而,不同的操作系统支持的线程优先级并不相同,不能很好的和Java中线程优先级一一对应。
15-
* 结论:Java 线程优先级控制并不可靠。
12+
* 即使设置了线程的优先级,也无法保证高优先级的线程一定先执行。 原因:这是因为线程优先级依赖于操作系统的支持,然而,不同的操作系统支持的线程优先级并不相同,不能很好的和Java中线程优先级一一对应。 结论:Java
13+
* 线程优先级控制并不可靠。
1614
* @author Zhang Peng
1715
* @date 2018/5/10
1816
*/
@@ -24,9 +22,9 @@ public static void main(String[] args) {
2422
System.out.println("NORM_PRIORITY = " + Thread.NORM_PRIORITY);
2523
System.out.println("MIN_PRIORITY = " + Thread.MIN_PRIORITY);
2624

27-
Thread t1 = new Thread(new ThreadPriority(), "线程A"); // 实例化线程对象
28-
Thread t2 = new Thread(new ThreadPriority(), "线程B"); // 实例化线程对象
29-
Thread t3 = new Thread(new ThreadPriority(), "线程C"); // 实例化线程对象
25+
Thread t1 = new Thread(new MyThread(), "线程A"); // 实例化线程对象
26+
Thread t2 = new Thread(new MyThread(), "线程B"); // 实例化线程对象
27+
Thread t3 = new Thread(new MyThread(), "线程C"); // 实例化线程对象
3028
t1.setPriority(Thread.MIN_PRIORITY); // 优先级最低
3129
t2.setPriority(Thread.MAX_PRIORITY); // 优先级最低
3230
t3.setPriority(Thread.NORM_PRIORITY); // 优先级最低
@@ -35,7 +33,7 @@ public static void main(String[] args) {
3533
t3.start(); // 启动线程
3634
}
3735

38-
static class ThreadPriority implements Runnable {
36+
static class MyThread implements Runnable {
3937

4038
@Override
4139
public void run() {
@@ -45,8 +43,11 @@ public void run() {
4543
} catch (InterruptedException e) {
4644
e.printStackTrace();
4745
}
48-
System.out.println(Thread.currentThread().getName() + "运行,i = " + i); // 取得当前线程的名字
46+
// 取得当前线程的名字
47+
String out =
48+
Thread.currentThread().getName() + ",优先级:" + Thread.currentThread().getPriority() + ",运行:i = " + i;
49+
System.out.println(out);
4950
}
5051
}
5152
}
52-
};
53+
}
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
package io.github.dunwu.javase.concurrent.thread;
22

33
/**
4-
* Thread.sleep() 示例
5-
* sleep() 方法需要指定等待的时间,它可以让当前正在执行的线程在指定的时间内暂停执行,进入 Blocked 状态。
4+
* Thread.sleep() 示例 sleep() 方法需要指定等待的时间,它可以让当前正在执行的线程在指定的时间内暂停执行,进入 Blocked 状态。
65
* 该方法既可以让其他同优先级或者高优先级的线程得到执行的机会,也可以让低优先级的线程得到执行机会。
76
* 但是,sleep() 方法不会释放“锁标志”,也就是说如果有 synchronized 同步块,其他线程仍然不能访问共享数据。
87
* @author Zhang Peng
98
* @date 2018/1/18
109
* @see ThreadJoinDemo
1110
* @see ThreadYieldDemo
1211
*/
13-
public class ThreadSleepDemo implements Runnable {
14-
private String name;
15-
private int time;
12+
public class ThreadSleepDemo {
1613

17-
private ThreadSleepDemo(String name, int time) {
18-
this.name = name; // 设置线程名称
19-
this.time = time; // 设置休眠时间
14+
public static void main(String[] args) {
15+
new Thread(new MyThread("线程A", 1000)).start();
16+
new Thread(new MyThread("线程A", 2000)).start();
17+
new Thread(new MyThread("线程A", 3000)).start();
2018
}
2119

22-
@Override
23-
public void run() {
24-
try {
25-
Thread.sleep(this.time); // 休眠指定的时间
26-
} catch (InterruptedException e) {
27-
e.printStackTrace();
20+
static class MyThread implements Runnable {
21+
22+
private String name;
23+
private int time;
24+
25+
private MyThread(String name, int time) {
26+
this.name = name; // 设置线程名称
27+
this.time = time; // 设置休眠时间
2828
}
29-
System.out.println(this.name + "线程,休眠" + this.time + "毫秒。");
30-
}
3129

32-
public static void main(String[] args) {
33-
ThreadSleepDemo mt1 = new ThreadSleepDemo("线程A", 1000); // 定义线程对象,指定休眠时间
34-
ThreadSleepDemo mt2 = new ThreadSleepDemo("线程B", 2000); // 定义线程对象,指定休眠时间
35-
ThreadSleepDemo mt3 = new ThreadSleepDemo("线程C", 3000); // 定义线程对象,指定休眠时间
36-
new Thread(mt1).start(); // 启动线程
37-
new Thread(mt2).start(); // 启动线程
38-
new Thread(mt3).start(); // 启动线程
30+
@Override
31+
public void run() {
32+
try {
33+
Thread.sleep(this.time); // 休眠指定的时间
34+
} catch (InterruptedException e) {
35+
e.printStackTrace();
36+
}
37+
System.out.println(this.name + "线程,休眠" + this.time + "毫秒。");
38+
}
3939
}
40-
};
40+
}

0 commit comments

Comments
 (0)