1. 程式人生 > >執行緒的常用方法

執行緒的常用方法

1.jion方法 在這裡插入圖片描述 注意: 在這裡插入圖片描述 如:下例中timeThread.start()若沒有則TimeThread執行緒不會執行,jion阻塞不起作用 例子:

package texts;

import java.text.SimpleDateFormat;
import java.util.Date;	
public class Home {

public static void main(String[] args) throws InterruptedException {
	TimeThread timeThread = new TimeThread();		
	timeThread.start();		
	new CounterThread(timeThread).start();//更強勢優先執行
}	
}
class CounterThread extends Thread{
	TimeThread timeThread;
	
public CounterThread(TimeThread timeThread) {
	this.timeThread = timeThread;
}

@Override
public void run() {
	for (int i = 0; i < 5; i++) {
		System.out.println(i);
		if (i==2) {
			try {
			timeThread.join();//執行join方法的執行緒和呼叫囧方法的執行緒不是同一個		jion阻塞CounterThread執行緒
			//故而TimeThread執行緒開始執行		
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
			
		}
		
	}
}	
}
class TimeThread extends Thread{

@Override
public void run() {
	for (int i = 0; i < 5; i++)  {
		Date date = new Date();
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		System.out.println(sd.format(date));
		try {
			sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}	
}

2.interrupt方法 結束執行緒在呼叫Object類的wait方法,jion方法,sleep方法過程中的阻塞狀態,並在呼叫wait,jion,sleep方法處產生InterruptedException異常 在這裡插入圖片描述 如上圖加上counterThread.interrupt();之後當i==2時本該被阻塞的CounterThread執行緒未被阻塞繼續輸出且報異常,輸出完後到TimeThread執行緒執行

3.setDaem方法 • setDaemon方法:用於將一個尚未呼叫執行緒start方法的執行緒設定為守護執行緒。守護執行緒主要用於為其他執行緒的執行提供服務(Java中的垃圾回收機制就是守護執行緒),這種執行緒屬於建立它的執行緒,守護執行緒隨著主執行緒的終止而終止。 例子:

package texts;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Home {

public static void main(String[] args) throws InterruptedException {
	TimeThread timeThread = new TimeThread();
	
	timeThread.setDaemon(true);
	timeThread.start();
	System.out.println("jjjjjjjjj");
}
}
class CounterThread extends Thread{

@Override
public void run() {
	for (int i = 0; i < 5; i++) {
		System.out.println(i);			
	}
}	
}
	class TimeThread extends Thread{

@Override
public void run() {
	while(true) {
		Date date = new Date();
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		System.out.println(sd.format(date));
		try {
			sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}	
}

則只輸出jjjjjjjjj

注意:執行緒中所啟動的其他非守護執行緒執行緒不會隨著該執行緒的結束而結束 加上CounterThread counterThread = new CounterThread(timeThread); counterThread.start();後則CounterThread 執行緒還會繼續執行

4.currentThread方法 返回當前正在執行的執行緒物件

例子:

程式碼1:

public class Test {
	public static void main(String[] args) {
		Thread thread = new TimeThread();
		System.out.println(thread);
		thread.start();
	}
}
class TimeThread extends Thread{
	@Override
	public void run() {
		Thread thread = Thread.currentThread();
		System.out.println(thread);
	}
}

程式碼2:

import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
	public static void main(String[] args) {
		new TimeThread().start();
	}
}
class TimeThread extends Thread{
	public TimeThread(){
		super("時間執行緒");
	}
	
	@Override
	public void run() {
		printTime();
	}
	
	public void printTime(){
		Thread thread = Thread.currentThread();
		String time = new SimpleDateFormat("HH:mm:ss").format(new Date());
		System.out.println(thread.getName()+",當前時間:"+time);
	}
}

程式碼3:

public class Test {
		public static void main(String[] args) {
			TimeThread timeThread = new TimeThread();
			System.out.println("########"+timeThread);
			timeThread.start();		
			timeThread.run();//方法不是執行緒,故而正在執行得到執行緒物件還是主執行緒
		}
	}
class TimeThread extends Thread{
	@Override
	public void run() {
		Thread thread = Thread.currentThread();
		System.out.println("@@@@@@@@"+thread);
	}
}

程式碼4:

public class Test {
	public static void main(String[] args) {
		TimeThread timeThread = new TimeThread();
		System.out.println("########"+timeThread);
		timeThread.start();			
		timeThread.main();
}
}
class TimeThread extends Thread{
	@Override
	public void run() {
		main();
	}
	
void main(){
	Thread thread = Thread.currentThread();
	System.out.println("@@@@@@@@"+thread);
}
}

5.isAlive方法 判定該執行緒是否處於就緒、執行或阻塞狀態,如果是則返回true,否則返回false

例子:

package texts;
public class Home {

public static void main(String[] args) {
	Thread thread = Thread.currentThread();
	new PrintThread(thread).start();
	System.out.println("main執行緒狀態:"+thread.isAlive());
}
}

class PrintThread extends Thread{

private Thread thread;

public PrintThread(Thread thread){
	this.thread = thread;
}

@Override
public void run() {
	try {
		sleep(1000);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	System.err.println("main執行緒狀態:"+thread.isAlive());
}
}

先輸出main執行緒狀態:true;此時執行sleep(1000);使 PrintThread 執行緒阻塞所以輸出為true。再輸出main執行緒狀態:false;因為 System.out.println(“main執行緒狀態:”+thread.isAlive());此時執行緒已經結束。