ackageThreadDemo;/**
* 多線(xiàn)程
*? ? ? 程序:一組指定的集合(沒(méi)有運(yùn)行的程序)
*? ? ? 進(jìn)程:正在執(zhí)行的程序灸叼。負(fù)責(zé)獲取CPU內(nèi)存資源的
*? ? ? 線(xiàn)程:進(jìn)程中的一個(gè)組成部分鸦泳,負(fù)責(zé)被CPU執(zhí)行(CPU調(diào)度)
*
*? 注:
*? ? ? ? ? 一個(gè)進(jìn)程至少有一個(gè)線(xiàn)程,線(xiàn)程是進(jìn)程的組成部分跪腹,線(xiàn)程結(jié)束畏梆,進(jìn)程不一定結(jié)束闸婴,
*? ? ? ? ? 但是進(jìn)程結(jié)束坏挠,線(xiàn)程就一定會(huì)結(jié)束。
*
* java中線(xiàn)程的實(shí)現(xiàn)方式:
*? ? ? ? ? 繼承Thread類(lèi):
*? ? ? ? ? ? ? [1]繼承Thread類(lèi)
*? ? ? ? ? ? ? [2]重寫(xiě)run()方法
*? ? ? ? ? ? ? [3]創(chuàng)建對(duì)象邪乍,調(diào)用start方法降狠。啟動(dòng)線(xiàn)程
*/publicclassMyThreadextendsThread{publicMyThread(String name){super(name);? ? }@Overridepublicvoidrun() {for(inti =1;i<=5;i++){//獲取當(dāng)前線(xiàn)程及名稱(chēng)System.out.println(Thread.currentThread().getName()+"\t第"+i+"次執(zhí)行....");? ? ? ? ? ? }? ? ? ? }publicstaticvoidmain(String[] args) {//創(chuàng)建對(duì)象MyThread mt? =newMyThread("小琪");? ? ? ? MyThread mt1 =newMyThread("phoebe");? ? ? ? MyThread mt2 =newMyThread("菲比");? ? ? ? MyThread mt3 =newMyThread("帥帥");//mt.run();? //對(duì)象名.方法名();? 這個(gè)代碼寫(xiě)在了主方法里,由主線(xiàn)程來(lái)執(zhí)行mt.start();? ? ? ? mt1.start();? ? ? ? mt2.start();? ? ? ? mt3.start();? ? ? ? ? ? }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
packageThreadDemo;/**
* Java中線(xiàn)程實(shí)現(xiàn)的第二種方式:
*? ? ? ? ? 實(shí)現(xiàn)Runnable接口
*? ? ? ? ? ? ? [1]實(shí)現(xiàn)Runnable接口
*? ? ? ? ? ? ? [2]重寫(xiě)run方法
*? ? ? ? ? ? ? [3]創(chuàng)建一個(gè)Thread對(duì)象
*? ? ? ? ? ? ? [4]通過(guò)Thread對(duì)象庇楞,調(diào)用Thread類(lèi)中的start()方法啟動(dòng)線(xiàn)程
*
*? ? ? 開(kāi)發(fā)中建議使用接口的方式來(lái)實(shí)現(xiàn)多線(xiàn)程喊熟,以避免Java中類(lèi)的單繼承帶來(lái)的局限性
*? ? ? Runnable這個(gè)接口還能共享資源
*/publicclassMyRunimplementsRunnable{@Overridepublicvoidrun() {for(inti =1;i<=5;i++){? ? ? ? ? ? System.out.println(Thread.currentThread().getName()+":\t第"+i+"次運(yùn)行.....");? ? ? ? }? ? }publicstaticvoidmain(String[] args) {//創(chuàng)建對(duì)象MyRun mr =newMyRun();? ? ? ? Thread th =newThread(mr);//創(chuàng)建Thread類(lèi)對(duì)象,使用Thread類(lèi)中的start()方法啟動(dòng)線(xiàn)程th.start();? ? }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
packageThreadDemo01;/**
* 一共有5張票:(runable接口可以共享資源)
*? ? ? ? ? 合理的售完
*/publicclassMyRunnableimplementsRunnable{intticket =5;@Overridepublicvoidrun() {for(inti =1;i<=15;i++){//有15個(gè)人等著買(mǎi)票if(ticket>0){? ? ? ? ? ? ? ? ? ? ? ? System.out.println("線(xiàn)程"+Thread.currentThread().getName()+"賣(mài)票"+(ticket--));? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? }? ? ? ? }publicstaticvoidmain(String[] args) {//創(chuàng)建對(duì)象MyRunnable mr =newMyRunnable();? ? ? ? ? ? Thread th =newThread(mr);? ? ? ? ? ? Thread th1 =newThread(mr);? ? ? ? ? ? Thread th2 =newThread(mr);? ? ? ? ? ? th.start();? ? ? ? ? ? th1.start();? ? ? ? ? ? th2.start();? ? ? ? }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
packageThreadDemo02;/**
* 線(xiàn)程的五種狀態(tài):
*? ? ? ? ? 新生:通過(guò)new創(chuàng)建時(shí)
*? ? ? ? ? 就緒:通過(guò)調(diào)用start方法姐刁,具備執(zhí)行資格芥牌,等待分配到CPU資源
*? ? ? ? ? 運(yùn)行:具備執(zhí)行資格同時(shí)又剛好得到了CPU資源,執(zhí)行run方法中的代碼
*? ? ? ? ? ? ? ? ? ? 直到等待某資源而阻塞或執(zhí)行完任務(wù)而死亡
*? ? ? ? ? 阻塞:正在執(zhí)行時(shí)的線(xiàn)程聂使,被執(zhí)行了sleep(睡眠)方法等壁拉,停止了運(yùn)行則是阻塞階段
*? ? ? ? ? 死亡(終止):線(xiàn)程生命周期的最后一個(gè)階段:
*? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1、正常執(zhí)行完畢
*? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2柏靶、線(xiàn)程被強(qiáng)制制止
*? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 3弃理、線(xiàn)程拋出未捕獲的異常
*/publicclassThreadDemo{publicstaticvoidmain(String[] args) {//創(chuàng)建對(duì)象? ? 【新生】ThreadEntity te =newThreadEntity();? ? ? ? ? ? Thread t =newThread(te);? ? ? ? ? ? ? System.out.println("線(xiàn)程啟動(dòng)前:"+t.isAlive());//falset.start();//【就緒狀態(tài)】System.out.println("線(xiàn)程啟動(dòng)后:"+t.isAlive());//true//優(yōu)先級(jí)System.out.println("獲取當(dāng)前線(xiàn)程及名字:"+Thread.currentThread().getName());? ? ? ? ? ? System.out.println("獲取當(dāng)前線(xiàn)程的優(yōu)先級(jí):"+Thread.currentThread().getPriority());? ? ? ? ? ? System.out.println("線(xiàn)程的最高優(yōu)先級(jí):"+Thread.MAX_PRIORITY);? ? ? ? ? ? System.out.println("線(xiàn)程的最低優(yōu)先級(jí):"+Thread.MIN_PRIORITY);? ? ? ? ? ? System.out.println("線(xiàn)程的默認(rèn)優(yōu)先級(jí):"+Thread.NORM_PRIORITY);//設(shè)置優(yōu)先級(jí)t.setPriority(3);? ? ? ? ? ? System.out.println(t.getPriority());? ? ? ? }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
packageThreadDemo02;/**
*/publicclassThreadEntityimplementsRunnable{@Overridepublicvoidrun() {for(inti =1;i<=5;i++){? ? ? ? System.out.println(Thread.currentThread().getName()+":\t"+i);? ? }? ? }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16