一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - java基于線程池和反射機(jī)制實(shí)現(xiàn)定時(shí)任務(wù)完整實(shí)例

java基于線程池和反射機(jī)制實(shí)現(xiàn)定時(shí)任務(wù)完整實(shí)例

2020-01-13 17:395iasp JAVA教程

這篇文章主要介紹了java基于線程池和反射機(jī)制實(shí)現(xiàn)定時(shí)任務(wù)的方法,以完整實(shí)例形式較為詳細(xì)的分析了Java定時(shí)任務(wù)的功能原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了java基于線程池和反射機(jī)制實(shí)現(xiàn)定時(shí)任務(wù)的方法。分享給大家供大家參考,具體如下:

主要包括如下實(shí)現(xiàn)類:

1. Main類:

任務(wù)執(zhí)行的入口:

調(diào)用main方法,開始加載任務(wù)配置并執(zhí)行任務(wù)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.yanek.task;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Main {
 /**
  * @param args
  */
 public static void main(String[] args) {
  ScheduledExecutorService scheduExec = Executors.newScheduledThreadPool(1);
  /*
  TaskModel tm=new TaskModel();
  tm.setClassName("com.yanek.task.TaskA");
  tm.setMethodName("testA");
  tm.setInitialDelay(3);
  tm.setPeriod(5);
  */
  List tasks=XmlReader.getTasks();
  for (int i=0;i<tasks.size();i++)
  {
   TaskModel tm=(TaskModel)tasks.get(i);
   scheduExec.scheduleAtFixedRate(new MyTask(tm),tm.getInitialDelay(), tm.getPeriod(), TimeUnit.SECONDS);
  }
 }
}

2. MyTask 類 實(shí)現(xiàn)Runnable接口,在main類中調(diào)用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.yanek.task;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
public class MyTask implements Runnable {
 private TaskModel taskModel;
 public MyTask() {}
 public MyTask(TaskModel tm) {
  this.taskModel = tm;
 }
 public void run() {
   System.out.println("call at " + (new Date()));
   try {
    Class<?> classType = Class.forName(taskModel.getClassName());
    Method getMethod = classType.getMethod(taskModel.getMethodName());
    getMethod.invoke(classType);
   } catch (SecurityException e) {
    e.printStackTrace();
   } catch (IllegalArgumentException e) {
    e.printStackTrace();
   } catch (ClassNotFoundException e) {
    e.printStackTrace();
   } catch (NoSuchMethodException e) {
    e.printStackTrace();
   } catch (IllegalAccessException e) {
    e.printStackTrace();
   } catch (InvocationTargetException e) {
    e.printStackTrace();
   }
 }
}

3. TaskModel: 對(duì)任務(wù)類的封裝

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.yanek.task;
public class TaskModel {
 public String getClassName() {
  return className;
 }
 public void setClassName(String className) {
  this.className = className;
 }
 public String getMethodName() {
  return methodName;
 }
 public void setMethodName(String methodName) {
  this.methodName = methodName;
 }
 public long getInitialDelay() {
  return initialDelay;
 }
 public void setInitialDelay(long initialDelay) {
  this.initialDelay = initialDelay;
 }
 public long getPeriod() {
  return period;
 }
 public void setPeriod(long period) {
  this.period = period;
 }
 private String className;
 private String methodName;
 private long initialDelay;
 private long period;
}

4. XmlReader 任務(wù)配置解析類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.yanek.task;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class XmlReader {
 public static void main(String[] args) {
  XmlReader.getTasks();
 }
 public static List getTasks() {
  List tasks = new ArrayList();
  System.out.println("load task config start...");
  String path = "/work/TaskManager/conf/taskconfig.xml";
  File file = new File(path);
  if (file.exists() && !file.isDirectory()) {
   try {
    SAXBuilder sx = new SAXBuilder();
    Document doc = sx.build(file);
    Element rootelement = doc.getRootElement();
     List<Element> childs = rootelement.getChildren();
     for (int i = 0; i < childs.size(); i++) {
      TaskModel tModel = new TaskModel();
      tModel.setClassName(childs.get(i).getChildText("class"));
      System.out.println(childs.get(i).getChildText("class"));
      tModel.setMethodName(childs.get(i).getChildText("method"));
      System.out.println(childs.get(i).getChildText("method"));
      String initialDelay = childs.get(i).getChildText("initialDelay");
      tModel.setInitialDelay((Long.valueOf(initialDelay)));
      System.out.println("距離首次運(yùn)行還差" + initialDelay + "秒!");
      tModel.setPeriod(Integer.valueOf(childs.get(i).getChildText("period")));
      System.out.println(childs.get(i).getChildText("period"));
      tasks.add(tModel);
    }
   } catch (NumberFormatException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (JDOMException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  } else {
   System.out.println("file no exist!");
  }
  System.out.println("load task config end !");
  return tasks;
 }
}

5. 配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<taskconfig>
  <task>
    <class>com.yanek.task.TaskA</class>
    <method>testA</method>
    <initialDelay>5</initialDelay>
    <period>2</period>
  </task>
  <task>
    <class>com.yanek.task.TaskB</class>
    <method>testB</method>
    <initialDelay>5</initialDelay>
    <period>3</period>
  </task>
  <task>
    <class>com.yanek.task.TaskC</class>
    <method>testC</method>
    <initialDelay>5</initialDelay>
    <period>3</period>
  </task>
</taskconfig>

6. 測(cè)試任務(wù)類:

TaskA TaskB TaskC其中定義靜態(tài)方法 ,這些類的靜態(tài)方法配置在 xml文件中,被調(diào)用。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.yanek.task;
public class TaskA {
 /**
  * @param args
  */
 public static void main(String[] args) {
  System.out.println("task a test");
 }
 public static void testA()
 {
  System.out.println("taska testA method call!");
 }
}
package com.yanek.task;
public class TaskB {
 /**
  * @param args
  */
 public static void main(String[] args) {
  System.out.println("task b test");
 }
 public static void testB()
 {
  System.out.println("TaskB testB method call!");
 }
}
package com.yanek.task;
public class TaskC {
 /**
  * @param args
  */
 public static void main(String[] args) {
  System.out.println("task c test");
 }
 public static void testC()
 {
  System.out.println("Taskc testC method call!");
 }
}

希望本文所述對(duì)大家Java程序設(shè)計(jì)有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 婷综合 | 無码一区中文字幕少妇熟女网站 | 国产亚洲精品九九久在线观看 | 精品国语对白精品自拍视 | 亚洲天堂成人在线观看 | 无码人妻丰满熟妇啪啪网不卡 | 国产人成精品午夜在线观看 | 免费看全黄特黄毛片 | 午夜第九达达兔鲁鲁 | 日本肉体xxxx69xxxx | 娇妻与老头绿文小说系列 | 欧美一区二区日韩一区二区 | 亚洲国产欧美在线成人aaaa | 羞羞影院午夜男女爽爽影院网站 | 91久久国产视频 | 男女xxoo做爰猛烈动态一 | 亚洲国产精品综合久久一线 | 国产性视频 | 国产成人影院在线观看 | 成 人 亚洲 综合天堂 | 掰开逼操 | 国产乱码一卡二卡3卡四卡 国产乱插 | 亚洲日韩男人网在线 | 亚洲精品中文字幕久久久久久 | 16男男gaygays| 亚洲免费精品 | 动漫美女隐私尿口图片 | 精品国产乱码久久久久久软件 | 欧美做受 | 欧美性欲| 陈峰姚瑶全集小说无删节 | 暖暖的视频完整视频韩国免费 | 日本乱子 | 欧美午夜视频一区二区三区 | 欧美国产日产精品免费视频 | 亚洲欧美日韩在线观看看另类 | 草莓茄子丝瓜番茄小蝌蚪 | 亚洲国产在线午夜视频无 | 日韩精品视频观看 | 91久久综合九色综合欧美98 | 麻豆视频网 |