如果我调用 return 会发生什么?从可运行?
What happens if I call return; from Runnable?
我有 Java Runnable,我正在执行 run()
方法。该 运行 方法与服务器建立了一些连接,当它失败时,我对线程执行不再感兴趣,我想退出它。我这样做:
class MyRunnable implements Runnable {
@Override
public void run() {
// connect to server here
if (it failed) {
return;
}
// do something else
}
}
现在我用我自己的线程工厂将这个 运行nable 提交到 Executors.cachedThreadPool()
,这基本上没有什么新东西。
我可以安全地 return 从那个 运行nable 那样吗?
我查看了 jvisualvm,发现线程池中有一个线程 + 有一些线程正在与服务器逻辑的连接中执行,当我 return 时,我看到这些连接线程是停止,他们确实留在列表中,但他们是白色的......
从 void
方法中使用 return
完全没问题。它只是 returns 来自该方法,在这种情况下将完成线程执行。
您不是在向执行器提交线程,而是在向其提交 Runnable。在 Runnable 中调用 return 不会导致执行它的线程终止。执行器的编写使其可以 运行 Runnables 形式的多个任务,当 Runnable 完成执行时(无论它是否早 returns 或其他)线程继续,并从中获得更多工作它排队提交的任务。
这是 ThreadPoolExecutor#运行Worker 方法中的代码。显示 task.run()
的行是工作线程执行任务的地方,当你的任务 returns 然后工作线程的执行从那里继续进行。
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
w.unlock(); // allow interrupts
boolean completedAbruptly = true;
try {
while (task != null || (task = getTask()) != null) {
w.lock();
// If pool is stopping, ensure thread is interrupted;
// if not, ensure thread is not interrupted. This
// requires a recheck in second case to deal with
// shutdownNow race while clearing interrupt
if ((runStateAtLeast(ctl.get(), STOP) ||
(Thread.interrupted() &&
runStateAtLeast(ctl.get(), STOP))) &&
!wt.isInterrupted())
wt.interrupt();
try {
beforeExecute(wt, task);
Throwable thrown = null;
try {
task.run();
} catch (RuntimeException x) {
thrown = x; throw x;
} catch (Error x) {
thrown = x; throw x;
} catch (Throwable x) {
thrown = x; throw new Error(x);
} finally {
afterExecute(task, thrown);
}
} finally {
task = null;
w.completedTasks++;
w.unlock();
}
}
completedAbruptly = false;
} finally {
processWorkerExit(w, completedAbruptly);
}
}
我有 Java Runnable,我正在执行 run()
方法。该 运行 方法与服务器建立了一些连接,当它失败时,我对线程执行不再感兴趣,我想退出它。我这样做:
class MyRunnable implements Runnable {
@Override
public void run() {
// connect to server here
if (it failed) {
return;
}
// do something else
}
}
现在我用我自己的线程工厂将这个 运行nable 提交到 Executors.cachedThreadPool()
,这基本上没有什么新东西。
我可以安全地 return 从那个 运行nable 那样吗?
我查看了 jvisualvm,发现线程池中有一个线程 + 有一些线程正在与服务器逻辑的连接中执行,当我 return 时,我看到这些连接线程是停止,他们确实留在列表中,但他们是白色的......
从 void
方法中使用 return
完全没问题。它只是 returns 来自该方法,在这种情况下将完成线程执行。
您不是在向执行器提交线程,而是在向其提交 Runnable。在 Runnable 中调用 return 不会导致执行它的线程终止。执行器的编写使其可以 运行 Runnables 形式的多个任务,当 Runnable 完成执行时(无论它是否早 returns 或其他)线程继续,并从中获得更多工作它排队提交的任务。
这是 ThreadPoolExecutor#运行Worker 方法中的代码。显示 task.run()
的行是工作线程执行任务的地方,当你的任务 returns 然后工作线程的执行从那里继续进行。
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
w.unlock(); // allow interrupts
boolean completedAbruptly = true;
try {
while (task != null || (task = getTask()) != null) {
w.lock();
// If pool is stopping, ensure thread is interrupted;
// if not, ensure thread is not interrupted. This
// requires a recheck in second case to deal with
// shutdownNow race while clearing interrupt
if ((runStateAtLeast(ctl.get(), STOP) ||
(Thread.interrupted() &&
runStateAtLeast(ctl.get(), STOP))) &&
!wt.isInterrupted())
wt.interrupt();
try {
beforeExecute(wt, task);
Throwable thrown = null;
try {
task.run();
} catch (RuntimeException x) {
thrown = x; throw x;
} catch (Error x) {
thrown = x; throw x;
} catch (Throwable x) {
thrown = x; throw new Error(x);
} finally {
afterExecute(task, thrown);
}
} finally {
task = null;
w.completedTasks++;
w.unlock();
}
}
completedAbruptly = false;
} finally {
processWorkerExit(w, completedAbruptly);
}
}