spring provides support thread pool
View basic
Java thread pool
First, a thread ThreadTransCode.java
package com.enorth.lichen.transcode;
public class ThreadTransCode implements Runnable{
@Override
public void run() {
System.out.println( "转码开始..............");
}
}
public class ThreadTransCode implements Runnable{
@Override
public void run() {
System.out.println( "转码开始..............");
}
}
Add thread pool configuration information in the spring configuration file
< 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 >
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 >
Add injected in 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 >
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 >
Call in the action
private TaskExecutor taskExecutor;
public TaskExecutor getTaskExecutor() {
return taskExecutor;
}
public void setTaskExecutor(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
public String execute() throws Exception {
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;
}
Above can be achieved by using a spring java thread pool ...
Unfortunately, when I use threads in the thread pool to perform transcoding flv, and found that only stopped tomcat can perform automated transcoding operations, other simple multi-threaded operation are possible. The reason is unknown, thread too do transcoding consumption of resources?
May day. Solved the problem. The reason is that
a deadlock ... the actual thread transcoding stopped after tomcat will automatically run already explained, I did not even pay attention to day ...
Thread Pool 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);
}
public static synchronized void trans(String videoPath,String targetPath){
ThreadTransCode trans= new ThreadTransCode(videoPath,targetPath);
exec.execute(trans);
}
This article comes from " WISPER technology to make the art of " blog, be sure to keep this source http://lichen.blog.51cto.com/697816/162057
Reproduced in: https: //my.oschina.net/lichen/blog/264890