Spring configuration thread pool, asynchronous processing

1. First configure the thread pool in the spring configuration file

    <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">

        <property name="corePoolSize" value="5" />

        <property name="maxPoolSize" value="10" />

        <property name="queueCapacity" value="25" />

    </bean>

<bean name="dataSyncService" class="com.aaa.impl.DataSyncServiceImpl">

        <property name="taskExecutor" ref="taskExecutor"/>

    </bean>

 

2. Define the implementation class that uses the thread pool

public class DataSyncServiceImpl implements DataSyncService{

 

    private class DataReplacer implements Runnable{

private SomeObject obj;

        

        public DataReplacer(SomeObject obj){

            this.loginResult = loginResult;

        }

        

        @Override

        public void run(){

//EVERYTHING

        }

    }

       

    @Override

    public void syncData(SomeObject obj){

        taskExecutor.execute(new DataReplacer(obj));

    }

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327084321&siteId=291194637