Thread

讓主線程等待子線程的運行ide

方法一:join,理解成等待子線程終止,可用於等待一個或多個子線程的結果,而後主線程再繼續執行。spa

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("Thread", "主線程開始運行");
        new Thread1().start();
        Log.e("Thread", "主線程結束運行");
    }
}

public class Thread1 extends Thread{
@Override
public void run() {
Log.e("Thread", "Thread1開始運行");
Thread2 thread2 = new Thread2();
thread2.start();
try {
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.e("Thread","Thread1結束運行");
}
}
 
 
public class Thread2 extends Thread{
@Override
public void run() {
Log.e("Thread", "Thread2開始運行");
try {
Thread2.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.e("Thread","Thread2結束運行");
}
}

 咱們在主線程中添加了一個thread1,在thread1中運行並加入thread2,在thread2中讓他延時1S。線程

咱們來看看結果,(主線程可能在Thread1開始前結束,可能在在開始後結束,這不是重點),Thread1在Thread2運行結束後才結束3d

說明咱們作到了讓父線程(Thread1)等待子線程(Thread2)code

 

 

 2.使用FLAG標誌位,就是設一個全局變量,子線程沒結束就一直while()blog

public class MainActivity extends AppCompatActivity {
    public static int FLAG = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new Thread1().start();
    }
}

public class Thread1 extends Thread{
    @Override
    public void run() {
        Log.e("Thread", "Thread1開始運行");
        Thread2 thread2 = new Thread2();
        thread2.start();
        while(MainActivity.FLAG != 1){
            //這裏能夠作點其餘的事情哦
        }
        MainActivity.FLAG = 0;
        Log.e("Thread","Thread1結束運行");
    }
}

public class Thread2 extends Thread{
    @Override
    public void run() {
        Log.e("Thread", "Thread2開始運行");
        try {
            Thread2.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.e("Thread","Thread2結束運行");
        MainActivity.FLAG = 1;
    }
}

 

運行結果,能夠看到咱們成功了,可是我不喜歡這種方法,感受很卑鄙it

 

3.CountDownLatch 經過檢查子線程的個數來實現的io

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new Thread1().start();
    }
}


public class Thread1 extends Thread{
    public static CountDownLatch countDownLatch = new CountDownLatch(1);//設置數量
    @Override
    public void run() {
        Log.e("Thread", "Thread1開始運行");
        Thread2 thread2 = new Thread2();
        thread2.start();
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.e("Thread","Thread1結束運行");
    }
}


public class Thread2 extends Thread{
    @Override
    public void run() {
        Log.e("Thread", "Thread2開始運行");
        try {
            Thread2.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.e("Thread","Thread2結束運行");
        Thread1.countDownLatch.countDown();//運行結束,數量減小
    }
}

 

 

不用說太多class

相關文章
相關標籤/搜索