spring提供了线程池的支持
查看基本
先是一个线程 ThreadTransCode.java
package com.enorth.lichen.transcode; public class ThreadTransCode implements Runnable{ @Override public void run() { System.out.println( "转码开始.............."); } }
在spring配置文件中添加线程池的配置信息
< bean id ="taskExecutor" class ="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" > < property name ="corePoolSize" value ="3" /> < property name ="keepAliveSeconds" value ="200" /> < property name ="maxPoolSize" value ="5" /> < property name ="queueCapacity" value ="25" /> </ bean >
在action中添加注入
< bean id ="saveVideoAction" class ="com.enorth.lichen.action.video.SaveVideoAction" scope ="prototype" > < property name ="videoService" > < ref bean ="videoService" /> </ property > < property name ="groupService" > < ref bean ="groupService" /> </ property > <property name="taskExecutor"> <ref bean="taskExecutor" /> </property> </ bean >
在action中调用
private TaskExecutor taskExecutor; public TaskExecutor getTaskExecutor() { return taskExecutor; } public void setTaskExecutor(TaskExecutor taskExecutor) { this.taskExecutor = taskExecutor; } public String execute() throws Exception {
for( int k = 0; k < 2; k++) { taskExecutor.execute( new ThreadTransCode()); } return SUCCESS; }
以上就可以用spring实现java线程池的使用...
遗憾的是, 我在用线程池执行flv的转码线程时, 发现只有停掉tomcat才能自动执行转码的操作, 其他简单操作的多线程都是可以的. 原因不明, 难道转码的线程太耗资源?
日日日.问题解决了.原因是 死锁...实际转码的线程在tomcat停掉后就能自动运行已经说明了,我日竟然没注意...
线程池 ThreadPool.java
public static ExecutorService exec = Executors.newFixedThreadPool(1); public static synchronized void trans(String videoPath,String targetPath){ ThreadTransCode trans= new ThreadTransCode(videoPath,targetPath); exec.execute(trans); }
本文出自 “” 博客,请务必保留此出处