Java提升——多線程(二)join、sleep、yield


join、sleep、yield都是Thread類的方法
html

join線程

join()方法:讓「主線程」線程等待「子線程」運行完以後再運行ide

//子線程 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()以後主線程會一直等待,直到子線程運行完畢以後才接着運行。this

源碼:spa

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 start

myThreadfinishcode

main finishorm

結果顯示同樣
htm

線程睡眠

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){//若是成立線程睡覺100ms
                try {
                    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不會釋放同步鎖blog

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

相關文章
相關標籤/搜索