原生方式

    1. package javatimer;
    2. import java.text.ParseException;
    3. import java.text.SimpleDateFormat;
    4. import java.util.*;
    5. public class JavaTimer {
    6. //做一个小计时器
    7. //模拟每天晚上 给所有同学发送短信(邮件)
    8. public static void main(String[] args) throws ParseException {
    9. System.out.println("开始运行");
    10. //存储用户名、手机号、邮箱......
    11. //最好创建一个集合(随时扩容)
    12. String[] userArray={"小慧","小榆","小小慧","久久"};
    13. ArrayList<String> userList=new ArrayList<>(Arrays.asList(userArray));
    14. //自定义发送时间和发送间隔
    15. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
    16. Date firstTime=sdf.parse("2021-08-23 18:28:30");
    17. //找一个计时器对象帮我们计时,时间到了就做事
    18. Timer timer=new Timer();
    19. timer.schedule(new TimerTask() {
    20. @Override
    21. public void run() {//最终的任务
    22. for(String user: userList){
    23. System.out.println(user+"你睡了吗");
    24. }
    25. }
    26. },firstTime,3000);
    27. }
    28. }

    image.png

    Spring方式管理Timer
    需要的包
    image.png
    image.png
    ApplicationContext.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xsi:schemaLocation=" http://www.springframework.org/schema/beans
    3. https://www.springframework.org/schema/beans/spring-beans.xsd"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns="http://www.springframework.org/schema/beans">
    6. <!--我们自己定义的任务类交给Spring来管理-->
    7. <bean id="springTask" class="springtimer.SpringTask"></bean>
    8. <!--创建一个Spring提供好的计时器对象,用来做倒计时管控(可以理解为前面的Timer对象)-->
    9. <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    10. <property name="targetObject" ref="springTask"></property>
    11. <property name="targetMethod" value="doSomething"></property>
    12. </bean>
    13. <!--创建一个Spring提供的触发器对象,一旦上面的目标对象及方法确定,触发器就开始进行实践的计算及循环-->
    14. <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    15. <property name="jobDetail" ref="jobDetail"></property>
    16. <property name="cronExpression" value="35/5 52 19 * * ?"></property>
    17. </bean>
    18. <!-- 将我们设置好的触发器对象 放置在一个Spring管理的集合对象中 SchedulerFactoryBean -->
    19. <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    20. <property name="triggers">
    21. <list>
    22. <ref bean="cronTrigger"/>
    23. </list>
    24. </property>
    25. </bean>
    26. </beans>

    SpringTask

    package springtimer;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    
    /**
     * 可以理解为是刚才的那个Task
     * 类中只有一个具体的事情(任务)
     * 没有timer帮我们做倒计时
     */
    public class SpringTask {
        String[] userArray={"小慧","小榆","小小慧","久久"};
        ArrayList<String> userList=new ArrayList<>(Arrays.asList(userArray));
    
        //具体的事情(刚才那个任务的方法)
        public void doSomething() {
            for(String user: userList){
                System.out.println(user+"你睡了吗");
            }
        }
    }
    

    TestMain

    package springtimer;
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestMain {
    
        public static void main(String[] args) {
            BeanFactory factory = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        }
    }