java 執行緒淺解02[方法及屬性]

我就是曹總發表於2019-05-11

執行緒中主要方法,常量以及屬性

方法有:sleep,wait,join,yield,

常量有:MAX_PRIORITY(9),NORM_PRIORITY(5),MIN_PRIORITY(1),括號中數字分別代表執行緒優先順序,級別越高,所佔用CPU執行時間片越多,換而言之,就是先執行

sleep:執行緒睡眠,暫停執行緒對應滴時間執行,如下程式碼

package com.thread;

public class Thread02_Sleep {
	public static void main(String[] args) {
		// 繼承Thread啟動執行緒方式
		MywThreadSleep mt4 = new MywThreadSleep();
		mt4.start();
	}

}

class MywThreadSleep extends Thread {
	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			try {
				sleep(1000);
				System.out.println("睡眠了1000毫秒");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(" i am 1");
		}
	}
}




執行結果:
睡眠了1000毫秒
 i am 1
睡眠了1000毫秒
 i am 1
睡眠了1000毫秒
 i am 1
睡眠了1000毫秒
 i am 1
睡眠了1000毫秒
 i am 1




join:合併執行緒,也即是優先順序先執行

package com.thread;

public class Thread03_join {

	
public static void main(String[] args) {
	MyThread2 mt2=new MyThread2("abc");
	mt2.start();
	try {
		mt2.join();
		} catch (InterruptedException e) {
	}
	for (int i = 0; i < 10; i++) {
		System.out.println(" i am main thread");
	}
}
	
}


class MyThread2 extends Thread{
	MyThread2(String s){
		super(s);
	}
	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			System.out.println(" i am "+getName());
			try {
				sleep(1000);
			} catch (Exception e) {
				return;
			}
		}
	}
}



執行結果:
 i am abc
 i am abc
 i am abc
 i am abc
 i am abc
 i am abc
 i am abc
 i am abc
 i am abc
 i am abc
 i am main thread
 i am main thread
 i am main thread
 i am main thread
 i am main thread
 i am main thread
 i am main thread
 i am main thread
 i am main thread
 i am main thread



yield:暫停當前執行緒執行,只暫停一次


package com.thread;

//讓出執行緒
//優先順序越高,得到CPU執行滴時間越多
public class Thread04_yield {
	//共三條路徑
	public static void main(String[] args) {
		MyThread4 mt1=new MyThread4("執行緒1");
		MyThread4 mt2=new MyThread4("執行緒2");
		mt1.start();
		mt2.start();
		
	}
}

class MyThread4 extends Thread {
	MyThread4(String s) {
		super(s);
	}

	@Override
	public void run() {
		for (int i = 0; i < 100; i++) {
			System.out.println(getName()+"   "+i);
			try {
				sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (i % 10 == 0) {
				yield();
			}

		}
	}

}

執行結果:
執行緒1   0
執行緒2   0
執行緒2   1
執行緒1   1
執行緒1   2
執行緒2   2
執行緒1   3
執行緒2   3
執行緒1   4
執行緒2   4
執行緒2   5
執行緒1   5
執行緒1   6
執行緒2   6
執行緒2   7
執行緒1   7
執行緒1   8
執行緒2   8
執行緒1   9
執行緒2   9
執行緒1   10
執行緒2   10
執行緒1   11
執行緒2   11



setPriority:設定執行緒優先順序

package com.thread;

//讓出執行緒
//優先順序越高,得到CPU執行滴時間越多
public class Thread05_properties {
	//共三條路徑
	public static void main(String[] args) {
		Thread mt1=new Thread(new T1("t1"));
//		Thread mt2=new Thread(new T1("t2"));
//		mt1.start();
//		mt2.start();
		
		
		
		
		mt1.setPriority(Thread.NORM_PRIORITY+4);
		Thread mt2=new Thread(new T2());
		System.out.println(mt1.getPriority()+"  "+mt2.getPriority());
		mt1.start();
		mt2.start();
		
	}
}

class T1 implements Runnable{
	private String s="";
	T1(String s1){
		s=s1;
	}
	

	public void run() {
		System.out.println(Thread.currentThread().isAlive());
		for (int i = 0; i < 100; i++) {
			System.out.println(s+"t1 "+i);
		}		
	}
	
}

class T2 implements Runnable{

	public void run() {
		for (int i = 0; i < 100; i++) {
			System.out.println("t2 "+i);
		}	
	}
	
}



執行結果:
9  5
true
t1t1 0
t2 0
t1t1 1
t2 1
t1t1 2
t2 2
t1t1 3
t2 3
t1t1 4
t2 4
t1t1 5
t2 5
t1t1 6
t2 6
t1t1 7
t2 7
t1t1 8
t2 8
t1t1 9
t2 9
t1t1 10
t2 10
t1t1 11
t2 11
t1t1 12
t2 12
t1t1 13
t2 13
t1t1 14
t2 14
t1t1 15
t2 15
t1t1 16
t2 16
t1t1 17
t2 17
t1t1 18
t2 18
t1t1 19
t2 19
t1t1 20
t2 20
t1t1 21
t2 21
t1t1 22
t2 22
t1t1 23
t2 23
t1t1 24
t2 24
t1t1 25
t2 25
t1t1 26
t2 26
t1t1 27
t2 27
t1t1 28
t2 28
t1t1 29
t2 29
t1t1 30
t2 30
t1t1 31
t2 31
t1t1 32
t2 32
t1t1 33
t2 33
t1t1 34
t2 34
t1t1 35
t2 35
t1t1 36
t2 36
t1t1 37
t2 37
t1t1 38
t2 38
t1t1 39
t2 39
t1t1 40
t2 40
t1t1 41
t2 41
t1t1 42
t2 42
t1t1 43
t2 43
t1t1 44
t2 44
t1t1 45
t2 45
t1t1 46
t2 46
t1t1 47
t2 47
t1t1 48
t2 48
t1t1 49
t2 49
t1t1 50
t2 50
t1t1 51
t2 51
t1t1 52
t2 52
t1t1 53
t2 53
t1t1 54
t2 54
t1t1 55
t2 55
t1t1 56
t2 56
t1t1 57
t2 57
t1t1 58
t2 58
t1t1 59
t2 59
t1t1 60
t2 60
t1t1 61
t2 61
t1t1 62
t2 62
t1t1 63
t2 63
t1t1 64
t2 64
t1t1 65
t2 65
t1t1 66
t2 66
t1t1 67
t2 67
t1t1 68
t2 68
t1t1 69
t2 69
t1t1 70
t2 70
t1t1 71
t2 71
t1t1 72
t2 72
t1t1 73
t2 73
t1t1 74
t2 74
t1t1 75
t2 75
t1t1 76
t2 76
t1t1 77
t2 77
t1t1 78
t2 78
t1t1 79
t2 79
t1t1 80
t2 80
t1t1 81
t2 81
t1t1 82
t2 82
t1t1 83
t2 83
t1t1 84
t2 84
t1t1 85
t2 85
t1t1 86
t2 86
t1t1 87
t2 87
t1t1 88
t2 88
t1t1 89
t2 89
t1t1 90
t2 90
t1t1 91
t2 91
t1t1 92
t2 92
t1t1 93
t2 93
t1t1 94
t2 94
t1t1 95
t2 95
t1t1 96
t2 96
t1t1 97
t2 97
t1t1 98
t2 98
t1t1 99
t2 99








我就是曹總最後編輯於:5年前

內容均為作者獨立觀點,不代表八零IT人立場,如涉及侵權,請及時告知。

相關文章