多线程(或异步)对Spring框架计算 [英] Multithreaded (or async) calculation on Spring Framework

查看:80
本文介绍了多线程(或异步)对Spring框架计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习Spring框架,它是pretty真棒。

I am learning Spring Framework, and it is pretty awesome.

我想用JAVA多线程,但我不知道该如何与Spring框架。

I want to use JAVA multithreading, but I don't know how with the Spring Framework.

下面是code服务:

//StudentService.java
public List<Grade> loadGradesForAllStudents(Date date) {
    try{
        List<Grade> grades = new ArrayList<Grade>();
        List<Student> students = loadCurrentStudents(); // LOAD FROM THE DB

        for(Student student : students) { // I WANT TO USE MULTITHREAD FOR THIS PART

            // LOAD FROM DB (MANY JOINS)
            History studentHistory = loadStudentHistory(student.getStudentId(), date); 

            // CALCULATION PART
            Grade calculatedGrade = calcStudentGrade(studentHistory, date);

            grades.add(calculatedGrade);
        }
        return grades;
    } catch(Exception e) {
        ...
        return null;
    }
}

和无多线程,它是pretty缓慢。

And without multithreading, it is pretty slow.

我想for循环会导致缓慢,但我不知道如何处理这个问题。如果给我一个有用的链接或例如code,我倒是AP preciate它。

I guess the for loop causes the slowness, but I don't know how to approach this problem. If give me an useful link or example code, I'd appreciate it.

我想出的方法是loadStudentHistory pretty慢(约300毫秒)比较calcStudentGrade(约30毫秒)。

I figured out the method loadStudentHistory is pretty slow (around 300ms) compare to calcStudentGrade (around 30ms).

推荐答案

使用多线程这在并发用户的应用程序是一个坏主意,因为而不是每个请求使用一个线程,一个连接现在每个查询使用多个线程和多个连接。它不会缩放随着用户数量的增长。

Using multithreading for this a bad idea in an application with concurrent users, because instead of having each request use one thread and one connection now each query uses multiple threads and multiple connections. It doesn't scale as the number of users grows.

当我看着你的榜样,我看到了两个可能的问题:

When I look at your example I see two possible issues:

1)你有应用程序和数据库,其中每个这些旅行的需要时间之间有太多往返。

1) You have too many round trips between the application and the database, where each of those trips takes time.

2)如果每个查询使用单独的事务(你不说,那里的交易是在例如code划分)并不清楚,如果你的查询都是各自建立自己的交易,可能是浪费的,因为每个事务的开销与它相关

2) It's not clear if each query is using a separate transaction (you don't say where the transactions are demarcated in the example code), if your queries are each creating their own transaction that could be wasteful, because each transaction has overhead associated with it.

使用多线程不会做很多工作来帮助与#1(如果它确实有助于它将把更多的负载在数据库上),并将不得不对#2没有影响,或使其恶化(取决于当前事务范围;如果你收到了在同一个事务的查询,使用多线程,他们将不得不在不同的交易)。正如已经说明它不会扩大。

Using multithreading will not do much to help with #1 (and if it does help it will put more load on the database) and will either have no effect on #2 or make it worse (depending on the current transaction scopes; if you had the queries in the same transaction before, using multiple threads they'll have to be in different transactions). And as already explained it won't scale up.

我的建议:

1)使服务事务性的,如果是没有准备好,以便它做的一切是一个事务内。删除异常醒目/空回来的东西,并引入一个异常处理程序,以便从控制器抛出任何事情都会被捕获并记录(与Spring是如何想使用异常回滚事务干扰)。这将创建从交易减少你的开销,让你的异常处理干净。

1) Make the service transactional, if it is not already, so that everything it does is within one transaction. Remove the exception-catching/null-returning stuff (which interferes with how Spring wants to use exceptions to rollback transactions) and introduce an exception-handler so that anything thrown from controllers will be caught and logged. That will minimize your overhead from creating transactions and make your exception-handling cleaner.

2)创建一个查询的带回你的学生名单。查询被发送到数据库一次的方式,然后把结果结果在组块(根据对结果集的提取大小)读回。您可以自定义查询以获取你需要让你不用有过多的加入只是什么。在查询运行解释-计划,并确保它使用索引。您将有一个更快的查询和往返的数量少得多,这会带来很大的速度提升。

2) Create one query that brings back a list of your students. That way the query is sent to the database once, then the resultset results are read back in chunks (according to the fetch size on the resultset). You can customize the query to get back only what you need so you don't have an excessive number of joins. Run explain-plan on the query and make sure it uses indexes. You will have a faster query and a much smaller number of round trips, which will make a big speed improvement.

这篇关于多线程(或异步)对Spring框架计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆