join 有三个重载方法
源码中的介绍:英文不太好,翻译的不知道可口不
public final void join() throws InterruptedException
// Waits for this thread to die.
表示等待该线程的终止
public final void join(long millis) throws InterruptedException
// Waits at most {@code millis} milliseconds for this thread to
* die. A timeout of {@code 0} means to wait forever.
等待该线程终止的时间最长为 millis 毫秒。超时如果为 0 线程要一直等下去。
public final void join(long millis,int nanos) throws InterruptedException
* Waits at most {@code millis} milliseconds plus
* {@code nanos} nanoseconds for this thread to die.
等待该线程终止的时间最长为 millis 毫秒 + nanos 纳秒。
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.
*
* <p> Yield is a heuristic attempt to improve relative progression
* between threads that would otherwise over-utilise a CPU. Its use
* should be combined with detailed profiling and benchmarking to
* ensure that it actually has the desired effect.
*
* <p> It is rarely appropriate to use this method. It may be useful
* for debugging or testing purposes, where it may help to reproduce
* bugs due to race conditions. It may also be useful when designing
* concurrency control constructs such as the ones in the
* {@link java.util.concurrent.locks} package.
*/
为了防止一些线程独占CPU资源(这样其它的线程就得不到”响应”了).
可以让当前执行的线程”休息”一下.但是这种thread.yield() 调用,
并不保证下一个运行的线程就一定不是该线程.
可以考虑用Thread.sleep(long millis);
方法强制当前线程睡眠至少millis毫秒.
但是使用时要对该方法捕获.
调用方法很简单,只要在要睡眠的线程中加入
Thread.yield();