美文网首页
java 线程解读(C++)

java 线程解读(C++)

作者: 晏雪峤_美杜莎 | 来源:发表于2019-05-21 11:45 被阅读0次
    1. 线程状态: NEW, RUNNABLE, BLOCKED,WAITING, TIMED_WAITING, TERMINATED
    2. 线程方法:
      2.1 启动线程-start
    public synchronized void start() {
          /**
           * This method is not invoked for the main method thread or "system"
           * group threads created/set up by the VM. Any new functionality added
           * to this method in the future may have to also be added to the VM.
           *
           * A zero status value corresponds to state "NEW".
           */
          if (threadStatus != 0)
              throw new IllegalThreadStateException();
    
          /* Notify the group that this thread is about to be started
           * so that it can be added to the group's list of threads
           * and the group's unstarted count can be decremented. */
          group.add(this);
    
          boolean started = false;
          try {
              start0();
              started = true;
          } finally {
              try {
                  if (!started) {
                      group.threadStartFailed(this);
                  }
              } catch (Throwable ignore) {
                  /* do nothing. If start0 threw a Throwable then
                    it will be passed up the call stack */
              }
          }
      }
    private native void start0();
    

    2.2 中断线程方法-interrupt

      void os::interrupt(Thread* thread) {
      assert(Thread::current() == thread || Threads_lock->owned_by_self(),
        "possibility of dangling Thread pointer");
    
      OSThread* osthread = thread->osthread();
    
      if (!osthread->interrupted()) {
        osthread->set_interrupted(true);
        // More than one thread can get here with the same value of osthread,
        // resulting in multiple notifications.  We do, however, want the store
        // to interrupted() to be visible to other threads before we execute unpark().
        OrderAccess::fence();
        ParkEvent * const slp = thread->_SleepEvent ;
        if (slp != NULL) slp->unpark() ;
      }
    
      // For JSR166. Unpark even if interrupt status already was set
      if (thread->is_Java_thread())
        ((JavaThread*)thread)->parker()->unpark();
    
      ParkEvent * ev = thread->_ParkEvent ;
      if (ev != NULL) ev->unpark() ;
    
    }
    中断线程并不是停止线程,而是变更线程的中断状态(_interrupted), 停止线程需要相应的业务逻辑!!!
    C++{
    volatile jint _interrupted;     // Thread.isInterrupted state
    }
    

    2.3 查看中断状态方法-isInterrupted

     public boolean isInterrupted() {
           return isInterrupted(false);
       }
    
    bool os::is_interrupted(Thread* thread, bool clear_interrupted) {
      assert(Thread::current() == thread || Threads_lock->owned_by_self(),
        "possibility of dangling Thread pointer");
    
      OSThread* osthread = thread->osthread();
    
      bool interrupted = osthread->interrupted();
    
      if (interrupted && clear_interrupted) {
        osthread->set_interrupted(false);
        // consider thread->_SleepEvent->reset() ... optional optimization
      }
    
      return interrupted;
    }
    

    2.4 查看中断状态并清除中断标志-interrupted

      public static boolean interrupted() {
          return currentThread().isInterrupted(true);
      }
     返回当前线程的中断状态,并将中断标识设置为0;
    
      volatile bool interrupted() const                 { return _interrupted != 0; }
      void set_interrupted(bool z)                      { _interrupted = z ? 1 : 0; }
    

    相关文章

      网友评论

          本文标题:java 线程解读(C++)

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