Thread

让主线程等待子线程的运行

方法一:join,理解成等待子线程终止,可用于等待一个或多个子线程的结果,然后主线程再继续执行。

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运行结束后才结束

说明我们做到了让父线程(Thread1)等待子线程(Thread2)

 

 

 2.使用FLAG标志位,就是设一个全局变量,子线程没结束就一直while()

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;
    }
}

 

运行结果,可以看到我们成功了,但是我不喜欢这种方法,感觉很卑鄙

 

3.CountDownLatch 通过检查子线程的个数来实现的

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();//运行结束,数量减少
    }
}

 

 

不用说太多

posted @ 2016-04-24 15:52  dreamOwn  阅读(242)  评论(0编辑  收藏  举报