當前位置: 首頁>>代碼示例>>Java>>正文


Java ThreadMXBean.getThreadCpuTime方法代碼示例

本文整理匯總了Java中java.lang.management.ThreadMXBean.getThreadCpuTime方法的典型用法代碼示例。如果您正苦於以下問題:Java ThreadMXBean.getThreadCpuTime方法的具體用法?Java ThreadMXBean.getThreadCpuTime怎麽用?Java ThreadMXBean.getThreadCpuTime使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.lang.management.ThreadMXBean的用法示例。


在下文中一共展示了ThreadMXBean.getThreadCpuTime方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getAllThreadInfo

import java.lang.management.ThreadMXBean; //導入方法依賴的package包/類
public static List<TInfo> getAllThreadInfo(){

        List<TInfo> threads = new ArrayList<>();
        ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
        long[] ids = threadBean.getAllThreadIds();
        ThreadInfo[] infos = threadBean.getThreadInfo(ids);
        for (ThreadInfo info : infos){
            long id = info.getThreadId();
            TInfo tInfo = new TInfo();
            tInfo.name = info.getThreadName();
            tInfo.id = id;
            tInfo.state = info.getThreadState();
            tInfo.cpuTime = threadBean.getThreadCpuTime(id);
            threads.add(tInfo);
        }
        Collections.sort(threads,new Comparator<TInfo>() {
            @Override
            public int compare(TInfo o1, TInfo o2) {
                return Long.compare(o2.cpuTime,o1.cpuTime);
            }
        });
        return threads;
    }
 
開發者ID:ctripcorp,項目名稱:cornerstone,代碼行數:24,代碼來源:ThreadingManager.java

示例2: getJvmThreadInstCpuTimeNs

import java.lang.management.ThreadMXBean; //導入方法依賴的package包/類
/**
 * Getter for the "JvmThreadInstCpuTimeNs" variable.
 */
public Long getJvmThreadInstCpuTimeNs() throws SnmpStatusException {
    long l = 0;
    final ThreadMXBean tmb = JvmThreadingImpl.getThreadMXBean();

    try {
        if (tmb.isThreadCpuTimeSupported()) {
            l = tmb.getThreadCpuTime(info.getThreadId());
            log.debug("getJvmThreadInstCpuTimeNs", "Cpu time ns : " + l);

            //Cpu time measurement is disabled or the id is not valid.
            if(l == -1) l = 0;
        }
    } catch (UnsatisfiedLinkError e) {
        // XXX Revisit: catch TO BE EVENTUALLY REMOVED
        log.debug("getJvmThreadInstCpuTimeNs",
                  "Operation not supported: " + e);
    }
    return new Long(l);
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:23,代碼來源:JvmThreadInstanceEntryImpl.java

示例3: main

import java.lang.management.ThreadMXBean; //導入方法依賴的package包/類
public static void main(String argv[]) {

        ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
        int cnt = 0;
        long [] idArr = {0, -1, -2, (Long.MIN_VALUE + 1), Long.MIN_VALUE};

        if (mbean.isThreadCpuTimeSupported()) {
            for (int i = 0; i < idArr.length; i++) {
                try {
                    mbean.getThreadCpuTime(idArr[i]);
                    System.out.println("Test failed. IllegalArgumentException" +
                        " expected for ID = " + idArr[i]);
                } catch (IllegalArgumentException iae) {
                    cnt++;
                }
            }
            if (cnt != idArr.length) {
                throw new RuntimeException("Unexpected number of " +
                    "IllegalArgumentException = " + cnt +
                    " expected = " + idArr.length);
            }

            // CPU time for a non-existence thread
            long time = mbean.getThreadCpuTime(999999);
            if (time < 0 && time != -1) {
                throw new RuntimeException("Cpu time for thread 999999" +
                    " is invalid = " + time + " expected to be -1.");
            }
        }
        System.out.println("Test passed.");
    }
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:32,代碼來源:InvalidThreadID.java

示例4: doThreadMeasurement

import java.lang.management.ThreadMXBean; //導入方法依賴的package包/類
/**
 * Does the thread measurement. For this, a single thread will be started and the results 
 * will be transfered to the specified {@link ThreadProtocolReceiver}.
 * 
 * @see ThreadProtocolReceiver
 *
 * @param timestamp the time stamp of the initial request
 * @param threadProtocolReceiver the {@link ThreadProtocolReceiver}
 */
public static void doThreadMeasurement(final long timestamp, final ThreadProtocolReceiver threadProtocolReceiver) {

	if (timestamp!=threadMeasurementLastTimeStamp) {

		// --- Remind this time stamp in order to avoid double work -------
		threadMeasurementLastTimeStamp = timestamp;
		
		// --- Start Thread to do0 the work -------------------------------
		Runnable threadMeasurement = new Runnable() {
			@Override
			public void run() {
				
				// --- Create a protocol instance -------------------------
				ThreadProtocol tp = new ThreadProtocol(timestamp, getLoadCPU());

				// --- Configure ThreadMXBean if possible and needed ------ 
				ThreadMXBean tmxb = ManagementFactory.getThreadMXBean();
		        if (tmxb.isThreadCpuTimeSupported()==true){
		        	if(tmxb.isThreadCpuTimeEnabled()==false){
		        		tmxb.setThreadCpuTimeEnabled(true);  
		        	}	
		        } else{
		        	System.err.println("ThreadTimeMeasurement not supported !!");
		        	threadProtocolReceiver.receiveThreadProtocol(null);
		        	return;
		        }
		        
		        // --- Do measurement ------------------------------------- 
		        String threadName;
		        long nanosCpuTime = 0L;
		        long nanosUserTime = 0L;
		        long factorMiliseconds = 1000000;

		        long[] threadIDs = tmxb.getAllThreadIds();
		        for (int i = 0; i < threadIDs.length; i++) {
		        	
		        	long threadID = threadIDs[i];
		            nanosCpuTime = tmxb.getThreadCpuTime(threadID);
		            nanosUserTime = tmxb.getThreadUserTime(threadID);
		            if (nanosCpuTime==-1 || nanosUserTime==-1) {
		            	continue;   // Thread died
		            }

		        	threadName = tmxb.getThreadInfo(threadID).getThreadName();
		        	// --- add times, converted to milliseconds, to thread-Protocol
		        	tp.getThreadDetails().add(new ThreadDetail(threadName, (nanosCpuTime/factorMiliseconds), (nanosUserTime/factorMiliseconds)));
		        }
		        // --- Send protocol to the requester of the measurement --
		        threadProtocolReceiver.receiveThreadProtocol(tp);
			}
		};
		// --- Start measurement thread -----------------------------------
		threadMeasurement.run();
	}
}
 
開發者ID:EnFlexIT,項目名稱:AgentWorkbench,代碼行數:65,代碼來源:LoadMeasureThread.java


注:本文中的java.lang.management.ThreadMXBean.getThreadCpuTime方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。