本章将详细介绍两个线程调度方法:线程插队方法-join(),线程让步方法-yield()

一、join()方法

join(),又叫线程插队方法。thread.join()是把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程。比如在线程B中调用了线程A的join()方法,直到线程A执行完毕后,才会继续执行线程B。

t.join(); //调用join方法,等待线程t执行完毕
t.join(1000); //等待 t 线程,等待时间是1000毫秒。

代码实例

public class ThreadJoinDemo {

    //线程间的共享资源
    volatile static String config = "配置未被初始化.";

    public static void main(String[] args) throws InterruptedException {
            Runnable runnable = new A();
            Thread a = new Thread(runnable, "线程a");
            a.start();
    }

}

class A implements Runnable{

    @Override
    public void run() {
        Runnable runnable = new B();
        Thread b = new Thread(runnable, "线程b");
        b.start();
        try {
            b.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(ThreadJoinDemo.config);
    }
}

class B implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " 已启动");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ThreadJoinDemo.config = "配置已经被初始化";
        System.out.println(Thread.currentThread().getName() + " 已初始化配置");

    }
}

运行结果:

线程b 已启动
线程b 已初始化配置
配置已经被初始化

线程a调用了线程b的join(),然后等待线程b初始化配置完毕后,线程a才继续执行,获取到被线程b初始化过的代码。

那么插队执行是怎么实现的呢?请看以下源码

/**
 *  Waits at most <code>millis</code> milliseconds for this thread to  
 * die. A timeout of <code>0</code> means to wait forever.    
 */

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);
        }
    } else {
        while (isAlive()) {
            long delay = millis - now;
            if (delay <= 0) {
                break;
            }
            wait(delay);
            now = System.currentTimeMillis() - base;
        }
    }
}

核心代码是Object类的wait()的调用(关于wait()方法可以参考细说Object之wait())。join()方法调用了其重载方法join(0),注意该方法上的synchronized关键字,这个方法是一个同步方法,调用该方法,线程a需要先获得锁,这个锁就是b这个线程实例对象。该方法先校验参数合法性,然后调用isAlive()循环判断线程b是否存活,如果线程b存活,则调用wait(0),这时,持有b对象锁的线程a会释放锁,进入等待状态。

注意,线程b执行完毕后,线程a会被唤醒,但是上面源码中并没有看到notify调用,而且文档上表明不建议应用程序自己去调用,最终答案在jvm C++源码里,源码之下:

重点看ensure_join 方法里的 lock.notify_all(thread);语句代码说明jvm会在线程b执行完成后帮我们调用notify_all()方法,这样线程a就会重回Runnable状态,时间片分配后, 线程a可执行join方法之后的代码。

void JavaThread::run() {
  ...
  thread_main_inner();
}

void JavaThread::thread_main_inner() {
  ...
  this->exit(false);
  delete this;
}

void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
  ...
  // Notify waiters on thread object. This has to be done after exit() is called
  // on the thread (if the thread is the last thread in a daemon ThreadGroup the
  // group should have the destroyed bit set before waiters are notified).
  ensure_join(this);
  ...
}

static void ensure_join(JavaThread* thread) {
  // We do not need to grap the Threads_lock, since we are operating on ourself.
  Handle threadObj(thread, thread->threadObj());
  assert(threadObj.not_null(), "java thread object must exist");
  ObjectLocker lock(threadObj, thread);
  // Ignore pending exception (ThreadDeath), since we are exiting anyway
  thread->clear_pending_exception();
  // Thread is exiting. So set thread_status field in  java.lang.Thread class to TERMINATED.
  java_lang_Thread::set_thread_status(threadObj(), java_lang_Thread::TERMINATED);
  // Clear the native thread instance - this makes isAlive return false and allows the join()
  // to complete once we've done the notify_all below
  java_lang_Thread::set_thread(threadObj(), NULL);
  lock.notify_all(thread);
  // Ignore pending exception (ThreadDeath), since we are exiting anyway
  thread->clear_pending_exception();
}

二、yield()方法

yield(),又叫线程让步方法,其作用是使线程放弃当前拥有的cpu资源,回到RUNNABLE状态,以允许具有相同优先级的其他线程获得运行机会。因此,使用yield()的目的是让相同优先级的线程之间能适当的轮转执行。但是,实际中无法保证yield()达到让步目的,因为让步的线程还有可能被线程调度程序再次选中。

代码实例:

public class YieldTest implements Runnable {
    @Override
    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for(int i=0;i<5;i++){
            System.out.println(Thread.currentThread().getName() + ": " + i);
            Thread.yield();
        }
    }

    public static void main(String[] args) {
        YieldTest runn = new YieldTest();
        Thread t1 = new Thread(runn,"FirstThread");
        Thread t2 = new Thread(runn,"SecondThread");

        t1.start();
        t2.start();

    }
}

运行结果如下:

FirstThread: 0
SecondThread: 0
FirstThread: 1
SecondThread: 1
FirstThread: 2
SecondThread: 2
FirstThread: 3
SecondThread: 3
FirstThread: 4
SecondThread: 4

这个例子就是通过yield方法来实现两个线程的交替执行。不过请注意:这种交替并不一定能得到保证,源码中也对这个问题进行。