SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
ChronoSingleThread.java
1/*
2 * Class: ChronoSingleThread
3 * Description: Compute the CPU time for a single thread
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author Éric Buist
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.util;
26
27import java.lang.management.ManagementFactory;
28import java.lang.management.ThreadMXBean;
29
38public class ChronoSingleThread extends AbstractChrono {
39 private long myThreadId;
40 static ThreadMXBean threadMXBean = null;
41
42 protected void getTime(long[] tab) {
43 long rawTime = getTime();
44 final long DIV = 1000000000L;
45 long seconds = rawTime / DIV;
46 long micros = (rawTime % DIV) / 1000L;
47 tab[0] = seconds;
48 tab[1] = micros;
49 }
50
51 protected long getTime() {
52 if (threadMXBean == null) {
53 // We use lazy initialization to avoid a potential exception being wrapped into
54 // a confusing
55 // ExceptionInInitializerError. That would happen if this initialization was in
56 // a static block instead of
57 // in this method.
58 threadMXBean = ManagementFactory.getThreadMXBean();
59 if (!threadMXBean.isThreadCpuTimeEnabled())
60 // Call this only when necessary, because this can throw a SecurityException if
61 // run under a security manager.
62 threadMXBean.setThreadCpuTimeEnabled(true);
63 }
64 long time = threadMXBean.getThreadCpuTime(myThreadId);
65 return time < 0 ? 0 : time;
66 }
67
73 super();
74 myThreadId = Thread.currentThread().getId();
75 init();
76 }
77
82 public ChronoSingleThread(Thread inThread) {
83 super();
84 myThreadId = inThread.getId();
85 init();
86 }
87
88}
void init()
Initializes this AbstractChrono to zero.
ChronoSingleThread()
Constructs a ChronoSingleThread object associated with current thread and initializes it to zero.
ChronoSingleThread(Thread inThread)
Constructs a ChronoSingleThread object associated with the given Thread variable and initializes it t...