定时任务多线程(结合spring测试)

来源:互联网 发布:mac转码软件 编辑:程序博客网 时间:2024/06/03 21:32
package com.xiuye.bean;public class TestForUpdate {private String uuid;private String content;private int state;public int getState() {return state;}public void setState(int state) {this.state = state;}public String getUuid() {return uuid;}public void setUuid(String uuid) {this.uuid = uuid;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}@Overridepublic String toString() {return "TestForUpdate [uuid=" + uuid + ", content=" + content+ ", state=" + state + "]";}}

package com.xiuye.mapper;import java.util.List;import com.xiuye.bean.TestForUpdate;public interface TestForUpdateMapper {void add(TestForUpdate forUpdate);List<TestForUpdate> queryListForUpdate(int rowNum);void update(TestForUpdate forUpdate);}

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><mappers><mapper resource="mapper/TestForUpdate.xml" /></mappers></configuration>


<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace="com.xiuye.mapper.TestForUpdateMapper"><resultMap id="BaseResultMap" type="com.xiuye.bean.TestForUpdate"><id column="UUID" property="uuid" jdbcType="VARCHAR" /><result column="CONTENT" property="content" jdbcType="VARCHAR" /><result column="STATE" property="state" jdbcType="INTEGER" /></resultMap><sql id="columns">UUID,CONTENT,STATE</sql><insert id="add" parameterType="com.tienon.bean.TestForUpdate">insert into testforupdate(<include refid="columns"></include>)values(#{uuid},#{content},#{state})</insert><select id="queryListForUpdate" parameterType="int" resultMap="BaseResultMap">select<include refid="columns"></include>from testforupdatewhere state = 1 and rownum < #{0}for update</select><update id="update" parameterType="com.tienon.bean.TestForUpdate">update testforupdate set content=#{content},state=#{state}where uuid=#{uuid}</update></mapper>

package com.xiuye.task;import java.util.List;import java.util.Random;import javax.annotation.Resource;import org.springframework.stereotype.Component;import org.springframework.transaction.annotation.Transactional;import com.xiuye.bean.TestForUpdate;import com.xiuye.mapper.TestForUpdateMapper;import com.xiuye.util.TestForUpdateUtil;@Componentpublic class TestForUpdateTask implements Cloneable {@Resourceprivate TestForUpdateMapper testForUpdateDao;@Transactionalpublic List<TestForUpdate> handling() {List<TestForUpdate> forUpdates = this.testForUpdateDao.queryListForUpdate(TestForUpdateUtil.getNumOfData());if (forUpdates == null || forUpdates.isEmpty()) {return forUpdates;}for (TestForUpdate forUpdate : forUpdates) {forUpdate.setContent(new Random().nextInt(100000) + "--改");forUpdate.setState(2);this.testForUpdateDao.update(forUpdate);}return forUpdates;}@Overridepublic TestForUpdateTask clone() {try {return (TestForUpdateTask) super.clone();} catch (CloneNotSupportedException e) {e.printStackTrace();}return null;}}

package com.xiuye.thread;import java.util.List;import org.apache.log4j.Logger;import com.xiuye.bean.TestForUpdate;import com.xiuye.task.TestForUpdateTask;public class TestForUpdateThread implements Runnable, Cloneable {private Logger log = Logger.getLogger(TestForUpdateThread.class);private TestForUpdateTask task;public TestForUpdateThread(TestForUpdateTask task) {this.task = task;}@Overridepublic void run() {System.out.println("=======================================");System.out.println("Current Thread := " + Thread.currentThread() + " Start!");System.out.println("=======================================");List<TestForUpdate> forUpdates = task.handling();for (TestForUpdate forUpdate : forUpdates) {System.out.println("Current Thread := " + Thread.currentThread()+ "  完成查询更新数据 := " + forUpdate);}System.out.println("=======================================");System.out.println("Current Thread := " + Thread.currentThread() + " Over!");System.out.println("=======================================");}@Overrideprotected TestForUpdateThread clone() {try {TestForUpdateThread thread = (TestForUpdateThread) super.clone();thread.task = task.clone();return thread;} catch (CloneNotSupportedException e) {e.printStackTrace();}return null;}public TestForUpdateThread getSelfCopy(){return this.clone();}}

package com.xiuye.thread.pool;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;import com.xiuye.thread.TestForUpdateThread;public class TestForUpdateThreadPool {private static int threadsNumber = 100;private static ScheduledExecutorService threadsPool;static {threadsPool = Executors.newScheduledThreadPool(threadsNumber);}public static void executeTestForUpdateThreads(TestForUpdateThread runnable) {System.out.println("================================");System.out.println("runnable address := " + runnable);System.out.println("================================");threadsPool.scheduleAtFixedRate(runnable, 1, 1, TimeUnit.SECONDS);TestForUpdateThread forUpdateThread = (TestForUpdateThread) runnable;for (int i = 1; i < threadsNumber; i++) {runnable = forUpdateThread.getSelfCopy();System.out.println("================================");System.out.println("runnable address := " + runnable);System.out.println("================================");threadsPool.scheduleAtFixedRate(runnable, 100, 1, TimeUnit.MICROSECONDS);//threadsPool.execute(runnable);}}public static void shutdownTestForUpdateThreads(){threadsPool.shutdown();}}
package com.xiuye.service;import javax.annotation.Resource;import org.springframework.stereotype.Service;import com.xiuye.mapper.TestForUpdateMapper;import com.xiuye.task.TestForUpdateTask;import com.xiuye.thread.TestForUpdateThread;import com.xiuye.thread.pool.TestForUpdateThreadPool;@Servicepublic class TestForUpdateService {@Resourceprivate TestForUpdateMapper testForUpdateDao;@Resourceprivate TestForUpdateTask task;public void start(){TestForUpdateThread thread = new TestForUpdateThread(task);TestForUpdateThreadPool.executeTestForUpdateThreads(thread);}public void end(){TestForUpdateThreadPool.shutdownTestForUpdateThreads();}}


package com.xiuye.util;import java.util.HashMap;public class TestForUpdateUtil {private static HashMap<String, Object> params = new HashMap<String, Object>();public static int getNumOfData(){Integer num = (Integer) params.get("testForUpdateNumber");if(num == null){try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}params.put("testForUpdateNumber", 50);num = 50;}return num;}}





0 0