/**
 * 源代码如下
 * 不解处:TalkingClock.start方法执行(clock.start(1000, true);)后,方法内部生成的Timer t对象已经不处于生存期,有点糊涂?
 * 希望能说得透彻些!
 */import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;public class AnonymousInnerClassTest
{
   public static void main(String[] args)
   {
      TalkingClock clock = new TalkingClock();
      clock.start(1000, true);      // keep program running until user selects "Ok"
      JOptionPane.showMessageDialog(null, "Quit program?");
      System.exit(0);
   }
}/**
 * A clock that prints the time in regular intervals.
 */
class TalkingClock
{
   /**
    * Starts the clock.
    * @param interval the interval between messages (in milliseconds)
    * @param beep true if the clock should beep
    */
   public void start(int interval, final boolean beep)
   {
      ActionListener listener = new ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               Date now = new Date();
               System.out.println("At the tone, the time is " + now);
               if (beep) Toolkit.getDefaultToolkit().beep();
            }
         };
      Timer t = new Timer(interval, listener);
      t.start();
   }
}

解决方案 »

  1.   

    /** 
    * 源代码如下 
    * 不解处:TalkingClock.start方法执行(clock.start(1000, true);)后,方法内部生成的Timer t对象已经不处于生存期,有点糊涂? 
    * 希望能说得透彻些! 
    */ 
    【code=Java】
    import java.awt.*; 
    import java.awt.event.*; 
    import java.util.*; 
    import javax.swing.*; 
    import javax.swing.Timer; public class AnonymousInnerClassTest 

      public static void main(String[] args) 
      { 
          TalkingClock clock = new TalkingClock(); 
          clock.start(1000, true);       // keep program running until user selects "Ok" 
          JOptionPane.showMessageDialog(null, "Quit program?"); 
          System.exit(0); 
      } 
    } /** 
    * A clock that prints the time in regular intervals. 
    */ 
    class TalkingClock 

      /** 
        * Starts the clock. 
        * @param interval the interval between messages (in milliseconds) 
        * @param beep true if the clock should beep 
        */ 
      public void start(int interval, final boolean beep) 
      { 
          ActionListener listener = new ActionListener() 
            { 
                public void actionPerformed(ActionEvent event) 
                { 
                  Date now = new Date(); 
                  System.out.println("At the tone, the time is " + now); 
                  if (beep) Toolkit.getDefaultToolkit().beep(); 
                } 
            }; 
          Timer t = new Timer(interval, listener); 
          t.start(); 
      } 

    【/code】
      

  2.   

    /** 
    * 源代码如下 
    * 不解处:TalkingClock.start方法执行(clock.start(1000, true);)后,方法内部生成的Timer t对象已经不处于生存期,有点糊涂? 
    * 希望能说得透彻些! 
    */ 
    import java.awt.*; 
    import java.awt.event.*; 
    import java.util.*; 
    import javax.swing.*; 
    import javax.swing.Timer; public class AnonymousInnerClassTest 

      public static void main(String[] args) 
      { 
          TalkingClock clock = new TalkingClock(); 
          clock.start(1000, true);       // keep program running until user selects "Ok" 
          JOptionPane.showMessageDialog(null, "Quit program?"); 
          System.exit(0); 
      } 
    } /** 
    * A clock that prints the time in regular intervals. 
    */ 
    class TalkingClock 

      /** 
        * Starts the clock. 
        * @param interval the interval between messages (in milliseconds) 
        * @param beep true if the clock should beep 
        */ 
      public void start(int interval, final boolean beep) 
      { 
          ActionListener listener = new ActionListener() 
            { 
                public void actionPerformed(ActionEvent event) 
                { 
                  Date now = new Date(); 
                  System.out.println("At the tone, the time is " + now); 
                  if (beep) Toolkit.getDefaultToolkit().beep(); 
                } 
            }; 
          Timer t = new Timer(interval, listener); 
          t.start(); 
      } 

      

  3.   

    我的意思是,在方法内定义的对象在方法执行完之后object reference已经作废,对象也变成一个垃圾内存空间,在这种情况下程序怎么能正常运作下去呢?
      

  4.   

    1. The start method is called.
    2. The object variable listener is initialized by a call to the constructor of the inner class TimePrinter.
    3. The listener reference is passed to the Timer constructor, the timer is started, and the start method exits. At this point, the beep parameter variable of the start method no longer exists.
    4. A second later, the actionPerformed method executes就是说从上面第3步到第4步,是如何良好运转的,我有迷惑。
      

  5.   

    Timer 对象是在方法当中定义的,但这并不表示方法退出后 Timer 对象就会要回收。当 Timer 开始运行后,虽然方法退出了,但 Timer 对象的引用仍然存在。垃圾回收是一个复杂的过程,但同时也是很严谨的。楼主不需要担心运行中的线程会被回收。这是不可能的。
      

  6.   

    对象引用是作废了,根据java的基本语法规则。
    但Timer类里有静态成员,这些是不会作废的。
      

  7.   

    这是timer的实现问题,属于类库部分,如果仅仅使用就不要关心了吧。