Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Util或Swing任何一种Java定时器都会使我的整个系统挂起、冻结,如何保护系统崩溃?_Java_Swing_Time - Fatal编程技术网

Util或Swing任何一种Java定时器都会使我的整个系统挂起、冻结,如何保护系统崩溃?

Util或Swing任何一种Java定时器都会使我的整个系统挂起、冻结,如何保护系统崩溃?,java,swing,time,Java,Swing,Time,当我使用util或swing定时器时,系统被挂起/冻结。我怎样才能骑上它 其他用户界面我调用此任务如下,重试后,整个系统冻结 TimerTask t = new Task("Local",a); java.util.Timer timer = new java.util.Timer(); timer.scheduleAtFixedRate(t, 0, 10000); 尝试3:失败 import java.util.Timer; import java.util.TimerTask; imp

当我使用util或swing定时器时,系统被挂起/冻结。我怎样才能骑上它

其他用户界面我调用此任务如下,重试后,整个系统冻结

TimerTask t  = new Task("Local",a); 
java.util.Timer timer = new java.util.Timer();
timer.scheduleAtFixedRate(t, 0, 10000);
尝试3:失败

import java.util.Timer;
import java.util.TimerTask;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class Task extends TimerTask {

  private static Timer timer;
  //private static ui.V v;

  private static int input;
  private static String sinput;

  public static void main (String... arguments ) {
    TimerTask f  = new Task("Local:",20); 
    timer = new Timer();
    timer.scheduleAtFixedRate(f, 0, 10000);   
  }

  public Task(String sinput, int input) {
    this.input = input;
    this.sinput = sinput;
  }

  public void run() {
    System.out.println("Will freeze, dont freeze.....");
    //v = new ui.V(sinput);  loading a user interface
    //v.up(input); show the value
    try {
      Thread.sleep(4000); // wait 4 seconds
    } catch (InterruptedException ex) {
      Logger.getLogger(Task.class.getName()).log(Level.SEVERE, null, ex);
    }
    //v.killme(); // kill the display
    timer.cancel();
  }

}
我也尝试过以下方法,但所有相同的结果系统都被冻结,我必须手动关闭和打开电脑

尝试0:失败

t = new java.util.Timer();
t.schedule(new TimerTask() {
      @Override
   public void run() {
    // same
   }
}, 0, 10000);
尝试1:失败

t = new javax.swing.Timer(10000, new ActionListener() {
  public void actionPerformed(ActionEvent ae) {
    // same
  }
});
t.start();
尝试2:失败

new Thread(new Runnable() {
  public void run() {
   t = new javax.swing.Timer(10000, new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
     // same
    }
   });
t.start();
  }
}).start();

奇怪的是,因为我开始按如下方式运行它:(仍然没有显示为冻结)

参考:


运行您的代码,没有冻结。等待4秒,正常终止。@Marko Topolnik:非常奇怪,这里总是冻结。我在LinuxJava6上像这样运行这个任务,例如:当我不使用那个计时器时,它就工作了。我使用定时器的那一刻,它就冻结了-(但这不是同一个代码,我甚至没有看到那里的
run
方法。如果它冻结,则进行线程转储(例如使用jstack)并找到死锁
button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        EventQueue.invokeLater(new Runnable() {

          @Override
          public void run() {
                ....here... timer seems ... less... risky....
            }
        });

    }
});