Autopsy  4.19.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ThreadUtils.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2017-2021 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.coreutils;
20 
21 import java.lang.management.ManagementFactory;
22 import java.lang.management.ThreadInfo;
23 import java.lang.management.ThreadMXBean;
24 import java.util.Arrays;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.TimeUnit;
27 import java.util.stream.Collectors;
28 
29 /*
30  * Concurrent programming utilities.
31  */
32 final public class ThreadUtils {
33 
34  private static final long DEFAULT_TIMEOUT = 5;
35  private static final TimeUnit DEFAULT_TIMEOUT_UNITS = TimeUnit.SECONDS;
36 
43  public static void shutDownTaskExecutor(ExecutorService executor) {
44  executor.shutdown();
45  boolean tasksCompleted = false;
46  while (!tasksCompleted) {
47  try {
48  tasksCompleted = executor.awaitTermination(DEFAULT_TIMEOUT, DEFAULT_TIMEOUT_UNITS);
49  } catch (InterruptedException ignored) {
50  /*
51  * Ignore interrupts. The policy implemented by this method is
52  * an unconditional wait.
53  */
54  }
55  }
56  }
57 
65  public static String generateThreadDump() {
66  StringBuilder threadDump = new StringBuilder(System.lineSeparator());
67  ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
68  ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);
69  for (ThreadInfo threadInfo : threadInfos) {
70  threadDump.append(threadInfo.toString());
71  threadDump.append("\n");
72  }
73 
74  long[] deadlockThreadIds = threadMXBean.findDeadlockedThreads();
75  if (deadlockThreadIds != null) {
76  threadDump.append("-------------------List of Deadlocked Thread IDs ---------------------");
77  String idsList = (Arrays
78  .stream(deadlockThreadIds)
79  .boxed()
80  .collect(Collectors.toList()))
81  .stream().map(n -> String.valueOf(n))
82  .collect(Collectors.joining("-", "{", "}"));
83  threadDump.append(idsList);
84  }
85  return threadDump.toString();
86  }
87 
88  private ThreadUtils() {
89  }
90 }
static void shutDownTaskExecutor(ExecutorService executor)

Copyright © 2012-2021 Basis Technology. Generated on: Thu Sep 30 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.