Thread Timer : Timer « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » Timer 




Thread Timer
      
/**
  * This file is part of VoteBox.
  
  * VoteBox is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 3 as published by
  * the Free Software Foundation.
  
  * You should have received a copy of the GNU General Public License
  * along with VoteBox, found in the root of any distribution or
  * repository containing all or part of VoteBox.
  
  * THIS SOFTWARE IS PROVIDED BY WILLIAM MARSH RICE UNIVERSITY, HOUSTON,
  * TX AND IS PROVIDED 'AS IS' AND WITHOUT ANY EXPRESS, IMPLIED OR
  * STATUTORY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, WARRANTIES OF
  * ACCURACY, COMPLETENESS, AND NONINFRINGEMENT.  THE SOFTWARE USER SHALL
  * INDEMNIFY, DEFEND AND HOLD HARMLESS RICE UNIVERSITY AND ITS FACULTY,
  * STAFF AND STUDENTS FROM ANY AND ALL CLAIMS, ACTIONS, DAMAGES, LOSSES,
  * LIABILITIES, COSTS AND EXPENSES, INCLUDING ATTORNEYS' FEES AND COURT
  * COSTS, DIRECTLY OR INDIRECTLY ARISING OUR OF OR IN CONNECTION WITH
  * ACCESS OR USE OF THE SOFTWARE.
 */

//package verifier.util;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;

public class ThreadTimer {
  // helpful
  //
  public static final long NS_PER_MS = 1000000;

  public static long getThreadTime(long tid) {
    ThreadMXBean tb = ManagementFactory.getThreadMXBean();
    return tb.getThreadCpuTime(tid);
  }
  public static long getThreadTimeMilli(long tid) {
    return getThreadTime(tid/ NS_PER_MS;
  }
  public static long getCurrentThreadTime() {
    ThreadMXBean tb = ManagementFactory.getThreadMXBean();
    return tb.getCurrentThreadCpuTime();
  }
  public static long getCurrentThreadTimeMilli() {
    return getCurrentThreadTime() / NS_PER_MS;
  }
  
  // members
  //
  long _tid; /// thread ID
  long _start_ns; 
  long _stop_ns;

  // ctors
  //
  public ThreadTimer(long tid) {
    _tid = tid;
    _start_ns = _stop_ns = 0;
  }
  public static ThreadTimer timerForThread(Thread t) {
    return new ThreadTimer(t.getId());
  }
  public static ThreadTimer timerForCurrentThread() {
    return new ThreadTimer(Thread.currentThread().getId());
  }

  // methods
  //
  public long getTimeMilli() {
    return getThreadTimeMilli(_tid);
  }
  public long getTime() {
    return getThreadTime(_tid);
  }

  public void start() {
    _stop_ns = 0;
    _start_ns = getTime();
  }
  public long look() {
    if (_stop_ns > 0L)
      return _stop_ns - _start_ns;
    if (_start_ns > 0L)
      return getTime() - _start_ns;
    return 0L;
  }
  public long lookMilli() { return look() / NS_PER_MS; }
  public long stop() {
    if (_start_ns > 0)
      _stop_ns = getTime();
    return _stop_ns - _start_ns;
  }
  public void resume() {
    _stop_ns = 0;
  }
}

   
    
    
    
    
    
  














Related examples in the same category
1.Timer Skipping BeepTimer Skipping Beep
2.Timer Schedule a task that executes once every secondTimer Schedule a task that executes once every second
3.Use java.util.Timer to schedule a task to execute once 5 seconds have passedUse java.util.Timer to schedule a task to execute once 5 seconds have passed
4.Timer utilities
5.Timer and TimerTask Classes
6.Pause and start a timer task
7.Create a Timer object
8.Swing also provide a Timer class. A Timer object will send an ActionEvent to the registered ActionListener.
9.Schedule a task by using Timer and TimerTask.
10.Scheduling a Timer Task to Run Repeatedly
11.Create a scheduled task using timer
12.extends TimerTask to create your own task
13.A simple implementation of the Java 1.3 java.util.Timer API
14.Scheduling a Timer Task to Run at a Certain Time
15.Class encapsulating timer functionality
16.Timeout Observer
17.A pool of objects that should only be used by one thread at a time
18.A class that allows a programmer to determine the amount of time spend doing certain routines
19.All times in this Timer object are in milliseconds. The timer starts from the moment of it's creation.
20.Cancel Aware Timer
21.HRTimer is a simple system-wide timer facility using a singleton Timer, with additional instrumentation.
22.Used for timing events with millisecond precision.
23.A class to wait until a condition becomes true.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.