springmvc+spring thread pool processing HTTP concurrency Request data synchronization control problem

Source: Internet
Author: User

Spring configuration file Add thread pool configuration

<bean id= "Taskexecutor"  
        class= "Org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" >  
        <!--Core threads--  
        <property name= "Corepoolsize" value= "3"/>  
        <!--max Threads--  
        < Property Name= "Maxpoolsize" value= "ten"/>  
        <!--queue Maximum length >=mainexecutor.maxsize--  
        <property Name= "queuecapacity" value= "/>  
        <!--thread pool to maintain idle time allowed by thread--  
        <property name=" Keepaliveseconds " Value= the/>  
        <!--thread Pool handles the policy of rejecting a task (a wireless thread is available) Threadpoolexecutor.callerrunspolicy policy, the caller's thread executes the task, and if the actuator is closed, is discarded.  --  
        <property name= "Rejectedexecutionhandler" >  
            <bean class= " Java.util.concurrent.threadpoolexecutor$callerrunspolicy "/>  
        </property>  
    </bean>  

Controller class

Package Com.test.controller;  
Import Java.util.HashMap;  
Import Java.util.Map;  

Import Java.util.UUID;  

Import Javax.servlet.http.HttpServletRequest;  
Import org.springframework.beans.factory.annotation.Autowired;  
Import Org.springframework.core.task.TaskExecutor;  
Import Org.springframework.stereotype.Controller;  
Import Org.springframework.web.bind.annotation.ModelAttribute;  
Import org.springframework.web.bind.annotation.RequestMapping;  

Import Org.springframework.web.bind.annotation.ResponseBody;  
Import Com.test.entity.Order;  
Import Com.test.entity.Test;  

Import Com.test.service.TestService; @Controller @RequestMapping ("/test") public class TestController {@Autowired private taskexecutor execut  

    Or  

    @Autowired Testservice Testservice;  

        @ResponseBody @RequestMapping ("/order") public map<string, object> test (HttpServletRequest request) {  
      Thread pool + sync block Executor.execute (new Runnable () {      @Override public void Run () {synchronized (testservice) {tes  
                Tservice.insert (order);  
        }  
            }  
        });  
    return null;   }  
}

Database has a field num value of: 382
HTTP every request, NUM will-1
Simulate concurrent 500 requests
The result is as shown:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.