把需要并行的任务塞到一个任务队列里面,用一个线程去执行
var:
package com.etc.clear.data.common;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/**
* 变量
* <p>
*
* @ClassName : Variable
* </p>
* <p>
* @Description : TODO
* </p>
* <p>
* @Author : HuaZai
* </p>
* <p>
* @ContactInformation : 1461522031@qq.com/huazai6789@aliyun.com
* </p>
*
* @Date : 2018年1月4日 下午4:22:14
* @Version : V1.0.0
*
*/
public class Variable {
private static BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(10);
private static boolean running = false;
public BlockingQueue<Runnable> getQueue() {
return queue;
}
@SuppressWarnings("static-access")
public void setQueue(BlockingQueue<Runnable> queue) {
this.queue = queue;
}
public boolean isRunning() {
return running;
}
@SuppressWarnings("static-access")
public void setRunning(boolean running) {
this.running = running;
}
}
job:
package com.etc.clear.data.job;
import java.util.concurrent.TimeUnit;
import com.etc.clear.data.common.Variable;
/**
* 定义导出数据任务
* <p>
*
* @ClassName : ExpDataJob
* </p>
* <p>
* @Description : TODO
* </p>
* <p>
* @Author : HuaZai
* </p>
* <p>
* @ContactInformation : 1461522031@qq.com/huazai6789@aliyun.com
* </p>
*
* @Date : 2018年1月4日 下午4:05:10
* @Version : V1.0.0
*
*/
public class ExpDataJob implements Runnable {
Variable var = new Variable();
@Override
public void run() {
while (var.isRunning()) {
try {
//取得一个任务
Runnable job = var.getQueue().poll(5000L, TimeUnit.MILLISECONDS);
System.out.println("x-x-x-x-x-x-x 进入数据《导出》阶段 ....");
if (job != null) {
job.run();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.toString());
}
}
}
}
init:
package com.etc.clear.data.common;
import com.etc.clear.data.utils.OrderdJobUtils;
/**
* 初始化数据清理任务
* <p>
*
* @ClassName : InitDataQuartzCommon
* </p>
* <p>
* @Description : TODO
* </p>
* <p>
* @Author : HuaZai
* </p>
* <p>
* @ContactInformation : 1461522031@qq.com/huazai6789@aliyun.com
* </p>
*
* @Date : 2018年1月4日 下午3:45:09
* @Version : V1.0.0
*
*/
public class InitDataQuartzCommon {
private static int count = 0;
/**
* 初始化数据清理任务
* <p>
*
* @Title : startClearData
* </p>
* <p>
* @Description : TODO
* </p>
* <p>
* @Author : HuaZai
* </p>
*
* @Date : 2018年1月4日 下午3:47:47
*/
public void startClearData() {
// 初始化任务顺序
OrderdJobUtils orderdJobUtils = new OrderdJobUtils();
// 启动任务
orderdJobUtils.start();
for (int i = 0; i < 10; i++) {
orderdJobUtils.insertJob(new Runnable() {
@Override
public void run() {
System.out.println(count++);
}
});
}
synchronized (orderdJobUtils) {
try {
orderdJobUtils.wait(2000L);
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.toString());
}
}
orderdJobUtils.stop();
}
}
utils:
package com.etc.clear.data.utils;
import java.util.concurrent.TimeUnit;
import com.etc.clear.data.common.Variable;
import com.etc.clear.data.job.ExpDataJob;
/**
* 维护任务顺序
* <p>
*
* @ClassName : OrderdJobUtils
* </p>
* <p>
* @Description : TODO
* </p>
* <p>
* @Author : HuaZai
* </p>
* <p>
* @ContactInformation : 1461522031@qq.com/huazai6789@aliyun.com
* </p>
*
* @Date : 2018年1月4日 下午4:49:35
* @Version : V1.0.0
*
*/
public class OrderdJobUtils {
Variable var = new Variable();
/**
* 启动任务
* <p>
*
* @Title : start
* </p>
* <p>
* @Description : TODO
* </p>
* <p>
* @Author : HuaZai
* </p>
*
* @Date : 2018年1月4日 下午4:40:51
*/
public void start() {
var.setRunning(true);
Thread thread = new Thread(new ExpDataJob());
thread.start();
}
/**
* 停止任务
* <p>
*
* @Title : stop
* </p>
* <p>
* @Description : TODO
* </p>
* <p>
* @Author : HuaZai
* </p>
*
* @Date : 2018年1月4日 下午4:41:20
*/
public void stop() {
var.setRunning(false);
}
/**
* 添加任务
* <p>
*
* @Title : insertJob
* </p>
* <p>
* @Description : TODO
* </p>
* <p>
* @Author : HuaZai
* </p>
*
* @Date : 2018年1月4日 下午4:43:08
*/
public void insertJob(Runnable job) {
try {
//插入 一个任务
if (var.getQueue().offer(job, 5000L, TimeUnit.MILLISECONDS) == false) {
// 处理任务插入失败的情况
System.out.println("E-E-E-E-E-E-E: 任务插入失败 !~ ");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.toString());
}
}
}
main:
package com.etc.clear;
import com.etc.clear.data.common.InitDataQuartzCommon;
/**
* 程序启动入口/调用任务的类
* <p>
*
* @ClassName : ClsMain
* </p>
* <p>
* @Description : TODO
* </p>
* <p>
* @Author : HuaZai
* </p>
* <p>
* @ContactInformation : 1461522031@qq.com/huazai6789@aliyun.com
* </p>
*
* @Date : 2018年1月3日 下午1:21:56
*
* @Version : V1.0.0
*
*/
public class ApplicationMain {
public static void main(String[] args) {
// 初始化清理数据任务
InitDataQuartzCommon dataQuartzCommon = new InitDataQuartzCommon();
// 启动数据清理任务
dataQuartzCommon.startClearData();
}
}
原网址: 访问
创建于: 2021-01-28 19:04:48
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论