PageRenderTime 19ms CodeModel.GetById 11ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast/src/main/java/com/hazelcast/util/ThreadWatcher.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 59 lines | 39 code | 5 blank | 15 comment | 7 complexity | 1a318699df4c5d1114d5d0a7962aad28 MD5 | raw file
 1/*
 2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
 3 *
 4 * Licensed under the Apache License, Version 2.0 (the "License");
 5 * you may not use this file except in compliance with the License.
 6 * You may obtain a copy of the License at
 7 *
 8 * http://www.apache.org/licenses/LICENSE-2.0
 9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.hazelcast.util;
18
19public class ThreadWatcher {
20    long start = 0;
21    long totalWait = 0;
22    long totalTime = 0;
23    long waitCount = 0;
24    long runCount = 0;
25
26    public void addWait(long totalWaitNanos, long now) {
27        if (start == 0) {
28            start = now;
29        } else {
30            totalWait += totalWaitNanos;
31            totalTime += (now - start);
32            start = now;
33        }
34        waitCount++;
35    }
36
37    public long incrementRunCount() {
38        long now = System.nanoTime();
39        if (start == 0) {
40            start = now;
41        } else {
42            totalTime += (now - start);
43            start = now;
44        }
45        return runCount++;
46    }
47
48    public ThreadStats publish(boolean running) {
49        long now = System.nanoTime();
50        totalTime += (now - start);
51        long totalRun = totalTime - totalWait;
52        int utilizationPercentage = (totalTime <= 0 || runCount <= 1) ? 0 : (int) ((totalRun * 100L) / totalTime);
53        ThreadStats threadStats = new ThreadStats(utilizationPercentage, runCount, waitCount, running);
54        start = now;
55        totalTime = 0;
56        totalWait = 0;
57        return threadStats;
58    }
59}