01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18:
19: package org.apache.jmeter.threads;
20:
21: /**
22: * @version $Revision: 493779 $
23: */
24: public final class JMeterContextService {
25: static private ThreadLocal threadContext = new ThreadLocal() {
26: public Object initialValue() {
27: return new JMeterContext();
28: }
29: };
30:
31: private static long testStart = 0;
32:
33: private static int numberOfThreads = 0; // Active thread count
34:
35: private static int totalThreads = 0;
36:
37: /**
38: * Private constructor to prevent instantiation.
39: */
40: private JMeterContextService() {
41: }
42:
43: static public JMeterContext getContext() {
44: return (JMeterContext) threadContext.get();
45: }
46:
47: static public void startTest() {//TODO should this be synchronized?
48: if (testStart == 0) {
49: numberOfThreads = 0;
50: testStart = System.currentTimeMillis();
51: threadContext = new ThreadLocal() {
52: public Object initialValue() {
53: return new JMeterContext();
54: }
55: };
56: }
57: }
58:
59: static synchronized void incrNumberOfThreads() {
60: numberOfThreads++;
61: }
62:
63: static synchronized void decrNumberOfThreads() {
64: numberOfThreads--;
65: }
66:
67: static public synchronized int getNumberOfThreads() {
68: return numberOfThreads;
69: }
70:
71: static public void endTest() {//TODO should this be synchronized?
72: testStart = 0;
73: numberOfThreads = 0;
74: }
75:
76: static public long getTestStartTime() {// NOT USED
77: return testStart;
78: }
79:
80: public static synchronized int getTotalThreads() {
81: return totalThreads;
82: }
83:
84: public static synchronized void addTotalThreads(int this Group) {
85: totalThreads += this Group;
86: }
87:
88: public static synchronized void clearTotalThreads() {
89: totalThreads = 0;
90: }
91: }
|