关于后端:详谈线程间的协作waitnotifysleepyieldjoin

2次阅读

共计 10098 个字符,预计需要花费 26 分钟才能阅读完成。

如果竭尽本人最大致力依然还是一无所得,所剩下的只是凶恶意志,它诚如沉睡的宝石一样,本身就发射着耀目的光辉,本身之内就具备价值。

一、线程的状态

Java 中线程中状态可分为五种:New(新建状态),Runnable(就绪状态),Running(运行状态),Blocked(阻塞状态),Dead(死亡状态)。

  • New:新建状态,当线程创立实现时为新建状态,即 new Thread(…),还没有调用 start 办法时,线程处于新建状态。
  • Runnable:就绪状态,当调用线程的的 start 办法后,线程进入就绪状态,期待 CPU 资源。处于就绪状态的线程由 Java 运行时零碎的线程调度程序 (thread scheduler) 来调度。
  • Running:运行状态,就绪状态的线程获取到 CPU 执行权当前进入运行状态,开始执行 run 办法。
  • Blocked:阻塞状态,线程没有执行完,因为某种原因(如,I/ O 操作等)让出 CPU 执行权,本身进入阻塞状态。
  • Dead:死亡状态,线程执行实现或者执行过程中出现异常,线程就会进入死亡状态。

这五种状态之间的转换关系如下图所示:

有了对这五种状态的根本理解,当初咱们来看看 Java 中是如何实现这几种状态的转换的。

二、wait/notify/notifyAll 办法的应用

1、wait 办法:


JDK 中一共提供了这三个版本的办法,

  • wait()办法的作用是将以后运行的线程挂起(即让其进入阻塞状态),直到 notify 或 notifyAll 办法来唤醒线程.
  • wait(long timeout),该办法与 wait()办法相似,惟一的区别就是在指定工夫内,如果没有 notify 或 notifAll 办法的唤醒,也会主动唤醒。
  • 至于 wait(long timeout,long nanos),本意在于更准确的管制调度工夫,不过从目前版本来看,该办法貌似没有残缺的实现该性能,其源码 (JDK1.8) 如下:
public final void wait(long timeout, int nanos) throws InterruptedException {if (timeout < 0) {throw new IllegalArgumentException("timeout value is negative");
    }
    if (nanos < 0 || nanos > 999999) {
        throw new IllegalArgumentException("nanosecond timeout value out of range");
    }
    if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {timeout++;}
    wait(timeout);
}

从源码来看,JDK8 中对纳秒的解决,只做了四舍五入,所以还是依照毫秒来解决的,可能在将来的某个工夫点会用到纳秒级别的精度。尽管 JDK 提供了这三个版本,其实最初都是调用 wait(long timeout)办法来实现的,wait()办法与 wait(0)等效,而 wait(long timeout,int nanos)从下面的源码能够看到也是通过 wait(long timeout)来实现的。

上面咱们通过一个简略的例子来演示 wait()办法的应用:

package com.malf;

public class WaitTest {public void testWait() {System.out.println("Start-----");
        try {wait(1000);
        } catch (InterruptedException e) {e.printStackTrace();
        }
        System.out.println("End-------");
    }

    public static void main(String[] args) {final WaitTest test = new WaitTest();
        new Thread(new Runnable() {
            @Override
            public void run() {test.testWait();
            }
        }).start();}
}

这段代码的用意很简略,就是程序执行当前,让其暂停一秒,而后再执行。运行上述代码,查看后果:

Start-----
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at com.paddx.test.concurrent.WaitTest.testWait(WaitTest.java:8)
at com.paddx.test.concurrent.WaitTest$1.run(WaitTest.java:20)
at java.lang.Thread.run(Thread.java:745)

这段程序并没有按咱们的预期输入相应后果,而是抛出了一个异样。大家可能会感觉奇怪为什么会抛出异样?而抛出的 IllegalMonitorStateException 异样又是什么?咱们能够看一下 JDK 中对 IllegalMonitorStateException 的形容:

Thrown to indicate that a thread has attempted to wait on an object’s monitor or to notify other threads waiting on an object’s monitor without owning the specified monitor.

这句话的意思大略就是:线程试图期待对象的监视器或者试图告诉其余正在期待对象监视器的线程,但自身没有对应的监视器的所有权。

wait 办法是一个本地办法,其底层是通过一个叫做监视器锁的对象来实现的。所以下面之所以会抛出异样,是因为在调用 wait 形式时没有获取到 monitor 对象的所有权,那如何获取 monitor 对象所有权?

Java 中只能通过 Synchronized 关键字来实现,批改上述代码,减少 Synchronized 关键字:

package com.malf;

public class WaitTest {public synchronized void testWait() {// 减少 Synchronized 关键字
        System.out.println("Start-----");
        try {wait(1000);
        } catch (InterruptedException e) {e.printStackTrace();
        }
        System.out.println("End-------");
    }

    public static void main(String[] args) {final WaitTest test = new WaitTest();
        new Thread(new Runnable() {
            @Override
            public void run() {test.testWait();
            }
        }).start();}
}

当初再运行上述代码,就能看到预期的成果了:

Start-----
End-------

所以,通过这个例子,大家应该很分明,wait 办法的应用必须在同步的范畴内,否则就会抛出 IllegalMonitorStateException 异样,wait 办法的作用就是阻塞以后线程期待 notify/notifyAll 办法的唤醒,或期待超时后主动唤醒。

2、notify/notifyAll 办法


有了对 wait 办法原理的了解,notify 办法和 notifyAll 办法就很容易了解了。既然 wait 形式是通过对象的 monitor 对象来实现的,所以只有在同一对象下来调用 notify/notifyAll 办法,就能够唤醒对应对象 monitor 上期待的线程了。

notify 和 notifyAll 的区别在于前者只能唤醒 monitor 上的一个线程,对其余线程没有影响,而 notifyAll 则唤醒所有的线程,看上面的例子很容易了解这两者的差异:

package com.malf;

public class NotifyTest {public synchronized void testWait() {System.out.println(Thread.currentThread().getName() + "Start-----");
        try {wait(0);
        } catch (InterruptedException e) {e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "End-------");
    }

    public static void main(String[] args) throws InterruptedException {final NotifyTest test = new NotifyTest();
        for (int i = 0; i < 5; i++) {new Thread(new Runnable() {
                @Override
                public void run() {test.testWait();
                }
            }).start();}
        synchronized (test) {test.notify();
        }
        Thread.sleep(3000);
        System.out.println("----------- 分割线 -------------");
        synchronized (test) {test.notifyAll();
        }
    }
}

输入后果如下:

Thread-0 Start-----
Thread-1 Start-----
Thread-2 Start-----
Thread-3 Start-----
Thread-4 Start-----
Thread-0 End-------
----------- 分割线 -------------
Thread-4 End-------
Thread-3 End-------
Thread-2 End-------
Thread-1 End-------

从后果能够看出:调用 notify 办法时只有线程 Thread- 0 被唤醒,然而调用 notifyAll 时,所有的线程都被唤醒了。

最初,有两点须要留神:

1. 调用 wait 办法后,线程是会开释对 monitor 对象的所有权的。

2. 一个通过 wait 办法阻塞的线程,必须同时满足以下两个条件能力被真正执行:

  • 线程须要被唤醒(超时唤醒或调用 notify/notifyll)。
  • 线程唤醒后须要竞争到锁(monitor)。

三、sleep/yield/join 办法解析

下面咱们曾经分明了 wait 和 notify 办法的应用和原理,当初咱们再来看另外一组线程间合作的办法。这组办法跟下面办法的最显著区别是:这几个办法都位于 Thread 类中,而下面三个办法都位于 Object 类中。至于为什么,大家能够先思考一下。当初咱们一一剖析 sleep/yield/join 办法:

1、sleep

sleep 办法的作用是让以后线程暂停指定的工夫(毫秒),sleep 办法是最简略的办法,在上述的例子中也用到过,比拟容易了解。惟一须要留神的是其与 wait 办法的区别。最简略的区别是,wait 办法依赖于同步,而 sleep 办法能够间接调用。而更深层次的区别在于 sleep 办法只是临时让出 CPU 的执行权,并不开释锁。而 wait 办法则须要开释锁。

package com.malf;

public class SleepTest {public synchronized void sleepMethod() {System.out.println("Sleep start-----");
        try {Thread.sleep(1000);
        } catch (InterruptedException e) {e.printStackTrace();
        }
        System.out.println("Sleep end-----");
    }

    public synchronized void waitMethod() {System.out.println("Wait start-----");
        synchronized (this) {
            try {wait(1000);
            } catch (InterruptedException e) {e.printStackTrace();
            }
        }
        System.out.println("Wait end-----");
    }

    public static void main(String[] args) {final SleepTest test1 = new SleepTest();
        for (int i = 0; i < 3; i++) {new Thread(new Runnable() {
                @Override
                public void run() {test1.sleepMethod();
                }
            }).start();}
        try {Thread.sleep(10000);// 暂停十秒,等下面程序执行实现
        } catch (InterruptedException e) {e.printStackTrace();
        }
        System.out.println("----- 分割线 -----");

        final SleepTest test2 = new SleepTest();

        for (int i = 0; i < 3; i++) {new Thread(new Runnable() {
                @Override
                public void run() {test2.waitMethod();
                }
            }).start();}
    }
}

执行后果:

Sleep start-----
Sleep end-----
Sleep start-----
Sleep end-----
Sleep start-----
Sleep end-----
----- 分割线 -----
Wait start-----
Wait start-----
Wait start-----
Wait end-----
Wait end-----
Wait end-----

这个后果的区别很显著,通过 sleep 办法实现的暂停,程序是程序进入同步块的,只有当上一个线程执行实现的时候,下一个线程能力进入同步办法,sleep 暂停期间始终持有 monitor 对象锁,其余线程是不能进入的。而 wait 办法则不同,当调用 wait 办法后,以后线程会开释持有的 monitor 对象锁,因而,其余线程还能够进入到同步办法,线程被唤醒后,须要竞争锁,获取到锁之后再继续执行。

2、yield 办法

yield 办法的作用是暂停以后线程,以便其余线程有机会执行,不过不能指定暂停的工夫,并且也不能保障以后线程马上进行。yield 办法只是将 Running 状态转变为 Runnable 状态。咱们还是通过一个例子来演示其应用:

package com.malf;

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 办法来实现两个线程的交替执行。不过请留神:这种交替并不一定能失去保障,源码中也对这个问题进行阐明:

/**
 * 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.
 */

这段话次要阐明了三个问题:

  • 调度器可能会疏忽该办法。
  • 应用的时候要仔细分析和测试,确保能达到预期的成果。
  • 很少有场景要用到该办法,次要应用的中央是调试和测试。

3、join 办法


join 办法的作用是父线程期待子线程执行实现后再执行,换句话说就是将异步执行的线程合并为同步的线程。JDK 中提供三个版本的 join 办法,其实现与 wait 办法相似,join()办法实际上执行的 join(0),而 join(long millis, int nanos)也与 wait(long millis, int nanos)的实现形式统一,临时对纳秒的反对也是不残缺的。咱们能够看下 join 办法的源码,这样更容易了解:

public final void join() throws InterruptedException {join(0);
}

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;}
    }
}

public final synchronized void join(long millis, int nanos)
        throws InterruptedException {if (millis < 0) {throw new IllegalArgumentException("timeout value is negative");
    }
    if (nanos < 0 || nanos > 999999) {
        throw new IllegalArgumentException("nanosecond timeout value out of range");
    }
    if (nanos >= 500000 || (nanos != 0 && millis == 0)) {millis++;}
    join(millis);
}

大家重点关注一下 join(long millis)办法的实现,能够看出 join 办法就是通过 wait 办法来将线程的阻塞,如果 join 的线程还在执行,则将以后线程阻塞起来,直到 join 的线程执行实现,以后线程能力执行。

不过有一点须要留神,这里的 join 只调用了 wait 办法,却没有对应的 notify 办法,起因是 Thread 的 start 办法中做了相应的解决,所以当 join 的线程执行实现当前,会主动唤醒主线程持续往下执行。上面咱们通过一个例子来演示 join 办法的作用:

(1)不应用 join 办法:

package com.malf;

public class JoinTest implements Runnable {
    @Override
    public void run() {
        try {System.out.println(Thread.currentThread().getName() + "start-----");
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + "end------");
        } catch (InterruptedException e) {e.printStackTrace();
        }
    }

    public static void main(String[] args) {for (int i = 0; i < 5; i++) {Thread test = new Thread(new JoinTest());
            test.start();}
        System.out.println("Finished~~~");
    }
}

执行后果如下:

Thread-0 start-----
Thread-1 start-----
Thread-2 start-----
Thread-3 start-----
Finished~~~
Thread-4 start-----
Thread-2 end------
Thread-4 end------
Thread-1 end------
Thread-0 end------
Thread-3 end------

(2)应用 join 办法:

package com.malf;

public class JoinTest implements Runnable {
    @Override
    public void run() {
        try {System.out.println(Thread.currentThread().getName() + "start-----");
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + "end------");
        } catch (InterruptedException e) {e.printStackTrace();
        }
    }

    public static void main(String[] args) {for (int i = 0; i < 5; i++) {Thread test = new Thread(new JoinTest());
            test.start();
            try {test.join(); // 调用 join 办法
            } catch (InterruptedException e) {e.printStackTrace();
            }
        }
        System.out.println("Finished~~~");
    }
}

执行后果如下:

Thread-0 start-----
Thread-0 end------
Thread-1 start-----
Thread-1 end------
Thread-2 start-----
Thread-2 end------
Thread-3 start-----
Thread-3 end------
Thread-4 start-----
Thread-4 end------
Finished~~~

比照两段代码的执行后果很容易发现,在没有应用 join 办法之间,线程是并发执行的,而应用 join 办法后,所有线程是程序执行的。

四、总结

本文次要具体解说了 wait/notify/notifyAll 和 sleep/yield/join 办法。最初答复一下下面提出的问题:wait/notify/notifyAll 办法的作用是实现线程间的合作,那为什么这三个办法不是位于 Thread 类中,而是位于 Object 类中?位于 Object 中,也就相当于所有类都蕴含这三个办法(因为 Java 中所有的类都继承自 Object 类)。

要答复这个问题,还是得回过来看 wait 办法的实现原理,大家须要明确的是,wait 期待的到底是什么货色?如果对下面内容了解的比拟好的话,我置信大家应该很容易晓得 wait 期待其实是对象 monitor,因为 Java 中的每一个对象都有一个内置的 monitor 对象,天然所有的类都理当有 wait/notify 办法。

正文完
 0