线程构造器结构
1.线程中的构造器可以使用很多参数
- public thread():分配一个新的线程对象
- public Thread(String name):分配一个指定名字的线程对象
- public Thread(Runnable target):指定创建线程的目标对象,它实现了Runnable接口中的run方法
- public Thread(Runnable target, String name):分配一个带有指定目标新的线程对象并指定名字
比如下面的例子,主线程输出1-100之内的偶数,分线程也输出1=100之内的偶数,其中用户将分线程重命名为“线程1”:
提示:使用extends Thread
同时使用 public Thread(String name)
重命名thread
package Thread;public class ThreadConsTest {public static void main(String[] args) {//3. 创建该Thread类子类的对象(创建Thread子类的实例)PrintNumberCons x = new PrintNumberCons("线程1");//4. 调用线程对象的start()方法,启动该线程x.start();for (int i = 0; i <= 100; i++) {if (i % 2 == 0) {System.out.println(i + "\t" + Thread.currentThread().getName());}}}}//5. 如果想实现其他更多线程,需要创建新的线程对象,不能再次调用已经start的线程。//1. 创建继承Thread类的子类class PrintNumberCons extends Thread {public PrintNumberCons() {}public PrintNumberCons(String name) {super(name);}//2. 重写Thread的run(),该run方法体囊括此线程需要完成的各种操作@Overridepublic void run() {for (int i = 0; i <= 100; i++) {if (i % 2 == 0) {System.out.println(i + "\t" + Thread.currentThread().getName());}}}}
运行结果可以看到“线程1”和“main”线程交替出现
0 线程1
2 线程1
4 线程1
6 线程1
8 线程1
10 线程1
12 线程1
14 线程1
16 线程1
18 线程1
20 线程1
22 线程1
24 线程1
26 线程1
28 线程1
30 线程1
32 线程1
0 main
2 main
4 main
6 main
8 main
10 main
12 main
34 线程1
36 线程1
38 线程1
40 线程1
42 线程1
44 线程1
46 线程1
48 线程1
50 线程1
14 main
16 main
18 main
20 main
22 main
24 main
26 main
28 main
30 main
32 main
34 main
36 main
38 main
40 main
42 main
44 main
46 main
48 main
50 main
52 main
54 main
56 main
58 main
60 main
62 main
64 main
66 main
68 main
70 main
72 main
52 线程1
54 线程1
56 线程1
58 线程1
60 线程1
62 线程1
64 线程1
66 线程1
68 线程1
70 线程1
72 线程1
74 线程1
76 线程1
78 线程1
80 线程1
82 线程1
84 线程1
86 线程1
88 线程1
90 线程1
92 线程1
94 线程1
96 线程1
98 线程1
100 线程1
74 main
76 main
78 main
80 main
82 main
84 main
86 main
88 main
90 main
92 main
94 main
96 main
98 main
100 mainProcess finished with exit code 0
Thread类的常用方法
start():1.启动线程2.调用线程run()
run():将线程要执行的操作,声明在run()中
currentThread():获取当前执行代码对应的线程
setName():设置线程名
getName():获取线程名
sleep(long millis):静态方法,调用时会让当前线程睡眠括号里的毫秒数
yield():静态方法,一旦调用,该线程直接释放CPU的执行权(注意本thread释放CPU之后有可能又迅速的在公平竞争中把CPU抢回来了,所以用户未必能在结果中体会到这个CPU的释放),比如Thread.yield()
join():在线程A通过线程B调用join,表示线程A进入阻塞状态,CPU直接去执行线程B,直到线程B执行结束,线程A停止阻塞再继续执行。
isAlive():判断当前线程是否存活
stop():已过时 不建议使用,强行结束线程直接死亡。可能会出现收尾工作没有做完,比如关闭文件,数据库等,同时会立刻释放该线程所有的锁,导致数据不同步等问题。
suspend() resume():已经过时,不建议使用。操作其暂停和恢复,必须成对出现,否则非常容易发生死锁。suspend()会导致线程暂停但不释放任何锁资源,其它线程都无法访问被它占用的锁,直到resume().
先看一个使用join的例子
package ThreadPool;public class Test2 {public static void main(String[] args) {Thread t_add = new Thread (){public void run() {for (int i = 0; i <= 100; i++) {System.out.println(Thread.currentThread().getName()+" is printing "+i);}}};Thread t_minus= new Thread (){public void run() {try {t_add.join(); //在t_minus里面调用t_add,这句的意思是等着t_add执行完毕再执行下面的t_minus内容} catch (InterruptedException e) {e.printStackTrace();}for (int i = 100; i >= 0; i--) {System.out.println(Thread.currentThread().getName()+" is printing "+i);}}};//尽管 t_minus是先于t_add开始start的,因为有着上面的join()控制,
//t_add.start仍是先执行的。t_minus.start();t_add.start();}}
运行结果
Thread-0 is printing 0
Thread-0 is printing 1
Thread-0 is printing 2
Thread-0 is printing 3
Thread-0 is printing 4
Thread-0 is printing 5
Thread-0 is printing 6
Thread-0 is printing 7
Thread-0 is printing 8
Thread-0 is printing 9
Thread-0 is printing 10
Thread-0 is printing 11
Thread-0 is printing 12
Thread-0 is printing 13
Thread-0 is printing 14
Thread-0 is printing 15
Thread-0 is printing 16
Thread-0 is printing 17
Thread-0 is printing 18
Thread-0 is printing 19
Thread-0 is printing 20
Thread-0 is printing 21
Thread-0 is printing 22
Thread-0 is printing 23
Thread-0 is printing 24
Thread-0 is printing 25
Thread-0 is printing 26
Thread-0 is printing 27
Thread-0 is printing 28
Thread-0 is printing 29
Thread-0 is printing 30
Thread-0 is printing 31
Thread-0 is printing 32
Thread-0 is printing 33
Thread-0 is printing 34
Thread-0 is printing 35
Thread-0 is printing 36
Thread-0 is printing 37
Thread-0 is printing 38
Thread-0 is printing 39
Thread-0 is printing 40
Thread-0 is printing 41
Thread-0 is printing 42
Thread-0 is printing 43
Thread-0 is printing 44
Thread-0 is printing 45
Thread-0 is printing 46
Thread-0 is printing 47
Thread-0 is printing 48
Thread-0 is printing 49
Thread-0 is printing 50
Thread-0 is printing 51
Thread-0 is printing 52
Thread-0 is printing 53
Thread-0 is printing 54
Thread-0 is printing 55
Thread-0 is printing 56
Thread-0 is printing 57
Thread-0 is printing 58
Thread-0 is printing 59
Thread-0 is printing 60
Thread-0 is printing 61
Thread-0 is printing 62
Thread-0 is printing 63
Thread-0 is printing 64
Thread-0 is printing 65
Thread-0 is printing 66
Thread-0 is printing 67
Thread-0 is printing 68
Thread-0 is printing 69
Thread-0 is printing 70
Thread-0 is printing 71
Thread-0 is printing 72
Thread-0 is printing 73
Thread-0 is printing 74
Thread-0 is printing 75
Thread-0 is printing 76
Thread-0 is printing 77
Thread-0 is printing 78
Thread-0 is printing 79
Thread-0 is printing 80
Thread-0 is printing 81
Thread-0 is printing 82
Thread-0 is printing 83
Thread-0 is printing 84
Thread-0 is printing 85
Thread-0 is printing 86
Thread-0 is printing 87
Thread-0 is printing 88
Thread-0 is printing 89
Thread-0 is printing 90
Thread-0 is printing 91
Thread-0 is printing 92
Thread-0 is printing 93
Thread-0 is printing 94
Thread-0 is printing 95
Thread-0 is printing 96
Thread-0 is printing 97
Thread-0 is printing 98
Thread-0 is printing 99
Thread-0 is printing 100
Thread-1 is printing 100
Thread-1 is printing 99
Thread-1 is printing 98
Thread-1 is printing 97
Thread-1 is printing 96
Thread-1 is printing 95
Thread-1 is printing 94
Thread-1 is printing 93
Thread-1 is printing 92
Thread-1 is printing 91
Thread-1 is printing 90
Thread-1 is printing 89
Thread-1 is printing 88
Thread-1 is printing 87
Thread-1 is printing 86
Thread-1 is printing 85
Thread-1 is printing 84
Thread-1 is printing 83
Thread-1 is printing 82
Thread-1 is printing 81
Thread-1 is printing 80
Thread-1 is printing 79
Thread-1 is printing 78
Thread-1 is printing 77
Thread-1 is printing 76
Thread-1 is printing 75
Thread-1 is printing 74
Thread-1 is printing 73
Thread-1 is printing 72
Thread-1 is printing 71
Thread-1 is printing 70
Thread-1 is printing 69
Thread-1 is printing 68
Thread-1 is printing 67
Thread-1 is printing 66
Thread-1 is printing 65
Thread-1 is printing 64
Thread-1 is printing 63
Thread-1 is printing 62
Thread-1 is printing 61
Thread-1 is printing 60
Thread-1 is printing 59
Thread-1 is printing 58
Thread-1 is printing 57
Thread-1 is printing 56
Thread-1 is printing 55
Thread-1 is printing 54
Thread-1 is printing 53
Thread-1 is printing 52
Thread-1 is printing 51
Thread-1 is printing 50
Thread-1 is printing 49
Thread-1 is printing 48
Thread-1 is printing 47
Thread-1 is printing 46
Thread-1 is printing 45
Thread-1 is printing 44
Thread-1 is printing 43
Thread-1 is printing 42
Thread-1 is printing 41
Thread-1 is printing 40
Thread-1 is printing 39
Thread-1 is printing 38
Thread-1 is printing 37
Thread-1 is printing 36
Thread-1 is printing 35
Thread-1 is printing 34
Thread-1 is printing 33
Thread-1 is printing 32
Thread-1 is printing 31
Thread-1 is printing 30
Thread-1 is printing 29
Thread-1 is printing 28
Thread-1 is printing 27
Thread-1 is printing 26
Thread-1 is printing 25
Thread-1 is printing 24
Thread-1 is printing 23
Thread-1 is printing 22
Thread-1 is printing 21
Thread-1 is printing 20
Thread-1 is printing 19
Thread-1 is printing 18
Thread-1 is printing 17
Thread-1 is printing 16
Thread-1 is printing 15
Thread-1 is printing 14
Thread-1 is printing 13
Thread-1 is printing 12
Thread-1 is printing 11
Thread-1 is printing 10
Thread-1 is printing 9
Thread-1 is printing 8
Thread-1 is printing 7
Thread-1 is printing 6
Thread-1 is printing 5
Thread-1 is printing 4
Thread-1 is printing 3
Thread-1 is printing 2
Thread-1 is printing 1
Thread-1 is printing 0Process finished with exit code 0
下面的例子使用到了Thread类其他常见的method
package Thread;import static java.lang.Thread.currentThread;
import static java.lang.Thread.sleep;public class ThreadConsTest {public static void main(String[] args) throws InterruptedException {//3. 创建该Thread类子类的对象(创建Thread子类的实例)PrintNumberCons x = new PrintNumberCons("线程1");//4. 调用线程对象的start()方法,启动该线程x.setName("线程1计算能被2整除的数");x.start();Thread.currentThread().setName("主线程main在计算--不能被3整除的数");for (int i = 0; i <= 30; i++) {if (i % 3 != 0) {currentThread().sleep(800);if(i>=8) x.join();System.out.println(i + "\t" + Thread.currentThread().getName());}}System.out.println("主线程main还活着吗? "+Thread.currentThread().isAlive());System.out.println("线程1还活着吗? "+ x.isAlive());}}//5. 如果想实现其他更多线程,需要创建新的线程对象,不能再次调用已经start的线程。//1. 创建继承Thread类的子类class PrintNumberCons extends Thread {public PrintNumberCons() {}public PrintNumberCons(String name) {super(name);}//2. 重写Thread的run(),该run方法体囊括此线程需要完成的各种操作@Overridepublic void run() {for (int i = 0; i <= 20; i++) {if (i >=5){Thread.yield();}//if (i==16) currentThread().yield();if(i==16) {try {currentThread().join(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}if (i % 2 == 0) {if (i == 16) currentThread().yield();try {sleep(800);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(i + "\t" + Thread.currentThread().getName());}}}}
运行结果:
1 主线程main在计算--不能被3整除的数
0 线程1计算能被2整除的数
2 线程1计算能被2整除的数
2 主线程main在计算--不能被3整除的数
4 线程1计算能被2整除的数
4 主线程main在计算--不能被3整除的数
6 线程1计算能被2整除的数
5 主线程main在计算--不能被3整除的数
8 线程1计算能被2整除的数
7 主线程main在计算--不能被3整除的数
10 线程1计算能被2整除的数
12 线程1计算能被2整除的数
14 线程1计算能被2整除的数
16 线程1计算能被2整除的数
18 线程1计算能被2整除的数
20 线程1计算能被2整除的数
8 主线程main在计算--不能被3整除的数
10 主线程main在计算--不能被3整除的数
11 主线程main在计算--不能被3整除的数
13 主线程main在计算--不能被3整除的数
14 主线程main在计算--不能被3整除的数
16 主线程main在计算--不能被3整除的数
17 主线程main在计算--不能被3整除的数
19 主线程main在计算--不能被3整除的数
20 主线程main在计算--不能被3整除的数
22 主线程main在计算--不能被3整除的数
23 主线程main在计算--不能被3整除的数
25 主线程main在计算--不能被3整除的数
26 主线程main在计算--不能被3整除的数
28 主线程main在计算--不能被3整除的数
29 主线程main在计算--不能被3整除的数
主线程main还活着吗? true
线程1还活着吗? falseProcess finished with exit code 0
线程的优先级
getPriority():获取线程的优先级
setPriority():设置线程的优先级 范围 【1-10】
Thread类内部声明的三个常量:
- public static final int MIN_PRIORITY = 1; 最低优先级
- public static final int NORM_PRIORITY = 5; 默认优先级
- public static final int MAX_PRIORITY = 10; 最高优先级
下面的例子中括号里分别用了数字和来定义setPriority(),都是可以使用的,值得注意的是,对于多核CPU,资源抢占获得的优先级在运行结果处并不是十分明显。
package Thread;import static java.lang.Thread.currentThread;
import static java.lang.Thread.sleep;public class ThreadConsTest {public static void main(String[] args) throws InterruptedException {//3. 创建该Thread类子类的对象(创建Thread子类的实例)PrintNumberCons x = new PrintNumberCons("线程1");//4. 调用线程对象的start()方法,启动该线程x.setName("线程1计算能被2整除的数");x.setPriority(7); //也可以写 x.setPriority(MAX_PRIORITY);x.start();Thread.currentThread().setName("主线程main在计算--不能被3整除的数");Thread.currentThread().setPriority(2);//也可以写 Thread.currentThread().setPriority(MIN_PRIORITY);for (int i = 0; i <= 30; i++) {if (i % 3 != 0) {currentThread().sleep(800);if(i>=8) x.join();System.out.println(i + "\t" + Thread.currentThread().getName());}}System.out.println("主线程main还活着吗? "+Thread.currentThread().isAlive());System.out.println("线程1还活着吗? "+ x.isAlive());}}//5. 如果想实现其他更多线程,需要创建新的线程对象,不能再次调用已经start的线程。//1. 创建继承Thread类的子类class PrintNumberCons extends Thread {public PrintNumberCons() {}public PrintNumberCons(String name) {super(name);}//2. 重写Thread的run(),该run方法体囊括此线程需要完成的各种操作@Overridepublic void run() {for (int i = 0; i <= 20; i++) {if (i >=5){Thread.yield();}//if (i==16) currentThread().yield();if(i==16) {try {currentThread().join(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}if (i % 2 == 0) {if (i == 16) currentThread().yield();try {sleep(800);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(i + "\t" + Thread.currentThread().getName());}}}}
运行结果:
0 线程1计算能被2整除的数
1 主线程main在计算--不能被3整除的数
2 线程1计算能被2整除的数
2 主线程main在计算--不能被3整除的数
4 线程1计算能被2整除的数
4 主线程main在计算--不能被3整除的数
6 线程1计算能被2整除的数
5 主线程main在计算--不能被3整除的数
8 线程1计算能被2整除的数
7 主线程main在计算--不能被3整除的数
10 线程1计算能被2整除的数
12 线程1计算能被2整除的数
14 线程1计算能被2整除的数
16 线程1计算能被2整除的数
18 线程1计算能被2整除的数
20 线程1计算能被2整除的数
8 主线程main在计算--不能被3整除的数
10 主线程main在计算--不能被3整除的数
11 主线程main在计算--不能被3整除的数
13 主线程main在计算--不能被3整除的数
14 主线程main在计算--不能被3整除的数
16 主线程main在计算--不能被3整除的数
17 主线程main在计算--不能被3整除的数
19 主线程main在计算--不能被3整除的数
20 主线程main在计算--不能被3整除的数
22 主线程main在计算--不能被3整除的数
23 主线程main在计算--不能被3整除的数
25 主线程main在计算--不能被3整除的数
26 主线程main在计算--不能被3整除的数
28 主线程main在计算--不能被3整除的数
29 主线程main在计算--不能被3整除的数
主线程main还活着吗? true
线程1还活着吗? false