標籤:star ble 常用 orm 一點 完成 對象 方法總結 main
Java線程常用方法總結
1.join()方法
join —— 讓一個線程等待另一個線程完成才繼續執行。如A線程線程執行體中調用B線程的join()方法,則A線程被阻塞,知道B線程執行完為止,A才能得以繼續執行。
public class MyRunnable implements Runnable{ @Override public void run() { for(int i=0;i<5;i++){ System.out.println(Thread.currentThread().getName() + " " + i); } }}
public class Main { public static void main(String[] args) throws InterruptedException { Runnable runnable = new MyRunnable(); Thread thread = new Thread(runnable); for(int i=0;i<5;i++){ System.out.println("main:"+Thread.currentThread().getName() + " " + i); if(i==2){ thread.start(); thread.join(); } } }}
運行結果:
main:main 0main:main 1main:main 2Thread-0 0Thread-0 1Thread-0 2Thread-0 3Thread-0 4main:main 3main:main 4
2.Thread.sleep()方法
sleep 是線程類(Thread)的方法,導致此線程暫停執行指定時間,給執行機會給其他線程,但是監控狀態依然保持,到時後會自動回復,調用sleep 不會釋放對象鎖。由於沒有釋放對象鎖,所以不能調用裡面的同步方法。
public class Main { private int j = 10; Object obj = new Object(); public static void main(String[] args) throws InterruptedException { Main main = new Main(); Runnable r1 = main.new MyRunnable(); Runnable r2 = main.new MyRunnable(); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); } class MyRunnable implements Runnable { public void run() { synchronized(obj){ for (int i = 0; i < 5; i++) { j++; System.out.println("線程:"+Thread.currentThread()+"i:"+i+",j:"+j+",time:"+ System.currentTimeMillis()); if(i==2){ try { System.out.println("線程:"+Thread.currentThread()+"睡眠"); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } } } }}
運行結果:
線程:Thread[Thread-0,5,main]i:0,j:11,time:1503114191497線程:Thread[Thread-0,5,main]i:1,j:12,time:1503114191497線程:Thread[Thread-0,5,main]i:2,j:13,time:1503114191497線程:Thread[Thread-0,5,main]睡眠線程:Thread[Thread-0,5,main]i:3,j:14,time:1503114193500線程:Thread[Thread-0,5,main]i:4,j:15,time:1503114193500線程:Thread[Thread-1,5,main]i:0,j:16,time:1503114193500線程:Thread[Thread-1,5,main]i:1,j:17,time:1503114193500線程:Thread[Thread-1,5,main]i:2,j:18,time:1503114193501線程:Thread[Thread-1,5,main]睡眠線程:Thread[Thread-1,5,main]i:3,j:19,time:1503114195502線程:Thread[Thread-1,5,main]i:4,j:20,time:1503114195502
從結果看,Thread-0休眠後沒有釋放鎖,所以Thread-1並沒有執行,直到Thread-0休眠結束並執行完畢後Thread-1才開始執行。
3.Thread.yield( )方法
調用yield方法會讓當前線程交出CPU許可權,讓CPU去執行其他的線程。它跟sleep方法類似,同樣不會釋放鎖。但是yield不能控制具體的交出CPU的時間,另外,yield方法只能讓擁有相同優先順序的線程有擷取CPU執行時間的機會。
注意,調用yield方法並不會讓線程進入阻塞狀態,而是讓線程重回就緒狀態,它只需要等待重新擷取CPU執行時間,這一點是和sleep方法不一樣的。
public class Main { public static void main(String[] args) throws InterruptedException { Main main = new Main(); Thread t1 = main.new MyThread(); t1.start(); } class MyThread extends Thread{ public void run() { long beginTime=System.currentTimeMillis(); int count=0; for (int i=0;i<50000000;i++){ count=count+(i+1); Thread.yield(); } long endTime=System.currentTimeMillis(); System.out.println("用時:"+(endTime-beginTime)+" 毫秒!"); } }}
運行結果:
用時:4448 毫秒!
把Thread.yield();注釋掉,運行結果:
用時:1 毫秒!
4.getPriority和setPriority
用來擷取和設定線程優先順序。
Thread t1 = new Thread();t1.setPriority(Thread.MAX_PRIORITY);
MAX_PRIORITY:10
MIN_PRIORITY:1
NORM_PRIORITY:5
5.setDaemon和isDaemon
用來設定線程是否成為守護線程和判斷線程是否是守護線程。
Thread t1 = new Thread();t1.setDaemon(true);
守護線程(後台線程)主要是為其他線程提供服務。如JVM中的記憶體回收線程。
守護線程的生命週期與前台線程生命週期有一定關聯。主要體現在:當所有的前台線程都進入死亡狀態時,後台線程會自動死亡。
Java學習筆記—多線程(二)