简体   繁体   中英

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). 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.

<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? Thank you.

JSP creates dynamic html. So you are essentially placing the result of your business logic from javaBean to your html table. 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. 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. These can make concurrent calls back to the server to load the rest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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