Java的知识点30——线程的优先级、终止线程的典型方式、获取线程基本信息的方法
線程的優(yōu)先級(jí) ?1-10
1. NORM_PRIORITY ?5 默認(rèn)
?2. MIN_PRIORITY ?1
?3. MAX_PRIORITY ?10
注意:優(yōu)先級(jí)低只是意味著獲得調(diào)度的概率低。并不是絕對(duì)先調(diào)用優(yōu)先級(jí)高后調(diào)用優(yōu)先級(jí)低的線程。
使用下述方法獲得或設(shè)置線程對(duì)象的優(yōu)先級(jí)。
? int getPriority();
? void setPriority(int newPriority);?
/*** 線程的優(yōu)先級(jí) 1-10* 1. NORM_PRIORITY 5 默認(rèn)* 2. MIN_PRIORITY 1* 3. MAX_PRIORITY 10* * 概率: 不代表絕對(duì)的優(yōu)先級(jí)順序* @author Administrator**/ public class PriorityTest {public static void main(String[] args) {System.out.println(Thread.currentThread().getPriority()); //5MyPriority mp=new MyPriority();Thread t1=new Thread(mp,"1");Thread t2=new Thread(mp,"2");Thread t3=new Thread(mp,"3");Thread t4=new Thread(mp,"4");Thread t5=new Thread(mp,"5");Thread t6=new Thread(mp,"6");//設(shè)置優(yōu)先級(jí)在啟動(dòng)前t4.setPriority(Thread.MAX_PRIORITY);t5.setPriority(Thread.MAX_PRIORITY);t6.setPriority(Thread.MAX_PRIORITY);t1.setPriority(Thread.MIN_PRIORITY);t2.setPriority(Thread.MIN_PRIORITY);t3.setPriority(Thread.MIN_PRIORITY);t1.start();t2.start();t3.start();t4.start();t5.start();t6.start();} }class MyPriority implements Runnable{@Overridepublic void run() {System.out.println(Thread.currentThread().getName()+"-->"+ Thread.currentThread().getPriority()); //5Thread.yield();}}線程分為用戶線程和守護(hù)線程
虛擬機(jī)必須確保用戶線程執(zhí)行完畢
虛擬機(jī)不用等待守護(hù)線程執(zhí)行完畢;如:后臺(tái)記錄操作日志、監(jiān)控內(nèi)存使用
守護(hù)線程:是為用戶線程服務(wù)的;jvm停止 不用等待守護(hù)線程執(zhí)行完畢
默認(rèn):用戶線程jvm等待用戶線程執(zhí)行完畢才會(huì)停止
終止線程的典型方式
終止線程我們一般不使用JDK提供的stop()/destroy()方法(它們本身也被JDK廢棄了)。通常的做法是提供一個(gè)boolean型的終止變量,當(dāng)這個(gè)變量置為false,則終止線程的運(yùn)行。
public class TestThreadCiycle implements Runnable {String name;boolean live = true;// 標(biāo)記變量,表示線程是否可中止;public TestThreadCiycle(String name) {super();this.name = name;}public void run() {int i = 0;//當(dāng)live的值是true時(shí),繼續(xù)線程體;false則結(jié)束循環(huán),繼而終止線程體;while (live) {System.out.println(name + (i++));}}public void terminate() {live = false;}public static void main(String[] args) {TestThreadCiycle ttc = new TestThreadCiycle("線程A:");Thread t1 = new Thread(ttc);// 新生狀態(tài)t1.start();// 就緒狀態(tài)for (int i = 0; i < 10; i++) {System.out.println("主線程" + i);}ttc.terminate();System.out.println("ttc stop!");} }?? 暫停線程執(zhí)行常用的方法有sleep()和yield()方法,這兩個(gè)方法的區(qū)別是:
? ? ??1. sleep()方法:可以讓正在運(yùn)行的線程進(jìn)入阻塞狀態(tài),直到休眠時(shí)間滿了,進(jìn)入就緒狀態(tài)。
? ? ??2. yield()方法:可以讓正在運(yùn)行的線程直接進(jìn)入就緒狀態(tài),讓出CPU的使用權(quán)。
獲取線程基本信息的方法
?* isAlive() ?表示線程是否還活著
?* Thread.currentThread() ? 表示當(dāng)前線程
?* setName("") ?setName("") ? 表示代理對(duì)象的名稱?
?
/*** 其他方法* isAlive() 表示線程是否還活著* Thread.currentThread() 表示當(dāng)前線程* setName("") setName("") 表示代理對(duì)象的名稱 * @author Administrator**/ public class InfoTest {public static void main(String[] args) throws InterruptedException {System.out.println(Thread.currentThread().isAlive());//設(shè)置名稱:真實(shí)角色+代理角色MyInfo info=new MyInfo("戰(zhàn)斗機(jī)");Thread t=new Thread(info);t.setName("公雞");t.start();Thread.sleep(1000);System.out.println(t.isAlive());} } class MyInfo implements Runnable{private String name;public MyInfo(String name) {super();this.name = name;}@Overridepublic void run() {System.out.println(Thread.currentThread().getName()+"-->"+name);}}總結(jié)
以上是生活随笔為你收集整理的Java的知识点30——线程的优先级、终止线程的典型方式、获取线程基本信息的方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 计算机网络知识点2——数据交换、码分多路
- 下一篇: Java的知识点31——线程同步