Java提高——多线程(二)join、sleep、yield
join、sleep、yield都是Thread類的方法
join線程
join()方法:讓“主線程”線程等待“子線程”運行完之后再運行。
//子線程 public class son extends Thread(){void run(){......} } //主線程 public class F() extends Thread{void run(){son s = new son(); s.start(); s.join(); ...} }如:在主線程中調用子線程s.start()啟動子線程并調用s.join(),在調用s.join()之后主線程會一直等待,直到子線程運行完畢之后才接著運行。
源碼:
public final synchronized void join(long millis) throws InterruptedException {long base = System.currentTimeMillis(); long now = 0; if (millis < 0) {throw new IllegalArgumentException("timeout value is negative"); }if (millis == 0) {//判斷子線程是否還活著while (isAlive()) {wait(0);//讓當前CPU上運行的線程等待 }} else {while (isAlive()) {long delay = millis - now; if (delay <= 0) {break; }wait(delay); now = System.currentTimeMillis() - base; }} }用事實說話:
class MyThread1 extends Thread{public MyThread1(String name) {super(name); }@Override public void run(){System.out.println(this.getName()+" start\n"); //延時操作 for (int i = 0; i < 1000; i++) {}System.out.println(this.getName()+"finish\n"); } } public class Demo extends Thread {public static void main(String[] args) throws InterruptedException {MyThread1 t = new MyThread1("myThread"); t.start(); t.join(); System.out.println(currentThread().getName()+" finish"); } }myThread startmyThreadfinish
main finish
結果顯示一樣
線程睡眠
sleep()方法:讓當前線程休眠,指定休眠時間,從“運行狀態”進入“休眠(阻塞)狀態”。喚醒之后線程進入“就緒狀態”等待CPU調度。
class MyThread1 extends Thread{public MyThread1(String name) {super(name); }@Override public void run(){System.out.println(this.getName()+" start\n"); //延時操作 for (int i = 0; i < 1000; i++) {if(i%4==0){//如果成立線程睡覺100mstry {Thread.sleep(100); } catch (InterruptedException e) {e.printStackTrace(); }}}System.out.println(this.getName()+"finish\n"); } } public class Demo extends Thread {public static void main(String[] args) throws InterruptedException {MyThread1 thread = new MyThread1("myThread"); thread.start(); thread.join(); System.out.println(currentThread().getName()+" finish"); } }sleep & wait
wait()的作用也是能夠讓線程從“運行狀態”進入“休眠(阻塞)狀態”,同時釋放同步鎖。但是sleep不會釋放同步鎖。
public class SleepLockTest {static Object object = new Object(); public static void main(String[] args) {ThreadA ta = new ThreadA("t1"); ThreadA tb = new ThreadA("t2"); ta.start(); tb.start(); }static class ThreadA extends Thread{public ThreadA(String name) {super(name); }@Override public void run(){//獲取object對象的同步鎖 synchronized (object){for (int i = 0; i < 10; i++) {System.out.println(this.getName()+" "+i); if (i%4==0){try {Thread.sleep(100); } catch (InterruptedException e) {e.printStackTrace(); }}}}}} }ta、tb會獲得同一個對象的同步鎖,ta在運行中會執行Thread.sleep(100),但是tb不會獲得CPU的執行權,因為ta沒有釋放同步鎖。
注釋掉同步鎖之后:ta、tb可以互相切換
線程讓步?
yield():和sleep類似,讓線程從“運行狀態”到“就緒狀態”,但是不會阻塞線程,只是讓當前線程停一會兒,讓同優先級的其他線程獲得被執行的機會,但是不保證一定會被執行。
yield&wait
1)wait讓線程從“運行狀態”到“阻塞狀態”,yield讓線程從“運行狀態”到“就緒狀態”
2)wait會釋放同步鎖,yield和sleep一樣不會釋放同步鎖。
轉載請注明出處:http://www.cnblogs.comskywang12345/p/3479256.html
轉載于:https://www.cnblogs.com/huangzhe1515023110/p/9276096.html
總結
以上是生活随笔為你收集整理的Java提高——多线程(二)join、sleep、yield的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【AI应用】三分钟,带你了解英特尔明年重
- 下一篇: elk,logstash -elastc