简体   繁体   English

在JSP中并行化调用的最佳方法是什么?

[英]What's the best way to parallelize calls in JSP?

My understanding of JSP is that each line in the java code is run step by step (in sequence). 我对JSP的理解是java代码中的每一行都是逐步运行的(按顺序)。 Eg if I have a code below, doSomething("apple") will be executed first until it returns a value, then doSomething("orange") will be executed next until it returns a value, then finally doSomething("pear") will be executed until it returns a value and the whole page is displayed. 例如,如果我下面有代码,则首先执行doSomething("apple")直到返回值,然后再执行doSomething("orange")直到返回值,然后最终执行doSomething("pear")执行直到它返回一个值并显示整个页面。

<table border="1">
    <thead>
        <tr>
            <th>Test</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Column A</td>
            <td><%=javaBean.doSomething("apple")%></td>
        </tr>
        <tr>
            <td>Column B</td>
            <td><%=javaBean.doSomething("orange")%></td>
        </tr>
        <tr>
            <td>Column C</td>
            <td><%=javaBean.doSomething("pear")%></td>
        </tr>
    </tbody>
</table>

What is the best way to make these calls parallel eg run doSomething("apple") & doSomething("orange") & doSomething("pear") concurrently? 使这些调用并行的最佳方法是什么,例如同时运行doSomething("apple") & doSomething("orange") & doSomething("pear") Thank you. 谢谢。

JSP creates dynamic html. JSP创建动态html。 So you are essentially placing the result of your business logic from javaBean to your html table. 因此,您实际上是将业务逻辑的结果从javaBean放置到html表中。 As it is you can not make it concurrent as you need the result of each method to be placed in the row. 因为它是不能并发的,因为您需要将每种方法的结果放入行中。
You should restructure your code so as to calculate everything you need (perhaps using concurrency) and then retrieve the results to place them in the row. 您应该重新构建代码,以便计算所需的一切(可能使用并发),然后检索结果以将它们放在行中。

正如我理解你的问题,你需要为你的方法创建三个任务(三个线程),它将独立运行,而不管它们的顺序是否完成。

You shouldn't do this in JSP, it's designed to render in a single thread. 您不应该在JSP中执行此操作,它旨在在单个线程中呈现。 If the page is too slow, the usual method these days is to have a fast loading page with three placeholders. 如果页面太慢,则目前的常用方法是使用三个占位符快速加载页面。 Then load the slow parts with AJAX. 然后用AJAX加载慢速部件。 These can make concurrent calls back to the server to load the rest. 这些可以并发调用回服务器以加载其余部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM