关于多线程中sleep、join、yield的区别

好了、说了多线程,那就不得不说说多线程的sleep()、join()和yield()三个方法的区别啦java

   一、sleep()方法多线程

 /**
     * Causes the currently executing thread to sleep (temporarily cease
     * execution) for the specified number of milliseconds, subject to
     * the precision and accuracy of system timers and schedulers. The thread
     * does not lose ownership of any monitors.
     *  意思是说:当前正在执行的线程休眠(暂时中止执行)指定的毫秒数,具体取决于系统计时器和调度程序的精度和准确性。 该线程不会失去任何监视器的全部权。
     * @param  millis
     *         the length of time to sleep 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.
     */
    public static native void sleep(long millis) throws InterruptedException;

其实主要的就是他是让其余线程走,本身进行休眠,可是本身却不会释放对象锁,也就是说,若是有同步锁的时候,其余线程不能访问共享数据。并发

注意该方法要捕获异常 好比有两个线程同时执行(没有Synchronized),一个线程优先级为MAX_PRIORITY,另外一 个为MIN_PRIORITY,若是没有Sleep()方法,只有高优先级的线程执行完成后,低优先级 的线程才能执行;但当高优先级的线程sleep(5000)后,低优先级就有机会执行了。 总之,sleep()可使低优先级的线程获得执行的机会,固然也可让同优先级、高优先级的 线程有执行的机会。app

  二、yield() 方法oop

    /**
     * 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.
     * 它是一种启发式尝试,用于改善线程之间的相对进展,不然会过分利用CPU。 它的使用应与详细的分析和基准测试相结合,以确保它实际上具备所需的效果。
     * <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.
  * 使用这种方法不多是合适的。 它可能对调试或测试目的颇有用,它可能有助于重现因竞争条件而产生的错误。 在设计并发控制结构(如中的那些)时,它也可能颇有用
*/ public static native void yield();

yield() 这个方法从以上注释能够看出,也是一个休眠自身线程的方法,一样不会释放自身锁的标识,区别在于它是没有参数的,即yield()方 法只是使当前线程从新回到可执行状态,测试

因此执行yield()的线程有可能在进入到可执行状态 后立刻又被执行,另外yield()方法只能使同优先级或者高优先级的线程获得执行机会,这也 和sleep()方法不一样。this

 

三、join() 方法spa

这个方法比较有意思,Thread的非静态方法join()让一个线程B“加入”到另一个线程A的尾部。在A执行完毕以前, B不能工做。线程

/**
     * Waits for this thread to die.  
     *  等待线程死亡
     * <p> An invocation of this method behaves in exactly the same
     * way as the invocation
     *
     * <blockquote>
     * {@linkplain #join(long) join}{@code (0)}
     * </blockquote>
     *
     * @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.
     */
    public final void join() throws InterruptedException {
        join(0);  // 调用了有参方法
    }
  /**
     * 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.
     */
    public final synchronized void join(long millis)
    throws InterruptedException {

保证当前线程中止执行,直到该线程所加入的线程完成为止。然而,若是它加入的线程没有存活,则当前线程不须要中止。debug

相关文章
相关标签/搜索