File tree Expand file tree Collapse file tree 7 files changed +131
-2
lines changed
Expand file tree Collapse file tree 7 files changed +131
-2
lines changed Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 99public 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 22
33/**
44 * 线程创建的方式
5- * 共列举四种
65 *
76 * @Author: zzStar
87 * @Date: 2020-10-07 18:44
98 */
109public 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 */
2327class MyThread1 implements Runnable {
2428 @ Override
@@ -32,6 +36,7 @@ public void run() {
3236 * 这种创建线程的方法不够好,主要是因为其涉及运行机制问题,影响程序性能
3337 */
3438class 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+
Original file line number Diff line number Diff line change 1313public 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对象
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 1+ package startthread ;
2+
3+ /**
4+ * 启动线程总结
5+ *
6+ * @Author: zzStar
7+ * @Date: 10-13-2020 20:09
8+ */
9+ public class StartThreadSummary {
10+ }
You can’t perform that action at this time.
0 commit comments