——多線程的優先級,setDaemon方法,yield方法及join方法簡述

---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity開發</a>、<a href="http://www.itheima.com"target="blank">.Net培訓</a>、期待與您交流! ----------------------

守護線程:

setDaemon(boolean on):

   使用Thread類中的 static void setDaemon(boolean on)方法當on參數爲true時,將線程標記爲守護線程

   或者用戶線程(後臺線程)。當正在運行的線程都是守護線程時,java虛擬機退出。程序結束。

   注:該方法必須在啓動線程前調用。

   守護線程的特點是:

隨着前臺線程結束而結束。

setDaemon實例代碼:

package com.itheima.threaddemos.ThreadCommunication;

/**
 * 線程停止測試類
 * @author wuyong
 *
 */
public class SetDaemonDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		MyStopThread mst = new MyStopThread();
		
		Thread t1= new Thread(mst);
		Thread t2 = new Thread(mst);
		
		//調用setDaemon(boolean flag)方法將線程設置爲守護線程,即後臺線程
//		t1.setDaemon(true);
//		t2.setDaemon(true);
		
		//線程啓動
		t1.start();
		t2.start();
		
		//主線程執行的代碼
		for (int j = 0; j <= j ; j++) {
			if (j == 60) { 
//				強制將凍結狀態下的線程恢復到運行狀態,使線程對標記進行判斷,從而到達控制循環,以結束run方法。
				t1.interrupt();
				t2.interrupt();
				break;
			}
			System.out.println(Thread.currentThread().getName() + "---run");
		}
	}

}
/**
 * 自定義線程類
 * @author wuyong
 *
 */
class MyStopThread implements Runnable{
	//控制線程狀態的標記
	boolean flag = true;
	@Override
	public synchronized void run() {
		while (flag) {
			try {
				this.wait();//線程等待(凍結狀態)
			} catch (InterruptedException e) {
				flag = false;
				System.out.println(Thread.currentThread().getName() + "stop");
			}
			System.out.println(Thread.currentThread().getName() + "...run");
		}
	}
	
}


線程的join()方法:

   當A線程執行到了B線程等待join等待時,A就會將執行權交給B,等B線程都執行完,A纔會執行。

   join的作用:可以用來臨時加入線程執行,即可以手動的獲取CPU執行權。

線程的yield() 方法:
          作用:暫停當前正在執行的線程對象,並執行其他線程。

join和yield示例代碼:

package com.itheima.threaddemos.ThreadCommunication;


/**
 * 線程中join方法的測試類
 * @author wuyong
 *
 */
public class JoinMethodDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		JoinThread jt = new JoinThread();
		Thread t1 = new Thread(jt);
		Thread t2 = new Thread(jt);
		
		t1.start();
//		t1.join();//t1調用join方法,來獲得主線程的執行權,此時,主線程處於凍結狀態,要等到t1執行完畢時,主線程纔會執行。
		t2.start();
		for (int i = 0; i < 60; i++) {
//			System.out.println(Thread.currentThread().getName() + "---" + i);
		}
	}

}
/**
 * 自定義的線程類
 * @author wuyong
 *
 */
class JoinThread implements Runnable{

	@Override
	public void run() {
		for (int i = 0; i < 30; i++) {
			System.out.println(Thread.currentThread().getName() + "..." + i);
			Thread.yield();//使當前線程暫停。執行其他線程。
		}
	}
	
}

線程的優先級:

   線程的優先級範圍:1~10;

   用Thread類的setPriority(int newPriority)更改線程的優先級。

   其中Thread類中定義了三個優先級常量:

1,MAX PRIORITY:10;

3, NOM PRIORITY:5。

2, MIN PRIORITY:1;

    線程默認的優先級爲5。

作用:提高線程搶奪CPU執行權的頻率,但不能保證一定就是哪個優先。

語法:

Thread thread = new Thread();

thread.setPriority(3);//設置線程的優先級。

---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity開發</a>、<a href="http://www.itheima.com"target="blank">.Net培訓</a>、期待與您交流! ----------------------

join示例代碼:

package com.itheima.threaddemos.ThreadCommunication;


/**
 * 線程中join方法的測試類
 * @author wuyong
 *
 */
public class JoinMethodDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		JoinThread jt = new JoinThread();
		Thread t1 = new Thread(jt);
		Thread t2 = new Thread(jt);
		
		t1.start();
//		t1.join();//t1調用join方法,來獲得主線程的執行權,此時,主線程處於凍結狀態,要等到t1執行完畢時,主線程纔會執行。
		t2.start();
		for (int i = 0; i < 60; i++) {
//			System.out.println(Thread.currentThread().getName() + "---" + i);
		}
	}

}
/**
 * 自定義的線程類
 * @author wuyong
 *
 */
class JoinThread implements Runnable{

	@Override
	public void run() {
		for (int i = 0; i < 30; i++) {
			System.out.println(Thread.currentThread().getName() + "..." + i);
			Thread.yield();//使當前線程暫停。執行其他線程。
		}
	}
	
}
發佈了25 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章