join的作用在源码中就一句话Waits for this thread to die
其实这句话有点不准确,它少了一个主语:父线程。完整的表述应该是parent thread waits for this thread to die。意思就是调用这个线程的父线程会处于阻塞状态知道这个线程执行完。比如:
public static void main(String[] args) throws InterruptedException
{
Thread thread = new Thread(new Runnable()
{
public void run()
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("new Thread");
}
});
thread.start();
thread.join();
System.out.println("main thread");
}
这里打印信息是 new Thread
main Thread
线程thread的父线程是主线程,因此thread调用join()方法后主线程阻塞知道thread执行完
yield方法作用其实也可以用一句话来表述:表示愿意让出当前调度的使用权
源码解释的第一段是:A hint to the scheduler that the current thread is willing to yield
its current use of a processor. The scheduler is free to ignore this hint.
暗示调度者这个线程愿意让出对当前调度的使用,调度者可以无视这个暗示。
讲道理对于计算机来说准确性很重要我只能说对这个方法很无语