你的程序运行到最后自动退出了,没必要用这个。
System.exit(0);是强制退出的。比如做了个窗口,当人点x按纽时就调用System.exit(0);来退出

解决方案 »

  1.   

    System.exit(0);是正常退出
    System.exit(n);n如果是其它数字就是费正常退出
      

  2.   

    但我发现,注释掉System.exit后,程序要等一会才退出。
    而 加上 只要一点按键 马上就退出了。
    这是什么原因呢?
      

  3.   

    估计是Timer还没有执行完
    虽然你调用了Stop,但是不会停止正在执行的动作
      

  4.   

    就是说 System.exit(0)是用于多线程
    也就是Timer类是个任务类 初始化了一个线程对象。当我把System.exit 和 t.stop 都注释掉后,程序仍能正常退出。
    t.start() 不是一直执行吗?
      

  5.   

    忘了说了 我说的Timer类是 javax.swing.Timer;
      

  6.   

    这样的话,
    那Listener舰艇的事件又没有执行完呢?
      

  7.   

    看了一下javax.swing.Timer的文档,原来是线程的原因。
    Timer t = new Timer(2000, listener)是另开了个线程,而主程序退出时其他的线程均要退出
    t.start()启动这个线程,t.stop()正常停止该线程,但并没撤消该线程。
    System.exit(),我估计是撤消主程序中开启的一切 线程。而没有System.exit()的话,主程序退出后,其他线程会被强制撤消,也就是非正常退出。是这样的吗?而 treeroot(天才--天天被人踩,人才--人人都想踩) 说的 其他用户线程没退出,主程序不会退出我觉得 主程序是main的话,线程应该被强制退出,并撤消回收掉了。
    不然,注释掉System.exit() 和 t.stop()。程序仍能正常退出怎么解释呢?
      

  8.   

    附完整代码:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.Timer;
    // to resolve conflict with java.util.Timerpublic class TimerTest
    {
       public static void main(String[] args)
       {
          ActionListener listener = new TimePrinter();      // construct a timer that calls the listener
          // once every 10 seconds
          Timer t = new Timer(2000, listener);      
          t.start();
          JOptionPane.showMessageDialog(null, "Quit program?");
          t.stop();      
          System.exit(0);
       }
    }class TimePrinter implements ActionListener
    {
       public void actionPerformed(ActionEvent event)
       {
          Date now = new Date();
          System.out.println("At the tone, the time is " + now);
          Toolkit.getDefaultToolkit().beep();
       }
    }class Singleton
    {
    private Singleton( int n, String s)
    {
    num = n;
    str = s;
    }
    public Singleton getInstance( int n, String s)
    {
    Singleton ob = new Singleton( n, s);
    return ob;
    }

    int num;
    String str;
    }
      

  9.   

    我觉得 主程序是main的话,线程应该被强制退出,并撤消回收掉了。
    -------
    楼主理解错了,尽管main()这个主线程结束了,但是其它没有执行完得线程还是会继续执行到结束为止的!你看这个例子:
    class MyThread extends Thread {
    pulic void run() {
    for (int i=0;i<1000;i++) {
    System.out.println("Print "+i);
    }
    }
    }
    public class Test {
    public static void main (String []args) {
    new MyTread().start();
    System.out.println("Main() exits");
    }
    }
    执行的结果是主函数先退出,而自己的线程还是会继续执行的。但是在c/c++里面就不一样了,main()方法是程序的入口和出口,所以一般是这样来写:
    int main() {
    ...
    exit(0);//或者return 0;
    }
    这样就保证了主函数是程序的出口。java里面主函数只是入口,所以要想结束所有线程必须用System.exit(0);