Skip to content

Commit 6e63355

Browse files
add something about thread
1 parent f503906 commit 6e63355

File tree

7 files changed

+131
-2
lines changed

7 files changed

+131
-2
lines changed

AQS/AQS.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
</content>
88
<orderEntry type="inheritedJdk" />
99
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="library" name="scala-sdk-2.12.12" level="application" />
1011
</component>
1112
</module>

Thread/src/create/BothRunnableAndThread.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
public class BothRunnableAndThread {
1010
public static void main(String[] args) {
1111
Thread thread = new Thread(() -> System.out.println("来自Runnable的方法")) {
12+
13+
//这里的run方法已经把父类的run方法覆盖
1214
@Override
1315
public void run() {
1416
System.out.println("来自Thread的方法");
1517
}
1618
};
17-
thread.start();
19+
thread.start();//来自Thread的方法
1820
}
1921
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package create;
2+
3+
/**
4+
* 实现多线程的方法总结
5+
*
6+
* @Author: zzStar
7+
* @Date: 10-13-2020 19:06
8+
*/
9+
public class CreateThreadSummary {
10+
11+
/**
12+
* 两种方法!创建新的执行线程
13+
* 1.将类声明为子类Thread,重写类的run方法Thread,也即继承Thread类
14+
* 2.声明一个实现Runnable接口的类,实现run方法
15+
*
16+
* 准确地说,创建线程只有一种方式就是构造Thread类,而实现线程的执行单元有两种方式
17+
* 1.实现Runnable接口的run方法,并把Runnable实例传给Thread类
18+
* 2.重写Thread的run方法(继承Thread类)
19+
*/
20+
21+
22+
/**
23+
* 优先选择Runnable接口
24+
* 1.解耦
25+
* 2.传入实例可以反复利用同一个线程
26+
* 3.Java不支持双继承
27+
*/
28+
}

Thread/src/create/ThreadCreate.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22

33
/**
44
* 线程创建的方式
5-
* 共列举四种
65
*
76
* @Author: zzStar
87
* @Date: 2020-10-07 18:44
98
*/
109
public class ThreadCreate {
1110
public static void main(String[] args) {
11+
1212
//创建Runnable实现类的实例,并用这个实例作为Thread的target来创建Thread对象,
1313
//这个Thread对象才是真正的线程对象
14+
//最终调用target.run()
1415
new Thread(new MyThread1(), "thread 1").start();
1516

17+
//继承所实现的直接start
18+
//run()整个都被重写
1619
new MyThread2("thread 2").start();
1720
}
1821
}
1922

2023
/**
2124
* 通过实现Runnable接口来创建Thread线程,重写run()方法
25+
* 优先选择
2226
*/
2327
class MyThread1 implements Runnable {
2428
@Override
@@ -32,6 +36,7 @@ public void run() {
3236
* 这种创建线程的方法不够好,主要是因为其涉及运行机制问题,影响程序性能
3337
*/
3438
class MyThread2 extends Thread {
39+
3540
//调用super(name)后,getName才会返回你创建新实例时传入的name
3641
MyThread2(String name) {
3742
super(name);
@@ -42,3 +47,14 @@ public void run() {
4247
System.out.println(Thread.currentThread().getName() + "-->run...");
4348
}
4449
}
50+
51+
/*
52+
//run源码
53+
@Override
54+
public void run() {
55+
if (target != null) {
56+
target.run();
57+
}
58+
}
59+
*/
60+

Thread/src/create/TimerThreadTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
public class TimerThreadTest {
1414
public static void main(String[] args) {
1515
TimerTask task = new TimerTask() { //创建一个新的timer task
16+
1617
@Override
1718
public void run() { //定时器任务执行的操作
1819
Date date = new Date();//创建Date对象
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package startthread;
2+
3+
/**
4+
* 对比start & run
5+
*
6+
* 一个线程两次调用start方法时会抛出异常,见源码对线程状态的检查
7+
* 调用start才是真正意义上的启动线程,经历线程的各个周期
8+
*
9+
* @Author: zzStar
10+
* @Date: 10-13-2020 20:09
11+
*/
12+
public class StartAndRunMethod {
13+
public static void main(String[] args) {
14+
Runnable runnable = () -> {
15+
System.out.println(Thread.currentThread().getName());
16+
};
17+
18+
runnable.run();//main
19+
20+
new Thread(runnable).start();//Thread-0
21+
22+
}
23+
}
24+
25+
/*
26+
private volatile int threadStatus;
27+
28+
public synchronized void start() {
29+
*/
30+
/**
31+
* This method is not invoked for the main method thread or "system"
32+
* group threads created/set up by the VM. Any new functionality added
33+
* to this method in the future may have to also be added to the VM.
34+
* <p>
35+
* A zero status value corresponds to state "NEW".
36+
*//*
37+
38+
//检查线程状态,初始化就是0
39+
if (threadStatus != 0)
40+
throw new IllegalThreadStateException();
41+
42+
*/
43+
/* Notify the group that this thread is about to be started
44+
* so that it can be added to the group's list of threads
45+
* and the group's unstarted count can be decremented. *//*
46+
47+
//加入线程组
48+
group.add(this);
49+
50+
boolean started = false;
51+
try {
52+
start0();
53+
started = true;
54+
} finally {
55+
try {
56+
if (!started) {
57+
group.threadStartFailed(this);
58+
}
59+
} catch (Throwable ignore) {
60+
*/
61+
/* do nothing. If start0 threw a Throwable then
62+
it will be passed up the call stack *//*
63+
64+
}
65+
}
66+
}
67+
68+
//jdk源码
69+
private native void start0();
70+
*/
71+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package startthread;
2+
3+
/**
4+
* 启动线程总结
5+
*
6+
* @Author: zzStar
7+
* @Date: 10-13-2020 20:09
8+
*/
9+
public class StartThreadSummary {
10+
}

0 commit comments

Comments
 (0)