java線程Thread常用方法及守護(hù)線程
/**
* Thread類常用方法及守護(hù)線程
*/
public class Test6Thread extends Thread{
? ?@Override
? ?public void run() {
? ? ? ?System.out.println(Thread.currentThread().getName());
? ? ? ?//Thread類靜態(tài)方法currentThread()返回當(dāng)前線程,在線程對(duì)象中也可以使用this.getName()得到線程對(duì)象的name
? ?}
? ?public static void main(String[] args) {
? ? ? ?Test6Thread t1 = new Test6Thread();
? ? ? ?t1.start();
? ? ? ?//結(jié)果為:Thread-0 第一個(gè)創(chuàng)建的線程的name為0號(hào)
? ? ? ?try {
? ? ? ? ? ?Thread.sleep(1000);
? ? ? ?} catch (InterruptedException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?Thread t2 = new Thread(new Test6Thread());
? ? ? ?//Thread(Runnable)構(gòu)造方法,因?yàn)門hread實(shí)現(xiàn)了Runnable,所以也可以自己套自己
? ? ? ?t2.start();
? ? ? ?//結(jié)果為:Thread-2,因?yàn)閭魅氲膎ew Test6Thread()也產(chǎn)生了一個(gè)Thread對(duì)象Thread-1,而t2.start()后t2線程執(zhí)行了1號(hào)的run方法,currentThread()返回當(dāng)前執(zhí)行方法的線程而不是當(dāng)前run()方法所屬的對(duì)象
? ? ? ?//Thread類在構(gòu)造器中設(shè)定了新對(duì)象的name值,默認(rèn)使用Thread-序號(hào)的格式,每生成一個(gè)默認(rèn)name序號(hào)++
? ? ? ?Thread t3 = new Thread(new Test6Thread(),"sfda");
? ? ? ?//構(gòu)造器中設(shè)定t3的name值為sfda
? ? ? ?t3.start();
? ? ? ?//結(jié)果為:sfda,但內(nèi)置的new Test6Thread()的name是Thread-3
? ? ? ?Thread t4 = new Thread(new Test6Thread());
? ? ? ?t4.start();
? ? ? ?//結(jié)果為:Thread-5,因?yàn)閮?nèi)置的new Test6Thread()為Thread-4
? ? ? ?t4.setName("t4");
? ? ? ?//Thread類的.setName(String name)更改name值
? ? ? ?System.out.println(t4.isAlive());
? ? ? ?//.isAlive()判斷線程是否正在運(yùn)行,處于死亡狀態(tài)返回false
? ? ? ?try {
? ? ? ? ? ?Thread.sleep(3000);
? ? ? ?} catch (InterruptedException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?System.out.println(t1.isAlive());
? ? ? ?//結(jié)果為:false,t1線程已運(yùn)行結(jié)束變?yōu)樗劳鰻顟B(tài)
? ? ? ?Thread t5 = new Thread();
? ? ? ?System.out.println(t5.isAlive());
? ? ? ?//結(jié)果為:false,新生狀態(tài)下線程沒有運(yùn)行,只是一個(gè)對(duì)象
? ?}
}
class Test7Thread implements Runnable{
? ?boolean alive = true;
? ?int num;
? ?@Override
? ?public void run() {
? ? ? ?while (alive){
? ? ? ? ? ?num++;
? ? ? ? ? ?Thread.yield();
? ? ? ?}
? ? ? ?System.out.println(Thread.currentThread().getName()+"執(zhí)行次數(shù):"+num);
? ?}
? ?public void over(){
? ? ? ?alive=false;
? ? ? ?//結(jié)束線程
? ?}
? ?public static void main(String[] args) {
? ? ? ?Test7Thread run1 = new Test7Thread();
? ? ? ?Test7Thread run2 = new Test7Thread();
? ? ? ?Thread t1 = new Thread(run1,"t1");
? ? ? ?Thread t2 = new Thread(run2,"t2");
? ? ? ?t1.setPriority(Thread.MAX_PRIORITY);
? ? ? ?//.setPriority(int)設(shè)定優(yōu)先級(jí),線程優(yōu)先級(jí)最大MAX_PRIORITY=10
? ? ? ?System.out.println(t2.getPriority());
? ? ? ?//.getPriority()返回當(dāng)前優(yōu)先級(jí),默認(rèn)是NORM_PRIORITY=5
? ? ? ?t2.setPriority(Thread.MIN_PRIORITY);
? ? ? ?//線程優(yōu)先級(jí)最小MIN_PRIORITY=1,優(yōu)先級(jí)的設(shè)定必須在啟動(dòng)start之前才生效
? ? ? ?t1.start();
? ? ? ?t2.start();
? ? ? ?try {
? ? ? ? ? ?Thread.sleep(1000);
? ? ? ?} catch (InterruptedException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?run1.over();
? ? ? ?run2.over();
? ? ? ?//結(jié)果:
? ? ? ?//t2執(zhí)行次數(shù):7787259
? ? ? ?//t1執(zhí)行次數(shù):7761756
? ? ? ?//優(yōu)先級(jí)的設(shè)定只是java內(nèi)的概念,JVM只能將優(yōu)先級(jí)傳給操作系統(tǒng),實(shí)際的優(yōu)先級(jí)由操作系統(tǒng)決定
? ? ? ?//結(jié)論:沒什么用
? ?}
}
class DaemonThread implements Runnable{
? ?//測(cè)試守護(hù)線程
? ?//java中線程分為用戶線程和守護(hù)線程,通過對(duì)線程設(shè)定setDaemon(true)使該線程變?yōu)槭刈o(hù)線程,其他所有默認(rèn)false的線程都是用戶線程,用戶線程是程序里的自定義線程,守護(hù)線程是服務(wù)其他線程的線程,比如垃圾回收線程,當(dāng)所有用戶線程均已結(jié)束,只剩下守護(hù)線程時(shí),JVM退出,守護(hù)線程隨之中斷
? ?@Override
? ?public void run() {
? ? ? ?while (true){
? ? ? ? ? ?System.out.println("守護(hù)線程運(yùn)行中");
? ? ? ? ? ?try {
? ? ? ? ? ? ? ?Thread.sleep(20);
? ? ? ? ? ?} catch (InterruptedException e) {
? ? ? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ? ? ?}
? ? ? ? ? ?//實(shí)現(xiàn)接口的方法在接口中沒有設(shè)定throws拋出異常則實(shí)現(xiàn)方法也不能拋出
? ? ? ?}
? ?}
? ?public static void main(String[] args) {
? ? ? ?Thread t = new Thread(new DaemonThread());
? ? ? ?t.setDaemon(true);
? ? ? ?//.setDaemon(boolean)設(shè)定守護(hù)線程,默認(rèn)false即用戶線程
? ? ? ?t.start();
? ? ? ?//在main線程中啟動(dòng)了守護(hù)線程t,main中沒有再啟動(dòng)其他用戶線程,當(dāng)main主線程結(jié)束(可能存在其他JVM自動(dòng)生成的用戶線程隨之結(jié)束后),所有用戶線程結(jié)束,守護(hù)線程t隨JVM退出而結(jié)束
? ? ? ?try {
? ? ? ? ? ?Thread.sleep(500);
? ? ? ?} catch (InterruptedException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?System.out.println("主線程結(jié)束");
? ? ? ?//結(jié)果為:
? ? ? ?//守護(hù)線程運(yùn)行中
? ? ? ?//守護(hù)線程運(yùn)行中
? ? ? ?//主線程結(jié)束
? ? ? ?//任何子線程都可以啟動(dòng)守護(hù)線程,當(dāng)主線程、子線程等所有用戶線程結(jié)束后,JVM退出,守護(hù)線程結(jié)束
? ?}
}