CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  框架、开源

请问谁有定时任务的源程序(比如每过1秒就打印“hello”),在线等!!!急急!!!

楼主czhai(新手)2004-09-02 13:53:41 在 Java / 框架、开源 提问

请问谁有定时任务的源程序(比如每过1秒就打印“hello”),在线等!!!急急!!! 问题点数:100、回复次数:6Top

1 楼qjhaaaaa()回复于 2004-09-02 14:02:32 得分 20

public   class   FileMonitor  
          extends   Thread   {  
      public   FileMonitor()   {  
      }  
   
      public   static   Log4J   log4j   =   new   Log4J();  
      Object   cl;  
      String   action;  
      int   timeout;  
   
      public   FileMonitor(Object   cl,   String   action,   int   timeout)   {  
          this.cl   =   cl;  
          this.action   =   action;  
          this.timeout   =   timeout;  
      }  
   
      public   void   run()   {  
          while   (true)   {  
              Object   o   =   new   Object();  
              synchronized   (o)   {  
                  try   {  
                      o.wait(timeout);  
                      Method   method   =   cl.getClass().getMethod(action,   null);  
                      method.invoke(cl,   null);  
                  }  
                  catch   (Exception   ex)   {  
                      log4j.error.error("run()   :   "   +   ex.getMessage());  
                      ex.getMessage();  
                  }  
              }  
          }  
      }  
  }  
   
   
  public   class   Main   {  
  public   static   void   main(String[]   args)   throws   Exception   {  
          Main   main   =   new   Main();  
          new   FileMonitor(main,   "FileMonitor",   10000).start();  
  }Top

2 楼yangjuanli(珂儿)回复于 2004-09-07 08:23:14 得分 20

public   class   AddZcm   extends   Thread  
  {  
      public   AddZcm(){  
   
      }  
        public   static   void   main(String[]   args)  
        {       while(true)  
                {  
                try{  
                                      System.out.println("hello   world!");  
                    Thread.sleep(1000);  
                                  }  
                }  
        }  
  }Top

3 楼tjl713(tjl)回复于 2004-09-07 08:42:53 得分 20

yangjuanli(珂儿)   :  
          你的程序可能会有问题!  
          如果用线程的休眠方法,那么程序表明至少等待1秒才能运行,但并不表明1秒后一定是这个线程被唤起继续运行,如果操作系统内有其它的线程,也有可能是它们被唤起,然后才是这个程序执行。  
          个人意见,大家讨论!Top

4 楼Acylas(Acylas)回复于 2004-09-07 08:54:35 得分 20

javax.swing.Timer   timer   =   new   javax.swing.Timer(1000,   new   ActionListener()   {  
            public   void   actionPerformed(java.awt.event.ActionEvent   event)   {  
                    System.out.println("hello");  
            }  
  });  
  或者用  
  java.util.Timer   timer   =   new   Timer();  
  timer.schedule(new   TimerTask()   {  
          public   void   run()   {  
                System.out.println("hello");  
          }  
  },   0,   1000);  
  Top

5 楼sandyen(杉叶)回复于 2004-09-07 09:08:57 得分 20

public   class   Task{  
  public   void   run()   {  
        System.out.println("Hello");  
  }}  
   
  public   class   Main{  
          java.util.Timer   timer   =   new   java.util.Timer();  
          public   static   void   main(String[]   args)   {  
                  timer.schedule(new   Task(),   0,   1000);  
  }  
  }Top

6 楼power_zh(专门在技术区灌水赚分)回复于 2004-09-08 21:20:46 得分 0

upTop

相关问题

  • 急急......急需代理服务器的资料和源程序!
  • 急!!急!!急!!高分求收发手机中文短信的VB源程序
  • :急急急!!!请问那位大哥有FTP的源程序,请给我一个,谢谢,急用!!!
  • 急需用VB在文本框中编辑数学公式的源程序,急急急
  • 急急急。。。。大家帮我看看下面这段源程序。。谢谢大家了
  • 源程序
  • c源程序。
  • 求源程序!
  • ****寻求源程序*****
  • 寻找源程序!

关键词

  • log4j
  • 线程
  • filemonitor
  • timer
  • cl
  • timeout
  • hello
  • 程序
  • main
  • action

得分解答快速导航

  • 帖主:czhai
  • qjhaaaaa
  • yangjuanli
  • tjl713
  • Acylas
  • sandyen

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo