Don't run as demonpackage TEST;
import java.util.*;
class mytask extends TimerTask
{
    public void run()
    {
        System.out.println("Time:");
    }
}public class Parce
{    public static void main(String[] args)
    {
        Timer mytime = new Timer();
        mytask task=new mytask();
        mytime.scheduleAtFixedRate(task,
                                   0, 1000);   }}It works,at least at JB7

解决方案 »

  1.   

    I just search the daemon concept.
    For my poor skill,I still can not explain that.The following link illustrates what is the daemon under linuxhttp://www01.softhouse.com.cn/linux/knowledge/tech/7320.html
      

  2.   

    这里的
    new Timer(true);  true,false到底是指什么呢??为什么我看别人的列子都是true的??
      

  3.   

    找了找资料,说是true是守护进程,,那我的这段程序定时器,也应该是new Timer(true),才对啊?为什么又只能运行一次啊??有高手解惑么??
      

  4.   

    看来都是这个true惹得祸。我也在关注这个daemon。up一下
      

  5.   

    Here is the answer.a daemon thread is a background thread that is subordinate to the thread that creates it. If the thread that created the daemon thread end, the daemon thread dies with it.for you case, you timer thread is created by the thread that created by the main(). However, the main thread ended after created the timer thread. so, timer thread also dies with it. 
      

  6.   

    To  dmhorse(dmhorse):
       这是我看到的一点资料:
       守护线程是在Java程序运行到所有用户线程结束之后,有系统将他强迫终止掉。在Java虚拟机中,即使在main()方法结束后,如果另一个用户线程在运行,则守护线程仍然可以继续运行。。
         
        这段话和你说的那段,是一个意思么??是不是说,如果我现在的这个定时器的main()里,有两个线程在跑的话,那么main()结束后, 这个daemon thread 还可以运行的,现在我的main()里只有一个定时的线程在跑,所以在main()结束后,这个daemon thread 也结束了。 当是new Timer()时,因为是个用户线程,所以可以一直跑??
       谢谢
      

  7.   

    To keep the main thread alive and make the timer run as daemonpublic class Parce
    {
        public static void main(String[] args)
        {    Timer mytime = new Timer(true);
        mytask task=new mytask();
        mytime.scheduleAtFixedRate(task,
                                   0, 1000);
        while(true){}
        }}