Java 如果我叫return怎么办;从Runnable开始?

Java 如果我叫return怎么办;从Runnable开始?,java,multithreading,executorservice,Java,Multithreading,Executorservice,我在Java Runnable中实现了run()方法。在这个run方法中有一些到服务器的连接,当它失败时,我对线程执行不再感兴趣,我想退出它。我是这样做的: class MyRunnable implements Runnable { @Override public void run() { // connect to server here if (it failed) { return; }

我在Java Runnable中实现了
run()
方法。在这个run方法中有一些到服务器的连接,当它失败时,我对线程执行不再感兴趣,我想退出它。我是这样做的:

class MyRunnable implements Runnable {
    @Override
    public void run() {
        // connect to server here
        if (it failed) {
            return;
        }

        // do something else
    }
}
现在,我使用自己的线程工厂将此runnable提交到Executors.cachedThreadPool(),它基本上没有什么新功能

我能安全地从那样的地方回来吗


我查看了jvisualvm,我看到线程池中有一个线程+有一些线程在与服务器逻辑的连接中执行,当我返回时,我看到这些连接线程被停止,它们确实保留在列表中,但它们是白色的

void
方法使用
return
是非常好的。它只是从该方法返回,在本例中,该方法将完成线程执行。

void
方法使用
return
是完全正确的。它只是从该方法返回,在本例中,该方法将完成线程执行。

您不是向执行器提交线程,而是向其提交可运行文件。在Runnable中调用return不会导致执行它的线程终止。编写executor是为了在Runnable完成执行(不管它是否提前返回)后,线程继续执行,并从排队提交的任务中获得更多的工作,它可以以Runnable的形式运行多个任务

以下是ThreadPoolExecutor#runWorker方法中的代码。显示
task.run()
的行是工作线程执行任务的位置,当您的任务返回时,工作线程将从那里继续执行

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);
    }
}

您不是向执行器提交线程,而是向其提交可运行程序。在Runnable中调用return不会导致执行它的线程终止。编写executor是为了在Runnable完成执行(不管它是否提前返回)后,线程继续执行,并从排队提交的任务中获得更多的工作,它可以以Runnable的形式运行多个任务

以下是ThreadPoolExecutor#runWorker方法中的代码。显示
task.run()
的行是工作线程执行任务的位置,当您的任务返回时,工作线程将从那里继续执行

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);
    }
}

是的,它只是一个方法调用。返回很好。是的,它只是一个方法调用。回来很好。是的,你是对的。我对此进行了测试,runnable过早地返回了几次,并且在JVM的整个生命周期中,jvisualvm中仍然有一个线程处于活动状态,因此在空闲时可以重用线程。是的,你是对的。我对此进行了测试,runnable过早地返回了几次,并且在JVM的整个生命周期中,jvisualvm中仍然有一个线程处于活动状态,因此线程在空闲时被重用。