- package com.uniot.ssms.sync.presenter.changlv;
- import java.sql.SQLException;
- import java.text.ParseException;
- import java.util.Date;
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.ScheduledFuture;
- import java.util.concurrent.ScheduledThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
- import com.uniot.ssms.sync.utils.RestClientConfig;
- public class StartSynchroniz {
- public static void main(String[] args) throws InterruptedException,
- ExecutionException {
- ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(
- 3); // 使用给定核心池大小创建一个新 ScheduledThreadPoolExecutor
- ScheduledFuture future1 = executor.scheduleWithFixedDelay(
- new SynOrder(), 0, 1, TimeUnit.SECONDS); // 创建并执行一个在给定初始延迟后首次启用的定期操作,随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟。
- ScheduledFuture future2 = executor.scheduleWithFixedDelay(
- new SynReTicket(), 0, 1, TimeUnit.SECONDS);
- ScheduledFuture future3 = executor.scheduleWithFixedDelay(
- new SysValidateOrder(), 0, 1, TimeUnit.SECONDS);
- BlockingQueue<ScheduledFuture> blockingQueue = new ArrayBlockingQueue<ScheduledFuture>(
- 3, true); // true:按照 FIFO 顺序访问插入或移除时受阻塞线程的队列
- blockingQueue.add(future1);
- blockingQueue.add(future2);
- blockingQueue.add(future3);
- while (!blockingQueue.isEmpty()) {
- ScheduledFuture future = blockingQueue.poll(); // poll:获取并移除此队列的头,如果此队列为空,则返回
- // null
- if (!future.isDone()) // isDone:如果任务已完成,则返回 true
- blockingQueue.add(future);
- else
- System.out.println(future.get());
- }
- }
- }
- /**
- * 进程(同步订单)
- * @author Administrator
- *
- */
- class SynOrder extends Service implements Runnable {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- this.SynOrderList();
- TimeUnit.SECONDS.sleep(Integer.parseInt(RestClientConfig
- .getInstance().getSynTicketSecond()));
- System.out.println("票务同步休眠"
- + RestClientConfig.getInstance().getSynTicketSecond()
- + "秒---------------------------");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 进程(同步验票)
- * @author Administrator
- *
- */
- class SysValidateOrder extends Service implements Runnable {
- @Override
- public void run() {
- try {
- this.SyncValidateOrder();
- TimeUnit.SECONDS.sleep(Integer.parseInt(RestClientConfig
- .getInstance().getSysValidateOrderSecond()));
- System.out.println("验票同步休眠"
- + RestClientConfig.getInstance()
- .getSysValidateOrderSecond()
- + "秒---------------------------");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 进程(同步退票)
- * @author Administrator
- *
- */
- class SynReTicket extends Service implements Runnable {
- @Override
- public void run() {
- try {
- TimeUnit.SECONDS.sleep(Integer.parseInt(RestClientConfig
- .getInstance().getReTicketSecond()));
- this.SyncReTicket();
- System.out.println("退票同步休眠"
- + RestClientConfig.getInstance().getReTicketSecond()
- + "秒---------------------------");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1007201513066.html
来源: http://www.codesnippet.cn/detail/1007201513066.html