public static void main(String args[]){
Runnable runnable = new Runnable() {
public void run() {
//定义查询语句
String queryString = "SELECT josoninfo from jd_back_order_insert_record WHERE returninfo not LIKE '%listDate%' AND returninfo not LIKE '%success%' and returninfo <>'' and create_time >='"+Creat_time+"'";
Log.info(queryString);
//查询得到returnInfo的数组
List<String> returnInfoList = getreturnInfo(driverName,driverUrl,UserName,PassWord,queryString );
Log.info("共获取
数据库中
记录数:"+returnInfoList.size());
//截取字符串“order_Info=”得到真正的json字符串数组
List<String> returnJsonStringArray = getOrderjson(returnInfoList);
//分析json值得到orderNum
List<String> rderListOrderNum = getOrderIdFromJson(returnJsonStringArray);
try {
AddOrderList(OrderListOrderNum);
Log.info("执行日期:"+date.formatDateTime(new Date())+"执行结束");
} catch (Exception e) {
// TODO Auto-generated catch block
Log.error("调用addorderlist函数插入订单号异常", e);
}
Creat_time = date.formatDateTime(new Date());
}
};
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
// 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间
service.scheduleAtFixedRate(runnable, 1, 1, TimeUnit.DAYS);
}
- publicclassTask1 {
- publicstaticvoidmain(String[] args) {
- finallongtimeInterval =1000;
- Runnable runnable =newRunnable() {
- publicvoidrun() {
- while(true) {
- System.out.println("Hello !!");
- try{
- Thread.sleep(timeInterval);
- }catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- };
- Thread thread =newThread(runnable);
- thread.start();
- }
- }
- importjava.util.Timer;
- importjava.util.TimerTask;
- publicclassTask2 {
- publicstaticvoidmain(String[] args) {
- TimerTask task =newTimerTask() {
- @Override
- publicvoidrun() {
- System.out.println("Hello !!!");
- }
- };
- Timer timer =newTimer();
- longdelay =0;
- longintevalPeriod =1*1000;
- timer.scheduleAtFixedRate(task, delay, intevalPeriod);
- }
- }
/**
* 普通thread
* 这是最常见的,创建一个thread,然后让它在while循环里一直运行着,
* 通过sleep方法来达到定时任务的效果。这样可以快速简单的实现,代码如下:
* @author GT
*
*/
public class Task1 {
public static void main(String[] args) {
// run in a second
final long timeInterval = 1000;
Runnable runnable = new Runnable() {
public void run() {
while (true) {
// ------- code for task to run
System.out.println("Hello !!");
// ------- ends here
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
参考地址http://blog.csdn.net/haorengoodman/article/details/23281343/