3

System get hang/freeze when i use util or swing Timer. How can i get ride of this?

Other user interface i am calling this Task as below, after retrying, the whole system get frozen.

TimerTask t  = new Task("Local",a); 
java.util.Timer timer = new java.util.Timer();
timer.scheduleAtFixedRate(t, 0, 10000);

Try 3: FAIL

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();
  }

}

I have also tried following method's but all same result system get freezed, and manually i have to power off and on the PC.

Try 0: FAIL

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

Try 1: FAIL

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

Try 2: FAIL

new Thread(new Runnable() {
  public void run() {
   t = new javax.swing.Timer(10000, new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
     // same
    }
   });
t.start();
  }
}).start();
  • 1
    Ran your code, no freeze. Waits for 4 seconds, terminates normally. – Marko Topolnik Jul 25 '12 at 12:05
  • @Marko Topolnik: Very very very strange, here its getting freeze always. I am running that task like this on Linux Java 6 e.g: https://gist.github.com/3175934 –  Jul 25 '12 at 12:35
  • When i do not use that timer, it simply works. The moment i am using timer, it just getting freeze. :-( –  Jul 25 '12 at 12:36
  • But that's not the same code, I don't even see the `run` method in there. – Marko Topolnik Jul 25 '12 at 12:37
  • 1
    If it freezes, take a thread dump (e.g. using jstack) and locate the deadlock – Robin Jul 25 '12 at 12:42

1 Answers1

1

Strange but since i start running it as following: (still it was not showing to be freeze)

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        EventQueue.invokeLater(new Runnable() {

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

    }
});

Reference:

https://stackoverflow.com/a/5500371/285594

Community
  • 1
  • 1