Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4037641

Applet freezes when retrieving a future Date()

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.1
    • core-libs
    • x86
    • windows_95



      Name: mc57594 Date: 03/08/97

      The problem seems to have to do with sleep() combined with Date().
      Here's the exact Java source; the date was set to 2097.
      The message about the bug stays on screen forever, but when the
      year is set back to normal, after one second the date appears
      and starts counting seconds.
      Removing first sleep() lets the program go, but only as far as
      next Date() call after the second sleep() was called.

      import java.applet.*;
      import java.awt.*;
      import java.util.*;

      public class TimeBug extends Applet
          implements Runnable
      {
          boolean init = false;
          Thread main = null;
          boolean gotTime = false;
          String timeString = null;

          public void init()
          {
              if (!init)
              {
                  init = true;
                  /*
                  were this real code there would be something
                  useful here
                  */
              }
          }

          public void paint(Graphics g)
          {
              if (gotTime)
              {
                  g.drawString(timeString, 10, 10);
              }
              else
              {
                  g.drawString("If the date on your computer is set too far away from today," +
                               " the applet may freeze due to a Java bug.", 10, 10);
                  g.drawString("In that case, please reset the date and reload this page.", 10, 50);
              }
          }

          public void start()
          {
              if (main == null)
              {
                  main = new Thread(this);
                  main.start();
              }
          }

          public void stop()
          {
              if (main != null)
              {
                  main.stop();
                  main = null;
              }
          }

          public void run()
          {
              sleep(1000); //removing this line will let the applet go...
              Date date = new Date();
              gotTime = true;
              while (true)
              {
                  GetTime(); //but only as far as this point on the second iteration
                  repaint();
                  sleep(1000); // (after passing this point next Date() freezes applet)
              }
          }

          private void sleep(int t)
          {
              try
              {
                  Thread.sleep(t);
              }
              catch (InterruptedException e)
              {
              }
          }

          private void GetTime()
          {
                  Date date = new Date();
                  timeString = date.toString();
          }
      }

      Accompanying HTML:

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
      <html>
      <HEAD><TITLE>Time Bug</TITLE></HEAD>
      <body>
      <center>
      <applet code=TimeBug.class width=600 height=100></applet>
      </center>
      </body></HTML>




            ssenthilsunw Shanmugam Senthil (Inactive)
            mchamnessunw Mark Chamness (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: