亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2015年11月03日 12:44:27   作者:5iasp  
這篇文章主要介紹了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方法,開(kāi)始加載任務(wù)配置并執(zhí)行任務(wù)

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)用

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ù)類的封裝

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ù)配置解析類

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. 配置文件:

<?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)用。

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ì)有所幫助。

相關(guān)文章

  • springcloud 熔斷監(jiān)控Hystrix Dashboard和Turbine

    springcloud 熔斷監(jiān)控Hystrix Dashboard和Turbine

    這篇文章主要介紹了springcloud 熔斷監(jiān)控Hystrix Dashboard和Turbine,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Spring配置文件使用占位符配置方式

    Spring配置文件使用占位符配置方式

    這篇文章主要介紹了Spring配置文件使用占位符配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Java  mysql數(shù)據(jù)庫(kù)并進(jìn)行內(nèi)容查詢實(shí)例代碼

    Java mysql數(shù)據(jù)庫(kù)并進(jìn)行內(nèi)容查詢實(shí)例代碼

    這篇文章主要介紹了Java mysql數(shù)據(jù)庫(kù)并進(jìn)行內(nèi)容查詢實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • Kafka簡(jiǎn)單客戶端編程實(shí)例

    Kafka簡(jiǎn)單客戶端編程實(shí)例

    這篇文章主要為大家詳細(xì)介紹了Kafka簡(jiǎn)單客戶端編程實(shí)例,利用Kafka的API進(jìn)行客戶端編程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • SpringCloud的Hystrix簡(jiǎn)單介紹

    SpringCloud的Hystrix簡(jiǎn)單介紹

    這篇文章主要介紹了SpringCloud的Hystrix簡(jiǎn)單介紹,SpringCloud Hystrix是Netflix開(kāi)源的一款容錯(cuò)框架,具備服務(wù)降級(jí),服務(wù)熔斷,依賴隔離,監(jiān)控(Hystrix Dashboard)等功能,同樣具有自我保護(hù)能力,需要的朋友可以參考下
    2023-07-07
  • Spring Cloud Feign的文件上傳實(shí)現(xiàn)的示例代碼

    Spring Cloud Feign的文件上傳實(shí)現(xiàn)的示例代碼

    這篇文章主要介紹了Spring Cloud Feign的文件上傳實(shí)現(xiàn)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • Springboot中用 Netty 開(kāi)啟UDP服務(wù)方式

    Springboot中用 Netty 開(kāi)啟UDP服務(wù)方式

    這篇文章主要介紹了Springboot中用 Netty 開(kāi)啟UDP服務(wù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • SpringBoot整合Jasypt實(shí)現(xiàn)配置加密的步驟詳解

    SpringBoot整合Jasypt實(shí)現(xiàn)配置加密的步驟詳解

    Jasypt是一個(gè)Java庫(kù),提供了一種簡(jiǎn)單的加密解密方式,可用于保護(hù)敏感數(shù)據(jù),例如密碼、API密鑰和數(shù)據(jù)庫(kù)連接信息等,本文給大家介紹了SpringBoot整合Jasypt實(shí)現(xiàn)配置加密的詳細(xì)步驟,感興趣的同學(xué)可以參考一下
    2023-11-11
  • Java項(xiàng)目打包發(fā)布到maven私倉(cāng)常見(jiàn)的幾種方式

    Java項(xiàng)目打包發(fā)布到maven私倉(cāng)常見(jiàn)的幾種方式

    這篇文章主要介紹了項(xiàng)目打包發(fā)布到maven私倉(cāng)常見(jiàn)的幾種方式,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-03-03
  • Java基礎(chǔ)知識(shí)之CharArrayReader流的使用

    Java基礎(chǔ)知識(shí)之CharArrayReader流的使用

    這篇文章主要介紹了Java基礎(chǔ)知識(shí)之CharArrayReader流的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12

最新評(píng)論