Autopsy  4.21.0
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.io.BufferedReader;
22 import java.io.StringReader;
23 import java.lang.management.ManagementFactory;
24 import java.lang.management.ThreadInfo;
25 import java.lang.management.ThreadMXBean;
26 import java.util.Arrays;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.TimeUnit;
29 import java.util.stream.Collectors;
30 
31 /*
32  * Concurrent programming utilities.
33  */
34 final public class ThreadUtils {
35 
36  private static final long DEFAULT_TIMEOUT = 5;
37  private static final TimeUnit DEFAULT_TIMEOUT_UNITS = TimeUnit.SECONDS;
38 
45  public static void shutDownTaskExecutor(ExecutorService executor) {
46  executor.shutdown();
47  boolean tasksCompleted = false;
48  while (!tasksCompleted) {
49  try {
50  tasksCompleted = executor.awaitTermination(DEFAULT_TIMEOUT, DEFAULT_TIMEOUT_UNITS);
51  } catch (InterruptedException ignored) {
52  /*
53  * Ignore interrupts. The policy implemented by this method is
54  * an unconditional wait.
55  */
56  }
57  }
58  }
59 
67  public static String generateThreadDump() {
68  StringBuilder threadDump = new StringBuilder(System.lineSeparator());
69  ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
70  ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);
71  for (ThreadInfo threadInfo : threadInfos) {
72  // Break the stack trace into lines and then put back together using the
73  // appropriate line ending for the system.
74  threadDump.append(new BufferedReader(new StringReader(threadInfo.toString()))
75  .lines()
76  .collect(Collectors.joining(System.lineSeparator())));
77  threadDump.append(System.lineSeparator());
78  }
79 
80  long[] deadlockThreadIds = threadMXBean.findDeadlockedThreads();
81  if (deadlockThreadIds != null) {
82  threadDump.append("-------------------List of Deadlocked Thread IDs ---------------------");
83  threadDump.append(System.lineSeparator());
84  String idsList = (Arrays
85  .stream(deadlockThreadIds)
86  .boxed()
87  .collect(Collectors.toList()))
88  .stream().map(n -> String.valueOf(n))
89  .collect(Collectors.joining("-", "{", "}"));
90  threadDump.append(idsList);
91  }
92  return threadDump.toString();
93  }
94 
95  private ThreadUtils() {
96  }
97 }
static void shutDownTaskExecutor(ExecutorService executor)

Copyright © 2012-2022 Basis Technology. Generated on: Tue Feb 6 2024
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.