Java定时任务的方式

1.Timer&&TimerTask

package bruceLee.jyangzi5.util;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Test2 {
    private final Timer timer = new Timer();
    private void playSound() {
    System.out.println(new Date()+"--test");
        }
    public void start() {
        timer.schedule(new TimerTask() {
            public void run() {               
            playSound();
            }
        }, 0, 3000);
    }
    public static void main(String[] args) {
    Test2 eggTimer = new Test2();
        eggTimer.start();
    }
}
-------------------------------------------------------------------
2.Thread.sleep

package cn.com.xinli.xiaowu.thread;

import java.util.Date;

public class Tread1 extends Thread
{
public void run()
{   
  try
  {
     while(true)
     {    //业务方法  此处只作打印
         System.out.println(new Date()+"--test");
    this.sleep(1000*3);
     }
  }
  catch (InterruptedException e)
  {
  e.printStackTrace();
  }
  }

public static void main(String arg[])
    {
new Tread1().start();
    }
}

--------------------------------------------------------
3.quartz(强烈推荐)

参考 :http://jyangzi5.iteye.com/admin/blogs/643437

你可能感兴趣的:(java,thread,quartz)