先看下通俗的定义区别
新建MyThread线程
package chapter01.section02.thread_1_2_1.project_3_z;
public class MyThread extends Thread{
private int i;
public MyThread(int i) {
super();
this.i = i;
}
@Override
public void run() {
System.out.println(i);
}
}
start()方法执行效果
package chapter01.section02.thread_1_2_1.project_3_z;
public class Test {
public static void main(String[] args) {
MyThread t11 = new MyThread(1);
MyThread t12 = new MyThread(2);
MyThread t13 = new MyThread(3);
MyThread t14 = new MyThread(4);
MyThread t15 = new MyThread(5);
MyThread t16 = new MyThread(6);
MyThread t17 = new MyThread(7);
MyThread t18 = new MyThread(8);
MyThread t19 = new MyThread(9);
MyThread t110 = new MyThread(10);
MyThread t111 = new MyThread(11);
MyThread t112 = new MyThread(12);
MyThread t113 = new MyThread(13);
t11.start();
t12.start();
t13.start();
t14.start();
t15.start();
t16.start();
t17.start();
t18.start();
t19.start();
t110.start();
t111.start();
t112.start();
t113.start();
}
}
start()执行结果【执行随机】
2
5
4
1
3
7
6
10
9
12
13
8
11
run()方法代码
package chapter01.section02.thread_1_2_1.project_3_z;
public class Test {
public static void main(String[] args) {
MyThread t11 = new MyThread(1);
MyThread t12 = new MyThread(2);
MyThread t13 = new MyThread(3);
MyThread t14 = new MyThread(4);
MyThread t15 = new MyThread(5);
MyThread t16 = new MyThread(6);
MyThread t17 = new MyThread(7);
MyThread t18 = new MyThread(8);
MyThread t19 = new MyThread(9);
MyThread t110 = new MyThread(10);
MyThread t111 = new MyThread(11);
MyThread t112 = new MyThread(12);
MyThread t113 = new MyThread(13);
t11.run();
t12.run();
t13.run();
t14.run();
t15.run();
t16.run();
t17.run();
t18.run();
t19.run();
t110.run();
t111.run();
t112.run();
t113.run();
}
}
run()方法执行结果【顺序执行】
1
2
3
4
5
6
7
8
9
10
11
12
13
上述代码援引:https://github.com/burnszp/java-multi-thread-programming
来自文件:
Java多线程编程核心技术_完整版 PDF电子书下载 带书签目录@www.java1234.com.pdf
下载链接:
链接:https://pan.baidu.com/s/1014DK3LnjRRpPOSFjS7yIg
提取码:bnan
复制这段内容后打开百度网盘手机App,操作更方便哦