日期:2014-05-17 浏览次数:20760 次
public static void main(String ss[]) { ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); Runnable doThing = new Runnable() { public void run() { System.out.println("dddd"); } }; scheduler.scheduleAtFixedRate(doThing, 0, 3, TimeUnit.SECONDS); }
------解决方案--------------------
package com.jx.sms.tools; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimerTask; /** * 定时发送处理 * * @author Administrator * */ public class MyTask extends TimerTask { public void run() { // System.out.println("call at " + (new Date())); Connection conn = DataBase_local.getConn(); Statement stmt = null; ResultSet rs = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery("select * from unsendsms"); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); java.util.Calendar c1 = java.util.Calendar.getInstance(); java.util.Calendar c2 = java.util.Calendar.getInstance(); String d1 = df.format(new Date()); while (rs.next()) { String d2 = rs.getString("sendTime");// 获取短信的发送时间 c1.setTime(df.parse(d1)); c2.setTime(df.parse(d2)); int result = c1.compareTo(c2);// 比较大小 if (result == 0 || result > 0) { SMS.send(rs.getString("telphone") + ",", rs .getString("messageContent"), rs.getInt("smsType")); // 发送待发短信 Statement stmt1 = conn.createStatement(); stmt1.execute("delete from unsendsms where id='" + rs.getInt("id") + "'");// 发送完成后删除待发短信 stmt1.close(); } } } catch (Exception e) { e.printStackTrace(); System.out.println("call at " + (new Date()) + " database error"); return; } finally { try { rs.close(); stmt.close();