【问题标题】:How to prevent main thread execution the next line untill the last line fully executed如何防止主线程执行下一行,直到最后一行完全执行
【发布时间】:2020-03-04 12:10:19
【问题描述】:

首先,我是 Spring-boot 的新手。我想抓取一个新闻网站以制作一个休息 api。 我所做的只是创建一条路线,负责在抓取新闻网站后为最新新闻创建 json 响应。 这是路线

@GetMapping("/latest")
    public ArrayList<Result> scrape() {
        
        String title = "";
        Document doc;
        Element body ;
        ArrayList<Element> elements = new ArrayList<>() ;
        
        ArrayList<Result> results = new ArrayList<>();
        
        ArrayList<Element>li;
        
        try {
            
            doc = Jsoup.connect("https://timesofindia.indiatimes.com").get(); //error take place here
            
            title = doc.title();
            
            body = doc.body();
            
            elements = body.getElementsByAttributeValue("data-vr-zone","latest");
            System.out.println("Size "+elements.size());
        
            li = elements.get(0).select("li > a");
        
            System.out.println("WHole Data "+li.toString());
        
            System.out.println(li.size());
        
        int id = 0;
        
        for(Element text : li ) {
            
            id++;
                        
            String link  = "";
            
            if( text.attr("href") != ""  &&  text.attr("title") != "" ) {
                link = "https://timesofindia.indiatimes.com/" + text.attr("href") ;
                results.add(new Result(id,text.attr("title"),link));
            }       
        }

        } catch (IOException e) {
            
            e.printStackTrace();    
        }
        
        return results;
    }

请有人帮助如何在 java 中处理这种情况。

因为Jsoup.connect().get()发出了http请求,所以主线程把它放在一个唯一的线程中并行执行,输出ArrayList = 0的大小。

[这里是输出][1] [1]:https://i.stack.imgur.com/HubJD.png

堆栈跟踪:

2020-03-04T12:18:34.410009+00:00 heroku[路由器]: at=info method=GET path="/latest" host=morning-waters-01018.herokuapp.com request_id=15a8fdba-e541- 4aa2-a0df-34838b2e7e5f fwd="47.30.171.180" dyno=web.1 连接=0ms 服务=177ms 状态=500 字节=473 协议=https 2020-03-04T12:18:34.399950+00:00 应用 [web.1]:大小 0 2020-03-04T12:18:34.402793+00:00 app[web.1]: 2020-03-04 12:18:34.402 错误 4 --- [io-17255-exec-5] o.a.c.c.C.[.[.[ /].[dispatcherServlet] : Servlet.service() 用于路径 [] 上下文中的 servlet [dispatcherServlet] 引发异常 [请求处理失败;嵌套异常是 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0] 根本原因 2020-03-04T12:18:34.402794+00:00 应用[web.1]: 2020-03-04T12:18:34.402795+00:00 应用程序 [web.1]:java.lang.IndexOutOfBoundsException:索引:0,大小:0 2020-03-04T12:18:34.402796+00:00 app[web.1]: at java.util.ArrayList.rangeCheck(ArrayList.java:657) ~[na:1.8.0_242-heroku] 2020-03-04T12:18:34.402797+00:00 app[web.1]: at java.util.ArrayList.get(ArrayList.java:433) ~[na:1.8.0_242-heroku]

【问题讨论】:

  • 你能发布堆栈跟踪吗?
  • stackTrace 显示 IndexOutOfBoundsException

标签: java multithreading spring-boot


【解决方案1】:

我认为Jsoup.connect("https://timesofindia.indiatimes.com").get() 进行了同步调用。

你只需要找到错误的原因。

最简单的方法

请将catch (IOException e) 更改为catch (Exception e)

main()方法添加到同一个源文件中,将scrape()的主体复制到main()中并在IDE中运行main()方法。您可以只打印results(不返回它们)。

您可以尝试在main()方法中调试代码。

原因,貌似

elements = body.getElementsByAttributeValue("data-vr-zone","latest")

elements 此处为空

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多