Java Code Examples for java.lang.Thread.State#RUNNABLE

The following examples show how to use java.lang.Thread.State#RUNNABLE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OverviewController.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** taken from sun.misc.VM
 * 
 * Returns Thread.State for the given threadStatus
 */
private static Thread.State toThreadState(int threadStatus) {
    if ((threadStatus & JVMTI_THREAD_STATE_RUNNABLE) != 0) {
        return State.RUNNABLE;
    } else if ((threadStatus & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) != 0) {
        return State.BLOCKED;
    } else if ((threadStatus & JVMTI_THREAD_STATE_WAITING_INDEFINITELY) != 0) {
        return State.WAITING;
    } else if ((threadStatus & JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT) != 0) {
        return State.TIMED_WAITING;
    } else if ((threadStatus & JVMTI_THREAD_STATE_TERMINATED) != 0) {
        return State.TERMINATED;
    } else if ((threadStatus & JVMTI_THREAD_STATE_ALIVE) == 0) {
        return State.NEW;
    } else {
        return State.RUNNABLE;
    }
}
 
Example 2
Source File: OverviewController.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
/** taken from sun.misc.VM
 * 
 * Returns Thread.State for the given threadStatus
 */
private static Thread.State toThreadState(int threadStatus) {
    if ((threadStatus & JVMTI_THREAD_STATE_RUNNABLE) != 0) {
        return State.RUNNABLE;
    } else if ((threadStatus & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) != 0) {
        return State.BLOCKED;
    } else if ((threadStatus & JVMTI_THREAD_STATE_WAITING_INDEFINITELY) != 0) {
        return State.WAITING;
    } else if ((threadStatus & JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT) != 0) {
        return State.TIMED_WAITING;
    } else if ((threadStatus & JVMTI_THREAD_STATE_TERMINATED) != 0) {
        return State.TERMINATED;
    } else if ((threadStatus & JVMTI_THREAD_STATE_ALIVE) == 0) {
        return State.NEW;
    } else {
        return State.RUNNABLE;
    }
}
 
Example 3
Source File: StackTraceSnapshotBuilder.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
final public void addStacktrace(Map<String, Object>[] infoMap, long dumpTimeStamp) throws IllegalStateException {
    List<SampledThreadInfo> threads = new ArrayList<>(infoMap.length);

    for (Map<String,Object> threadInfo : infoMap) {
        String name = (String) threadInfo.get("name");
        StackTraceElement[] stack = (StackTraceElement[]) threadInfo.get("stack");
        long tid = (Long) threadInfo.get("tid");
        Long threadCpuTime = (Long) threadInfo.get("threadCpuTime");
        State state = (State) threadInfo.get("state");

        if (threadCpuTime == null) {
            threadCpuTime = Long.valueOf(-1);   // no thread cpu time
        }
        if (state == null) {
            state = State.RUNNABLE;
        }
        SampledThreadInfo i = new SampledThreadInfo(name, tid, state, stack, threadCpuTime.longValue(), filter);

        threads.add(i);
    }
    addStacktrace(threads.toArray(new SampledThreadInfo[0]), dumpTimeStamp);
}
 
Example 4
Source File: IODetectorTask.java    From LagMonitor with MIT License 5 votes vote down vote up
@Override
public void run() {
    //According to this post the thread is still in Runnable although it's waiting for
    //file/http resources
    //https://stackoverflow.com/questions/20795295/why-jstack-out-says-thread-state-is-runnable-while-socketread
    if (mainThread.getState() == State.RUNNABLE) {
        //Based on this post we have to check the top element of the stack
        //https://stackoverflow.com/questions/20891386/how-to-detect-thread-being-blocked-by-io
        StackTraceElement[] stackTrace = mainThread.getStackTrace();
        StackTraceElement topElement = stackTrace[stackTrace.length - 1];
        if (topElement.isNativeMethod()) {
            //Socket/SQL (connect) - java.net.DualStackPlainSocketImpl.connect0
            //Socket/SQL (read) - java.net.SocketInputStream.socketRead0
            //Socket/SQL (write) - java.net.SocketOutputStream.socketWrite0
            if (isElementEqual(topElement, "java.net.DualStackPlainSocketImpl", "connect0")
                    || isElementEqual(topElement, "java.net.SocketInputStream", "socketRead0")
                    || isElementEqual(topElement, "java.net.SocketOutputStream", "socketWrite0")) {
                actionManager.logCurrentStack("Server is performing {1} on the main thread. "
                        + "Properly caused by {0}", "java.net.SocketStream");
            } //File (in) - java.io.FileInputStream.readBytes
            //File (out) - java.io.FileOutputStream.writeBytes
            else if (isElementEqual(topElement, "java.io.FileInputStream", "readBytes")
                    || isElementEqual(topElement, "java.io.FileOutputStream", "writeBytes")) {
                actionManager.logCurrentStack("Server is performing {1} on the main thread. " +
                        "Properly caused by {0}", "java.io.FileStream");
            }
        }
    }
}
 
Example 5
Source File: SaturativeExecutor.java    From ACDD with MIT License 5 votes vote down vote up
protected boolean isSaturated() {
    if (getPoolSize() <= 3) {
        return DEBUG;
    }
    int corePoolSize = getCorePoolSize();
    int i = CountedTask.mNumRunning.get();
    int size = mThreads.size();
    if (i < corePoolSize || i < size) {
        return true;
    }
    boolean z;
    synchronized (mThreads) {
        Iterator<Thread> it = mThreads.iterator();
        size = 0;
        while (it.hasNext()) {
            State state = it.next().getState();
            if (state == State.RUNNABLE || state == State.NEW) {
                i = size + 1;
            } else {
                if (state == State.TERMINATED) {
                    it.remove();
                }
                i = size;
            }
            size = i;
        }
    }
    z = size >= corePoolSize;
    return z;
}
 
Example 6
Source File: IODetectorTask.java    From LagMonitor with MIT License 5 votes vote down vote up
@Override
public void run() {
    //According to this post the thread is still in Runnable although it's waiting for
    //file/http resources
    //https://stackoverflow.com/questions/20795295/why-jstack-out-says-thread-state-is-runnable-while-socketread
    if (mainThread.getState() == State.RUNNABLE) {
        //Based on this post we have to check the top element of the stack
        //https://stackoverflow.com/questions/20891386/how-to-detect-thread-being-blocked-by-io
        StackTraceElement[] stackTrace = mainThread.getStackTrace();
        StackTraceElement topElement = stackTrace[stackTrace.length - 1];
        if (topElement.isNativeMethod()) {
            //Socket/SQL (connect) - java.net.DualStackPlainSocketImpl.connect0
            //Socket/SQL (read) - java.net.SocketInputStream.socketRead0
            //Socket/SQL (write) - java.net.SocketOutputStream.socketWrite0
            if (isElementEqual(topElement, "java.net.DualStackPlainSocketImpl", "connect0")
                    || isElementEqual(topElement, "java.net.SocketInputStream", "socketRead0")
                    || isElementEqual(topElement, "java.net.SocketOutputStream", "socketWrite0")) {
                actionManager.logCurrentStack("Server is performing {1} on the main thread. "
                        + "Properly caused by {0}", "java.net.SocketStream");
            } //File (in) - java.io.FileInputStream.readBytes
            //File (out) - java.io.FileOutputStream.writeBytes
            else if (isElementEqual(topElement, "java.io.FileInputStream", "readBytes")
                    || isElementEqual(topElement, "java.io.FileOutputStream", "writeBytes")) {
                actionManager.logCurrentStack("Server is performing {1} on the main thread. " +
                        "Properly caused by {0}", "java.io.FileStream");
            }
        }
    }
}
 
Example 7
Source File: CPUSampler.java    From jvmtop with GNU General Public License v2.0 4 votes vote down vote up
public void update() throws Exception
  {
    boolean samplesAcquired = false;
    for (ThreadInfo ti : threadMxBean_.dumpAllThreads(false, false))
    {
      long cpuTime = threadMxBean_.getThreadCpuTime(ti.getThreadId());
      Long tCPUTime = threadCPUTime.get(ti.getThreadId());
      if (tCPUTime == null)
      {
        tCPUTime = 0L;
      }
      else
      {
      Long deltaCpuTime = (cpuTime - tCPUTime);

      if (ti.getStackTrace().length > 0
          && ti.getThreadState() == State.RUNNABLE
            ) {
          for (StackTraceElement stElement : ti.getStackTrace()) {
            if (isReallySleeping(stElement)) {
              break;
            }
            if (isFiltered(stElement)) {
            continue;
          }
          String key = stElement.getClassName() + "."
              + stElement.getMethodName();
          data_.putIfAbsent(key, new MethodStats(stElement.getClassName(),
              stElement.getMethodName()));
          data_.get(key).getHits().addAndGet(deltaCpuTime);
          totalThreadCPUTime_.addAndGet(deltaCpuTime);
            samplesAcquired = true;
          break;
        }
      }
      }
      threadCPUTime.put(ti.getThreadId(), cpuTime);
    }
    if (samplesAcquired)
{
  updateCount_.incrementAndGet();
}
  }