【问题标题】:How to put this duplicated code into a method using parameters?如何将此重复的代码放入使用参数的方法中?
【发布时间】:2022-11-05 00:41:49
【问题描述】:

我对 Java 编程相当陌生,并且被提示进行一些重构,其中该代码的逻辑是相同的,只需要放入一个可以调用的方法中,并将服务作为参数传递。该功能基本上是关闭线程,我们这里有两个线程池,只需要知道如何将这些服务作为参数传递,它们也是接口的一部分。这是我试图重构为可以调用的方法的代码。

if (scheduledExecutorService1 != null) {
            scheduledExecutorService1.shutdown(); // Disable new tasks from being submitted
            try {
                // Wait a while for existing tasks to terminate
                if (!scheduledExecutorService1.awaitTermination(60, TimeUnit.SECONDS)) {
                    scheduledExecutorService1.shutdownNow(); // Cancel currently executing tasks
                    // Wait a while for tasks to respond to being cancelled
                    if (!scheduledExecutorService1.awaitTermination(60, TimeUnit.SECONDS))
                        System.err.println("Pool did not terminate");
                }
            } catch (InterruptedException ie) {
                // (Re-)Cancel if current thread also interrupted
                scheduledExecutorService1.shutdownNow();
                // Preserve interrupt status
                Thread.currentThread().interrupt();
            }
        }
        if (scheduledExecutorService2 != null) {
            scheduledExecutorService2.shutdown(); // Disable new tasks from being submitted
            try {
                // Wait a while for existing tasks to terminate
                if (!scheduledExecutorService2.awaitTermination(60, TimeUnit.SECONDS)) {
                    scheduledExecutorService2.shutdownNow(); // Cancel currently executing tasks
                    // Wait a while for tasks to respond to being cancelled
                    if (!scheduledExecutorService2.awaitTermination(60, TimeUnit.SECONDS))
                        System.err.println("Pool did not terminate");
                }
            } catch (InterruptedException ie) {
                // (Re-)Cancel if current thread also interrupted
                scheduledExecutorService2.shutdownNow();
                // Preserve interrupt status
                Thread.currentThread().interrupt();
            }
        }

【问题讨论】:

  • 最好在Code Review 询问有关优化运行代码的问题

标签: java methods


【解决方案1】:

只需将代码放入方法中:

private void xx(type_of_scheduledExecutorService scheduledExecutorService ) {
    if (scheduledExecutorService != null) {
        scheduledExecutorService.shutdown(); // Disable new tasks from being submitted
        try {
            // Wait a while for existing tasks to terminate
            if (!scheduledExecutorService.awaitTermination(60, TimeUnit.SECONDS)) {
                scheduledExecutorService.shutdownNow(); // Cancel currently executing tasks
                // Wait a while for tasks to respond to being cancelled
                if (!scheduledExecutorService.awaitTermination(60, TimeUnit.SECONDS))
                    System.err.println("Pool did not terminate");
            }
        } catch (InterruptedException ie) {
            // (Re-)Cancel if current thread also interrupted
            scheduledExecutorService.shutdownNow();
            // Preserve interrupt status
            Thread.currentThread().interrupt();
        }
    }
}

并调用它两次:

xx(scheduledExecutorService1);
xx(scheduledExecutorService2);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-05
    • 2016-08-27
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    • 2016-06-05
    相关资源
    最近更新 更多