ee221215952f87c02b98a6635b08921c31080786
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.core.infrastructure.threading;
23
24 import org.slf4j.ext.XLogger;
25 import org.slf4j.ext.XLoggerFactory;
26
27 /**
28  * The Class ThreadingTestThread.
29  *
30  * @author Liam Fallon (liam.fallon@ericsson.com)
31  */
32 public class ThreadingTestThread implements Runnable {
33
34     // Logger for this class
35     private static final XLogger logger = XLoggerFactory.getXLogger(ThreadingTestThread.class);
36
37     private boolean interrupted = false;
38
39     private long counter = -1;
40
41     private String threadName;
42
43     /**
44      * {@inheritDoc}.
45      */
46     @Override
47     public void run() {
48         this.threadName = Thread.currentThread().getName();
49         if (logger.isDebugEnabled()) {
50             logger.debug("starting threading test thread \"" + threadName + "\" . . .");
51         }
52
53         while (!interrupted) {
54             counter++;
55             if (logger.isDebugEnabled()) {
56                 logger.debug("in threading test thread \"" + threadName + "\", counter=" + counter + " . . .");
57             }
58             if (!ThreadUtilities.sleep(50)) {
59                 interrupted = true;
60             }
61         }
62
63         if (logger.isDebugEnabled()) {
64             logger.debug("stopped threading test thread \"" + threadName + "\"");
65         }
66     }
67
68     /**
69      * Gets the name.
70      *
71      * @return the name
72      */
73     public String getName() {
74         return threadName;
75     }
76
77     /**
78      * Interrupt.
79      */
80     public void interrupt() {
81         interrupted = true;
82     }
83
84     /**
85      * Gets the counter.
86      *
87      * @return the counter
88      */
89     public Long getCounter() {
90         return counter;
91     }
92 }