下面列出了怎么用org.apache.commons.lang3.ThreadUtils的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Note that this method is trying to remove references to the
* NewtWindowListener as it's preventing the MapViewTileRenderer from being
* garbage collected. This is still a work in progress.
*/
@Override
public void dispose() {
if (glComponent != null) {
glComponent = null;
}
finished = true;
if (surface != null && surface.stopThread() && g != null) {
g.dispose();
}
surface = null;
// initSurface() creates HighResTimerThread threads that don't die so kill them manually
final Collection<Thread> threadsToKill = ThreadUtils.findThreadsByName("HighResTimerThread");
for (final Thread thread : threadsToKill) {
thread.interrupt();
}
}
public void execute(Integer count) throws Exception {
new Thread() {
public void run() {
try {
Map<Long, Integer> state_new = new HashMap<>();
Map<Long, Integer> state_runable = new HashMap<>();
Map<Long, Integer> state_blocked = new HashMap<>();
Map<Long, Integer> state_waiting = new HashMap<>();
Map<Long, Integer> state_timed_waiting = new HashMap<>();
Map<Long, Integer> state_terminated = new HashMap<>();
File startFile = null;
for (int i = 1; i <= count; i++) {
Date date = new Date();
StringBuffer buffer = new StringBuffer();
for (Thread thread : ThreadUtils.getAllThreads()) {
state(thread, state_new, state_runable, state_blocked, state_waiting,
state_timed_waiting, state_terminated);
dump(buffer, thread);
}
File file = new File(Config.dir_logs(true), fileName(i, count, date));
if (i == 1) {
startFile = file;
}
if (i != count) {
FileUtils.write(file, buffer, DefaultCharset.charset_utf_8);
logger.print(String.format("thread dump to file(%d/%d): %s.", i, count,
file.getAbsolutePath()));
Thread.sleep(5000);
} else {
buffer.append(System.lineSeparator()).append("Thread state statistics:");
buffer.append(" NEW(" + state_new.size() + "),");
buffer.append(" RUNABLE(" + state_runable.size() + "),");
buffer.append(" blocked(" + state_blocked.size() + "),");
buffer.append(" waiting(" + state_waiting.size() + "),");
buffer.append(" timed_waiting(" + state_timed_waiting.size() + "),");
buffer.append(" terminated(" + state_terminated.size() + ").");
buffer.append(System.lineSeparator());
writeState(buffer, state_new, State.NEW, count);
writeState(buffer, state_runable, State.RUNNABLE, count);
writeState(buffer, state_blocked, State.BLOCKED, count);
writeState(buffer, state_waiting, State.WAITING, count);
writeState(buffer, state_timed_waiting, State.TIMED_WAITING, count);
writeState(buffer, state_terminated, State.TERMINATED, count);
FileUtils.write(file, buffer, DefaultCharset.charset_utf_8);
logger.print(String.format("thread dump to files: %s - %s.",
startFile.getAbsolutePath(), file.getAbsolutePath()));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}