1 package fr.ifremer.quadrige3.synchro.server.technical;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import com.google.common.collect.Lists;
28 import fr.ifremer.quadrige3.core.dao.technical.Assert;
29
30 import java.util.List;
31
32
33
34
35
36
37 public class ThreadUtils {
38
39 protected ThreadUtils() {
40
41 }
42
43 public static Thread getThreadById(long id) {
44 for (Thread t: Thread.getAllStackTraces().keySet()) {
45 if (t.getId() == id) {
46 return t;
47 }
48 }
49
50 return null;
51 }
52
53 public static Thread getThreadByName(String name) {
54 for (Thread t: Thread.getAllStackTraces().keySet()) {
55 if (name.equals(t.getName())) {
56 return t;
57 }
58 }
59
60 return null;
61 }
62
63 public static List<Thread> getThreadsByRegexName(String regex) {
64 List<Thread> result = Lists.newArrayList();
65
66 for (Thread t: Thread.getAllStackTraces().keySet()) {
67 String threadName = t.getName();
68 if (threadName.matches(regex)) {
69 result.add(t);
70 }
71 }
72
73 return result;
74 }
75
76 public static void stopThread(Thread t) {
77 Assert.notNull(t);
78
79 if (!t.isInterrupted()) {
80 t.interrupt();
81 }
82 }
83
84 }