]> git.basschouten.com Git - openhab-addons.git/blob
1335eb16491ed00811524d2693e2b332f33bb180
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.velux.internal.development;
14
15 import java.lang.management.ManagementFactory;
16 import java.lang.management.ThreadInfo;
17 import java.lang.management.ThreadMXBean;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * This is a helper class for dealing with multiple threads and synchronization.
25  *
26  * It provides the following methods:
27  * <ul>
28  * <li>{@link #findDeadlocked} to print the current locking situation.</li>
29  * </ul>
30  * <p>
31  *
32  * @author Guenther Schreiner - Initial contribution
33  */
34 @NonNullByDefault
35 public class Threads {
36     private static final Logger LOGGER = LoggerFactory.getLogger(Threads.class);
37
38     /*
39      * ************************
40      * ***** Constructors *****
41      */
42
43     /**
44      * Suppress default constructor for creating a non-instantiable class.
45      */
46     private Threads() {
47         throw new AssertionError();
48     }
49
50     // Class access methods
51
52     /**
53      * Print the current established locks with the associated threads.
54      * <P>
55      * Finds cycles of threads that are in deadlock waiting to acquire
56      * object monitors or ownable synchronizers.
57      *
58      * Threads are <em>deadlocked</em> in a cycle waiting for a lock of
59      * these two types if each thread owns one lock while
60      * trying to acquire another lock already held
61      * by another thread in the cycle.
62      * <p>
63      * This method is designed for troubleshooting use, but not for
64      * synchronization control. It might be an expensive operation.
65      *
66      * @see ThreadMXBean#findDeadlockedThreads
67      */
68     public static void findDeadlocked() {
69         ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
70         long[] ids = tmx.findDeadlockedThreads();
71         if (ids != null) {
72             ThreadInfo[] infos = tmx.getThreadInfo(ids, true, true);
73             LOGGER.warn("findDeadlocked() The following threads are deadlocked:");
74             for (ThreadInfo ti : infos) {
75                 LOGGER.warn("findDeadlocked(): {}.", ti);
76             }
77             LOGGER.warn("findDeadlocked() done.");
78         }
79     }
80 }