多執行緒02——執行緒狀態
阿新 • • 發佈:2022-03-29
多執行緒02——執行緒狀態
目錄五大狀態
具體每個過程的含義
方法 | 說明 |
---|---|
setPriority(int newPriority) | 更改執行緒的優先順序 |
static void sleep(long millis) | 在指定的毫秒數內讓當前正在執行的執行緒休眠 |
void join() | 等待該執行緒終止(VIP) |
static void yield() | 暫停當前正在執行的執行緒物件,並執行其他執行緒 |
void interrupt() | 中斷執行緒,別用這個方式 |
Boolean isAlive() | 測試執行緒是否處於活動狀態 |
停止執行緒
- 不推薦使用JDK提供的stop().
destroy()方法。[已廢棄] - 推薦執行緒自己停止下來
- 建議使用一個標誌位進行終止變數,當flag=false,則終止執行緒執行。
程式碼樣例
package com.hao.state; // 測試stop // 1. 建議執行緒正常停止==》利用次數,不建議死迴圈 // 2. 建議使用標誌位==》設定一個標誌位 // 3. 不要使用stop或者destroy等過時或者JDK不建議使用的方法 public class TestStop implements Runnable{ //1.設定一個標識位 private boolean flag = true; @Override public void run() { int i = 0; while(flag){ System.out.println("run...Thread"+i++); } } //2.設定一個公開的方法停止標誌位,轉換標誌位 public void stop(){ this.flag=false; } public static void main(String[] args) { TestStop testStop = new TestStop(); new Thread(testStop).start();//執行緒只做一件事情就是不停的重新整理 for (int i = 0; i < 1000; i++) { System.out.println("main"+i); if(i==900){ //呼叫stop方法,切換標誌位,讓執行緒停止 testStop.stop(); System.out.println("該執行緒停止了"); } } } }
執行緒休眠
- sleep(時間)指定當前執行緒阻塞的毫秒數;
- sleep存在異常InterruptedException;
- sleep時間達到後執行緒進入就緒狀態
- sleep可以模擬網路延時,倒計時等
- 每一個物件都有一個鎖,sleep不會釋放鎖
搶票的例子:
//票數 private int ticketNums=10; @Override public void run() { while(true){ if (ticketNums<=0){ break; } //模擬延時 try { Thread.sleep(200);//延時 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"-->拿到了第"+ticketNums--+"張票"); } } public static void main(String[] args) { TestThread4 t = new TestThread4(); new Thread(t,"小蜜蜂").start(); new Thread(t,"小欣宇").start(); new Thread(t,"小壯壯").start(); }
-
模擬網路延時:放大問題的發生性
-
-1張票出現了,說明此時執行緒不安全
模擬倒計時:
package com.hao.state;
//模擬倒計時
public class TestSleep2 {
public static void tenDown() throws InterruptedException {
int num = 10;
while(true){
Thread.sleep(1000);
System.out.println(num--);
if(num<=0){
break;
}
}
}
public static void main(String[] args) {
try {
tenDown();//內部靜態類可以直接呼叫
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
獲取當前系統時間:
-
System.currentTimeMillis());//獲取系統當前時間
-
new SimpleDateFormat("HH:mm:ss").format(startTime) //按照時間格式輸出
package com.hao.state;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;
//獲取目前系統時間
public class TestSleep2 {
public static void main(String[] args) {
//列印當前系統時間
Date startTime=new Date(System.currentTimeMillis());//獲取系統當前時間
while(true){
try {
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
startTime=new Date(System.currentTimeMillis());//更新系統當前時間
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
執行緒禮讓
Yield
- 禮讓執行緒,讓當前正在執行的執行緒暫停,但不阻塞
- 將執行緒從執行狀態轉為就緒狀態
- 讓CPU重新排程,禮讓不一定成功,看CPU心情
程式碼
package com.hao.state;
//測試禮讓執行緒
public class TestYield {
public static void main(String[] args) {
MyYield myYield = new MyYield();
new Thread(myYield,"A").start();
new Thread(myYield,"B").start();
}
}
class MyYield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"執行緒開始執行");
Thread.yield();//執行緒禮讓
System.out.println(Thread.currentThread().getName()+"執行緒停止執行");
}
}
執行緒強制執行
Join
- Join合併執行緒,待此執行緒執行完成後,再執行其他執行緒,其他執行緒阻塞
- 可以想象成插隊
package com.hao.state;
//測試join方法
//想象為插隊
public class TestJoin implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("執行緒vip來了");
}
}
public static void main(String[] args) throws InterruptedException {
//啟動我們的執行緒
TestJoin testJoin = new TestJoin();
Thread thread = new Thread(testJoin);
thread.start();
//主執行緒
for (int i = 0; i < 500; i++) {
if (i==200){
thread.join();//插隊
}
System.out.println("main"+i);
}
}
}
儘量少用,會讓程序堵塞
執行緒狀態觀測
- Thread.State
執行緒狀態,執行緒可以處於以下狀態之一:
-
NEW:尚未啟動的執行緒處於此狀態。
-
RUNNABLE:在Java虛擬機器中執行的執行緒處於此狀態。
-
BLOCKED:被阻塞等待監視器鎖定的執行緒處於此狀態。
-
WAITING:正在等待另一個執行緒執行特定動作的執行緒處於此狀態。
-
TIMED_ WAITING:正在等待另一個執行緒執行動作達到指定等待時間的執行緒處於此狀態。
-
TERMINATED:已退出的執行緒處於此狀態。
一個執行緒可以在給定時間點處於一個狀態。這些狀態是不反映任何作業系統執行緒狀態的虛擬機器狀態。
package com.hao.state;
// 觀察測試執行緒的狀態
public class TestState {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("/////");
});
//觀察狀態
Thread.State state = thread.getState();
System.out.println(state);//NEW
//觀察啟動後
thread.start();//啟動執行緒
state = thread.getState();
System.out.println(state);//Run
while(state!=Thread.State.TERMINATED){//只要執行緒不終止,就一直輸出狀態
Thread.sleep(100);
state = thread.getState();//更新執行緒狀態
System.out.println(state);//輸出狀態
}
}
}
注意:執行緒一旦死了,就不能在啟動
執行緒優先順序
- Java提供一個執行緒排程器來監控程式中啟動後進入就緒狀態的所有執行緒,執行緒排程器按照優先順序決定應該排程哪個執行緒來執行
- 執行緒的優先順序用數字表示,範圍從1-10
- Thread.MIN_PRIORITY=1;
- Thread.MAX_PRIORITY=10;
- Thread.NORM_PRIORITY=5;
- 使用以下方式改變或獲取優先順序
- getPriority().setPriority(int xxx)
package com.hao.state;
//測試執行緒的優先順序
public class TestPriority{
public static void main(String[] args) {
//主執行緒預設優先順序
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
MyPriority myPriority = new MyPriority();
Thread t1 = new Thread(myPriority);
Thread t2 = new Thread(myPriority);
Thread t3 = new Thread(myPriority);
Thread t4 = new Thread(myPriority);
Thread t5 = new Thread(myPriority);
Thread t6 = new Thread(myPriority);
// 先設定優先順序,再啟動
t1.start();
t2.setPriority(1);
t2.start();
t3.setPriority(4);
t3.start();
t4.setPriority(Thread.MAX_PRIORITY);//MAX_PRIORITY=10
t4.start();
}
}
class MyPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
}
}
優先順序低只是意味著獲得排程的概率低.並不是優先順序低就不會被呼叫了.這都是看CPU的排程(效能倒置)
守護(daemon)執行緒
- 執行緒分為使用者執行緒和守護執行緒
- 虛擬機器必須確保使用者執行緒執行完畢
- 虛擬機器不用等待守護執行緒執行完畢
- 如後臺記錄操作日誌,監控記憶體,垃圾回收等待。。。
package com.hao.state;
//測試守護執行緒
//上帝守護你
public class TestDaemon {
public static void main(String[] args) {
You you = new You();
God god = new God();
Thread thread = new Thread(god);
thread.setDaemon(true);//預設是false 表示是使用者執行緒 正常的執行緒都是使用者執行緒
thread.start();//上帝守護執行緒啟動
new Thread(you).start();//你 使用者執行緒啟動
}
}
//上帝
class God implements Runnable{
@Override
public void run() {
while(true){
System.out.println("上帝保佑著你");
}
}
}
//你
class You implements Runnable{
@Override
public void run() {
for (int i = 0; i < 36500; i++) {
System.out.println("你一生都開心的活著");
}
System.out.println("=====goodbye!world!");
}
}