java线程-如何用join实现T1、T2、T3线程,按照T3、T2、T1的顺序执行完毕

首先看一下join方法的内容和注释

直接看翻译,大概意思是

1、如果调用join方法时,如果时间参数为0,则是永远wait,和直接调wait方法很像

2、如果调用join方法时,时间参数大于0,就等待那么久,超时返回

3、这个join方法,用了一个while isAlive实现循环等待,判断只要当前线程活着,就调用wait,这是实现插队执行的关键,只有插队的线程执行完了,我才能上前执行,这个isAlive就是完成顺序执行的关键

4、此方法可以被notifyAll结束

5、推荐应用程序中,不要在Thread用wait、notify和notifyAll,这句话我还存疑,需要理解理解

/**
     * Waits at most {@code millis} milliseconds for this thread to
     * die. A timeout of {@code 0} means to wait forever.
     *
     * <p> This implementation uses a loop of {@code this.wait} calls
     * conditioned on {@code this.isAlive}. As a thread terminates the
     * {@code this.notifyAll} method is invoked. It is recommended that
     * applications not use {@code wait}, {@code notify}, or
     * {@code notifyAll} on {@code Thread} instances.
     *
     * @param  millis
     *         the time to wait in milliseconds
     *
     * @throws  IllegalArgumentException
     *          if the value of {@code millis} is negative
     *
     * @throws  InterruptedException
     *          if any thread has interrupted the current thread. The
     *          <i>interrupted status</i> of the current thread is
     *          cleared when this exception is thrown.
     */
    // BEGIN Android-changed: Synchronize on separate lock object not this Thread.
    // public final synchronized void join(long millis)
    public final void join(long millis)
    throws InterruptedException {
        synchronized(lock) {
        long base = System.currentTimeMillis();
        long now = 0;

        if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }

        if (millis == 0) {
            while (isAlive()) {
                lock.wait(0);
            }
        } else {
            while (isAlive()) {
                long delay = millis - now;
                if (delay <= 0) {
                    break;
                }
                lock.wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
        }
    }

实际操作

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private Button bt1;
    private Button bt2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Thread3 t3 = new Thread3();
        Thread2 t2 = new Thread2(t3);
        Thread1 t1 = new Thread1(t2);


        bt1 = findViewById(R.id.testBt);
        bt1.setOnClickListener(v -> {
            Log.d("ZTL","Button1");
            t1.start();
        });


        bt2 = findViewById(R.id.testBt2);
        bt2.setOnClickListener(v -> {
            Log.d("ZTL","Button2");

        });

    }


}
public class Thread1 extends Thread {

    Thread2 t2;

    public Thread1(Thread2 thread2){
        t2 = thread2;
    }

    @Override
    public void run() {
        super.run();
        Log.d("ZTL","thread1 run! start....");
        try {
            t2.start();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.d("ZTL","thread1 run! End!");
    }
}
public class Thread2 extends Thread {

    Thread3 t3;

    public Thread2(Thread3 thread3){
        t3 = thread3;
    }

    @Override
    public void run() {
        super.run();
        Log.d("ZTL","thread2 run! start....");
        try {
            t3.start();
            t3.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.d("ZTL","thread2 run! End!");
    }
}
public class Thread3 extends Thread {
    @Override
    public void run() {
        super.run();
        Log.d("ZTL","thread3 run! start....");
        try {
            sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.d("ZTL","thread3 run! End!");
    }
}

最后实际打印效果为:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值