a2c51539f56661fbc9feb232591d184c20d17e8f
[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 import java.util.concurrent.CountDownLatch;
28
29 /**
30  * The Class ThreadingTestThread.
31  *
32  * @author Liam Fallon (liam.fallon@ericsson.com)
33  */
34 public class ThreadingTestThread implements Runnable {
35
36     // Logger for this class
37     private static final XLogger logger = XLoggerFactory.getXLogger(ThreadingTestThread.class);
38
39     private boolean interrupted = false;
40
41     private long counter = -1;
42
43     private String threadName;
44
45     private CountDownLatch latch = new CountDownLatch(1);
46
47    /**
48      * {@inheritDoc}.
49      */
50     @Override
51     public void run() {
52         this.threadName = Thread.currentThread().getName();
53         if (logger.isDebugEnabled()) {
54             logger.debug("starting threading test thread \"" + threadName + "\" . . .");
55         }
56
57         while (!interrupted) {
58             counter++;
59             if (logger.isDebugEnabled()) {
60                 logger.debug("in threading test thread \"" + threadName + "\", counter=" + counter + " . . .");
61             }
62             if(!ThreadUtilities.sleep(50)) {
63                 interrupted = true;
64             }
65         }
66
67         if (logger.isDebugEnabled()) {
68             logger.debug("stopped threading test thread \"" + threadName + "\"");
69         }
70     }
71
72     /**
73      * Gets the name.
74      *
75      * @return the name
76      */
77     public String getName() {
78         return threadName;
79     }
80
81     /**
82      * Interrupt.
83      */
84     public void interrupt() {
85         interrupted = true;
86     }
87
88     /**
89      * Gets the counter.
90      *
91      * @return the counter
92      */
93     public Long getCounter() {
94         return counter;
95     }
96 }