当前位置: 首页>>代码示例>>Scala>>正文


Scala ThreadInfo类代码示例

本文整理汇总了Scala中java.lang.management.ThreadInfo的典型用法代码示例。如果您正苦于以下问题:Scala ThreadInfo类的具体用法?Scala ThreadInfo怎么用?Scala ThreadInfo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了ThreadInfo类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: ContentionSnapshot

//设置package包名称以及导入依赖的类
package com.twitter.jvm

import java.lang.Thread.State._
import java.lang.management.{ManagementFactory, ThreadInfo}


class ContentionSnapshot {
  ManagementFactory.getThreadMXBean.setThreadContentionMonitoringEnabled(true)

  case class Snapshot(
    blockedThreads: Seq[String],
    lockOwners: Seq[String],
    deadlocks: Seq[String])

  private[this] object Blocked {
    def unapply(t: ThreadInfo): Option[ThreadInfo] = {
      t.getThreadState match {
        case BLOCKED | WAITING | TIMED_WAITING => Some(t)
        case _ => None
      }
    }
  }

  def snap(): Snapshot = {
    val bean = ManagementFactory.getThreadMXBean

    val blocked = bean.getThreadInfo(bean.getAllThreadIds, true, true)
                      .filter(_ != null)
                      .collect { case Blocked(info) => info }

    val ownerIds = blocked map(_.getLockOwnerId) filter(_ != -1)
    val owners = if (ownerIds.length == 0) Seq[String]() else
      bean.getThreadInfo(ownerIds.toArray, true, true).map(_.toString).toSeq

    val deadlockThreadIds = bean.findDeadlockedThreads()
    val deadlocks = if (deadlockThreadIds == null) Array.empty[ThreadInfo] else
      deadlockThreadIds.flatMap { id =>
        blocked.find { threadInfo =>
          threadInfo.getThreadId() == id
        }
      }

    Snapshot(
      blockedThreads = blocked.map(_.toString).toSeq,
      lockOwners = owners,
      deadlocks = deadlocks.map(_.toString).toSeq)
  }
} 
开发者ID:lanshuijuntuan,项目名称:Java.util,代码行数:49,代码来源:ContentionSnapshot.scala


注:本文中的java.lang.management.ThreadInfo类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。