Как я могу выполнить код при выходе из потока
Я хочу выполнить код в самом конце, прежде чем поток умирает. Поэтому я ищу какой-то метод dispose(), tearDown() для потоков, гарантирующий выполнение определенных задач перед выходом из потока.
3 ответа
Решение
Вы можете обернуть код для выполнения в отдельный поток в своем собственном коде, который имеет try
/ finally
заблокировать, и вызвать run
метод "настоящий" Runnable
от try
, как это:
final Runnable realRunnable = ... // This is the actual logic of your thread
(new Thread(new Runnable() {
public void run() {
try {
realRunnable.run();
} finally {
runCleanupCode();
}
}
})).start();
Код runCleanupCode()
будет выполняться в том же потоке, который использовался для запуска логики вашего фактического потока.
Другие ответы не учитывают, что вы говорите о пуле потоков. Вот что вам нужно сделать:
private static class MyThreadFactory implements ThreadFactory {
public Thread newThread(final Runnable r) {
return new Thread() {
public void run() {
try {
r.run();
} finally {
// teardown code
}
}
};
}
}
public static void main(String[] args) {
ThreadPoolExecutor exec = new ThreadPoolExecutor(10, 20, 100, TimeUnit.SECONDS, null, new MyThreadFactory());
}
Взяв ответ dasblinkenlight немного дальше (слишком далеко?):
class ThreadWithCleanup extends Thread {
final Runnable main;
final Runnable cleanup;
ThreadWithCleanup(Runnable main, Runnable cleanup) {
this.main = main;
this.cleanup = cleanup;
}
@Override
public void run() {
try {
main.run();
} finally {
cleanup.run();
}
}
}
public class Demo {
public static void main(String[] args) {
Runnable m = new Runnable() {
@Override
public void run() {
System.out.println("Hello from main.");
throw new RuntimeException("Bleah!");
}
};
Runnable c = new Runnable() {
@Override
public void run() {
System.out.println("Hello from cleanup.");
}
};
ThreadWithCleanup threadWithCleanup = new ThreadWithCleanup(m, c);
threadWithCleanup.start();
try {
threadWithCleanup.join();
} catch (InterruptedException ex) {
}
}
}
И раньше я думал, что никогда не увижу законных оснований для расширения класса Thread!