多线程

作者: 雪国落叶 | 来源:发表于2020-04-25 21:19 被阅读0次

    先看下Thread类中之前没有太多关注的类定义:

    public
    class Thread implements Runnable {
        /* Make sure registerNatives is the first thing <clinit> does. */
    
        /**
         * The synchronization object responsible for this thread's join/sleep/park operations.
         */
        private final Object lock = new Object();
    
        private volatile long nativePeer;
    /* ThreadLocal values pertaining to this thread. This map is maintained
         * by the ThreadLocal class. */
        ThreadLocal.ThreadLocalMap threadLocals = null;
    
        /*
         * InheritableThreadLocal values pertaining to this thread. This map is
         * maintained by the InheritableThreadLocal class.
         */
        ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
    
    public final void join(long millis) throws InterruptedException {
            synchronized(lock) {
            long base = System.currentTimeMillis();
            long now = 0;
    
            if (millis < 0) {
                throw new IllegalArgumentException("timeout value is negative");
            }
    
            if (millis == 0) {
                while (isAlive()) {
                    lock.wait(0);
                }
            } else {
                while (isAlive()) {
                    long delay = millis - now;
                    if (delay <= 0) {
                        break;
                    }
                    lock.wait(delay);
                    now = System.currentTimeMillis() - base;
                }
            }
            }
        }
    

    join

    从上面看到实际上检测isAlive,因此线程未开始执行时,是不生效的;
    关键点调用了lock.wait函数,那么在哪里notifyAll()呢?
    在线程将要退出的时候,要查看jvm中的实现
    ensure_join方法中,调用 lock.notify_all(thread); 唤醒所有等待thread锁的线程。

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

    final线程安全

    而final可以防止此类事情的发生:如果某个成员是final的,JVM规范做出如下明确的保证:一旦对象引用对其他线程可见,则其final成员也必须正确的赋值了。

    volatile

    相关文章

      网友评论

          本文标题:多线程

          本文链接:https://www.haomeiwen.com/subject/yqcbbhtx.html