在使用线程运行时,不同时间得到不同的结果

4
@Path("/getVersion")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public List getVersion(String getVersionJson) throws InterruptedException {
        List outputList = new ArrayList();
        try {
            JSONArray jsonArr = new JSONArray(getVersionJson);

            for (int j = 0; j < jsonArr.length(); j++) {
                JSONObject jsonObj = jsonArr.getJSONObject(j);
                String ip = jsonObj.getString("ipaddress");
                String username = jsonObj.getString("username");
                String password = jsonObj.getString("password");

                new Thread() {
                    public void run() {
                        //outputList.add(ip);
                        final String connectionStatus = getSSHConnection(ip, username, password);
                        // outputList.add(connectionStatus);
                        if (connectionStatus.equals("Connected")) {
                            // outputList.add(connectionStatus);
                            //Version Check
                            expect.send("bwshowver" + "\n");
                            if (expect.expect("$") > -1) {
                                String contt = "";
                                contt = (expect.before);
                                if (contt != null && contt != "") {

                                    contt = contt.replaceAll("\n+", "\n");
                                    contt = contt.replaceAll(" +", " ");

                                    String splitter[] = contt.split("\n");

                                    for (int i = 0; i < splitter.length; i++) {
                                        if (splitter[i].contains("Patches")) {
                                            final String patches = splitter[i];
                                        }
                                        //version
                                        if (splitter[i].contains("version")) {
                                            // final String version = splitter[i];
                                            outputList.add(splitter[i]);

                                        }

                                    }

                                } else {
                                    final String v1 = "Error in version check";
                                    System.out.println("Error in version check");
                                }
                            }
                        } else {
                            outputList.add(connectionStatus);
                        }
                    }
                }.start();
            }
        } catch (Exception e) {
            final String v = "Error";
            //     logger.error("Exception in getVersion Function-ServService Class: " + e.getMessage());
        } finally {
            stopSSH();
        }
        Thread.sleep(20000);
        //outputList.add("ffk");
        // outputList.add("f");
        return outputList;
    }

我已经创建了一个使用线程的方法,在这个方法中,我发送了一个包含JSON对象的JSON数组。之前我创建了这个方法时没有使用线程,它可以正常工作。但是现在在使用线程后,我会在不同的时间得到不同的结果。你能告诉我哪里出了问题吗?


1
不要使用Thread.sleep,而是使用CountDownLatch来等待所有结果的返回。 - muzzlator
此外,确保你的输出列表是线程安全的(http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#synchronizedList-java.util.List-)。 - muzzlator
他确实有多个线程并行地填充数组吗? - muzzlator
2个回答

2

以上代码可以改进的三个主要方面:

  • 在多个线程修改时,使用线程安全的集合。如果集合没有明确说明它是线程安全的,则需要手动加锁或切换到线程安全的集合。
  • 使用 Thread.sleep() 进行同步通常不是一个好主意。您应该使用更精确的同步方法来避免不稳定的行为。我选择在这里使用 CountDownLatch,您也可以使用其他同步工具。
  • 在线程内抛出的异常不会向上传递到主线程,而可能只在该线程内部冒泡并且永远不会被注意到。您希望能够注意到运行中抛出的异常并在最后处理它。我还将修改代码以显示如何处理异常的大致指南。

以下是简洁格式的答案,您可以根据需要对代码进行调整:

public List getVersion(String getVersionJson)
        throws InterruptedException, IOException {
    // You must use a thread-safe list.
    final List outputList = Collections.synchronizedList(new ArrayList());
    // This reference is used to keep track of exceptions thrown
    // in the runnable.
    final AtomicReference<Exception> exceptionReference =
        new AtomicReference(null);
    // This latch acts as a synchronization barrier so that all threads
    // must have put data into outputList before the code will go
    // past the latch.await() call
    final CountDownLatch latch = new CountDownLatch(jsonArr.length());
    for (int j = 0; j < jsonArr.length(); j++) {
        new Thread() {
            public void run() {
                try {   
                     // Do network I/O stuff
                     outputList.add(resultFromNetworkStuff);
                     latch.countDown();
                } catch (IOException e) {
                     // Set the exception for later retrieval
                     exceptionReference.compareAndSet(null, e);
                }
            }                        
        }.start();
    }

    // Wait for all the threads to finish before continuing.
    try {
        latch.await();
    } finally {
        // Even if we're interrupted, we want to close the connection.
        closeSSH();
    }
    if (exceptionRef.get() != null) {
        // Throw any exceptions we see in our threads.
        throw exceptionRef.get();
    }
    return outputList;
}

1
请考虑编辑您的答案,解释出错的原因以及如何修复它,而不仅仅是倾倒一个新的代码块。 - Gray
1
我试图在代码注释中完成这个任务,大部分“what”都在问题下面的注释中提到了,但我会在答案中添加额外的信息,并更详细地讨论错误处理。 - muzzlator

1
你将主线程休眠了一段硬编码的时间,而在此期间,多个其他线程并行地将数据推入共享列表中。这从定义上讲是行不通的。
如果你的其他线程在20秒内完成,那么一切都好。但如果没有完成,你的方法将返回其他线程到目前为止收集到的任何内容。更糟糕的是,那个其他线程会继续更新你已经返回的列表。
你必须确保内部线程:
  • 不要以无控制的方式(相互覆盖!)访问结果列表

  • 在从该方法返回之前全部完成。

不要因为听说过某个概念就在代码中应用它,而不理解自己在做什么。你对如何使用Java线程以及使用它们意味着什么并不了解。因此,学习这些内容,而不是像这样盲目将其引入生产代码中。
然后:你的catch块正在丢弃错误信息!这总是一个坏主意。同样适用于将错误信息作为字符串推送到结果列表中的方法。
除此之外:阅读Robert Martin的《Clean Code》。你的代码会比你无缘无故添加线程受益更多。

是我给它点了踩,我认为你基于很少的证据对他的技能进行了过于苛刻的批评。另外,他确实有“多个”线程,因此他将获得 I/O 并行处理而不是串行处理的好处。总的来说,代码还有很大的改进空间,但我们不应该太苛求,并尝试通过切换到线程来解决他遇到的问题的核心。而学习的更好方法何在?除了尝试一些东西之外还有什么呢? - muzzlator
感谢您的回答,我已经重新修改了部分内容。实际上我错过了循环部分,但我仍然认为他的操作顺序有误。就像你不会在学习骑独轮车时学习杂耍一样。也就是说,你应该从好的教程开始学习,当你理解了所学内容后再将其应用到生产代码中。现在他正在同时进行3到5个复杂的操作,这是非常低效的方法。感谢您的解释,我会点赞支持您的回答 :-) - GhostCat
感谢您的帮助。我完全是新手,所以我正在尝试所有可能的方法来解决问题。@muzzlator @GhostCat - riza

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接