spring+struts 配置和管理线程池

<!-- 定义线程池 -->
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="5" />
    <property name="maxPoolSize" value="10" />
    <property name="queueCapacity" value="25" />
  <property name="corePoolSize" value="5" />
  <property name="queueCapacity" value="25" /> <property name="threadNamePrefix" value="my-thread-" />
  <property name="rejectedExecutionHandler"> <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" /> </property>
 </bean>

 

web.xml

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    <init-param>
        <param-name>actionPackages</param-name>
        <param-value>com.yourcompany.struts.actions</param-value>
    </init-param>
    <init-param>
        <param-name>config</param-name>
        <param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>
    </init-param>
</filter>

使用样例

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.apache.struts2.convention.annotation.Action;
import com.opensymphony.xwork2.ActionSupport;

public class YourAction extends ActionSupport {

    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @Action("yourAction")
    public String yourActionMethod() {
        // 使用线程池执行任务
        taskExecutor.execute(() -> {
            // 你的任务代码
        });

        return SUCCESS;
    }
}

 线程阻塞运行方法

import java.util.concurrent.ExecutorService;  
import java.util.concurrent.Executors;  
import java.util.concurrent.Future;  
  
public class TwoThreadsWithReturnValue {  
  
    public static void main(String[] args) throws Exception {  
        // 创建一个固定大小的线程池  
        ExecutorService executorService = Executors.newFixedThreadPool(2);  
  
        // 提交任务并获取Future对象  
        Future<String> future1 = executorService.submit(TwoThreadsWithReturnValue::firstMethod);  
        Future<String> future2 = executorService.submit(TwoThreadsWithReturnValue::secondMethod);  
  
        // 获取并打印两个方法的返回值  
        System.out.println("First method returned: " + future1.get()); // 这里会阻塞,直到firstMethod执行完成  
        System.out.println("Second method returned: " + future2.get()); // 这里会阻塞,直到secondMethod执行完成  
  
        // 关闭线程池(通常在实际应用中,你会在合适的时机关闭线程池,而不是立即关闭)  
        executorService.shutdown();  
  
        // 主线程继续执行其他任务  
        System.out.println("Both methods have completed, main thread continues.");  
    }  
  
    public static String firstMethod() {  
        try {  
            // 模拟长时间运行的任务  
            Thread.sleep(1000);  
        } catch (InterruptedException e) {  
            Thread.currentThread().interrupt(); // 重新设置中断状态  
            return "First method interrupted";  
        }  
        return "First method result";  
    }  
  
    public static String secondMethod() {  
        try {  
            // 模拟长时间运行的任务  
            Thread.sleep(1500);  
        } catch (InterruptedException e) {  
            Thread.currentThread().interrupt(); // 重新设置中断状态  
            return "Second method interrupted";  
        }  
        return "Second method result";  
    }  
}

 

posted @ 2024-03-26 22:03  显示昵称嗷嗷喊  阅读(18)  评论(0编辑  收藏  举报