2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.velux.internal.development;
15 import java.lang.management.ManagementFactory;
16 import java.lang.management.ThreadInfo;
17 import java.lang.management.ThreadMXBean;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * This is a helper class for dealing with multiple threads and synchronization.
26 * It provides the following methods:
28 * <li>{@link #findDeadlocked} to print the current locking situation.</li>
32 * @author Guenther Schreiner - Initial contribution
35 public class Threads {
36 private static final Logger LOGGER = LoggerFactory.getLogger(Threads.class);
39 * ************************
40 * ***** Constructors *****
44 * Suppress default constructor for creating a non-instantiable class.
47 throw new AssertionError();
50 // Class access methods
53 * Print the current established locks with the associated threads.
55 * Finds cycles of threads that are in deadlock waiting to acquire
56 * object monitors or ownable synchronizers.
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.
63 * This method is designed for troubleshooting use, but not for
64 * synchronization control. It might be an expensive operation.
66 * @see ThreadMXBean#findDeadlockedThreads
68 public static void findDeadlocked() {
69 ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
70 long[] ids = tmx.findDeadlockedThreads();
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);
77 LOGGER.warn("findDeadlocked() done.");