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

Add method isTimerThread() to Timer

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • None
    • core-libs
    • x86
    • windows_7

      A DESCRIPTION OF THE REQUEST :
      It would be nice for debugging purposes if there were a method of Timer that would return `true` if the calling thread is the timer's thread; `false` otherwise. Such a method could be used within assertions which assert that the current thread is the timer thread of a given Timer instance.

      JUSTIFICATION :
      The new method would be useful to assert that the current thread is the same as the timer thread of a given Timer instance. It would thus be similar to the isEventDispatchThread() static method of SwingUtilities, and the isDispatchThread() static method of EventQueue, which can both be used in assertions to verify that a given block of code that modifies a Swing/AWT component is running on the EDT.

      Within the run() override of a TimerTask, the programmer can be fairly certain that the calling thread is *a* timer thread, but perhaps the `run` implementation needs to execute on the timer thread of a particular Timer instance. An assertion could be added to assert that the current thread is the timer thread of that Timer instance using the new isTimerThread() method.

      Another potential use is, within a private member function that is called from the `run` override of timer tasks, to assert that the calling thread is the timer thread of a particular Timer. Using assertions this way can help to verify that access to data is properly synchronized.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Two new methods would be added to Timer:

          public boolean isTimerThread() {
              return isTimerThread(Thread.currentThread());
          }

          public boolean isTimerThread(Thread thread);

      The first method would return `true` iff the current thread (obtained with `Thread.currentThread()`) is the same as the timer's timer thread.

      The second method would return `true` iff the given thread is the same as the timer's timer thread.

      isTimerThread(Thread) would return `false` if the Thread parameter is `null`.

      ACTUAL -
      No such method exists in the Java SE 6 or Java SE 7 APIs.

      ---------- BEGIN SOURCE ----------
      After the two methods are added to Timer, the following program, executed with assertions enabled, would exit without error:


      import java.util.Timer;
      import java.util.TimerTask;

      public class App {
          public static void main(String[] args) {
              final Timer timer = new Timer();

              assert !timer.isTimerThread();
              
              Thread t = new Thread();
              assert !timer.isTimerThread(t);

              timer.schedule(new TimerTask() {
                  @Override
                  public void run() {
                      assert timer.isTimerThread();

                      System.exit(0);
                  }
              }, 1L);
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      It is possible to access the thread instance using reflection and then compare the given thread for equality:

          private static Field TIMER_THREAD_FIELD;
          static {
              Field fields[] = Timer.class.getDeclaredFields();
              for (Field field : fields) {
                  if (Thread.class.isAssignableFrom(field.getType())) {
                      TIMER_THREAD_FIELD = field;
                      field.setAccessible(true);
                      break;
                  }
              }
          }

          /**
           * Returns @c true if @p thread is the timer thread of @p timer.
           *
           * @param timer The timer of interest.
           * @return @c true if @p thread equals the timer
           * thread of @p timer; @c false otherwise.
           * @author Daniel Trebbien
           * @date 2011-07-02
           */
          public static boolean isTimerThreadOf(Timer timer, Thread thread) {
              final Object timerThread;
              try {
                  timerThread = TIMER_THREAD_FIELD.get(timer);
              } catch (IllegalAccessException ex) {
                  throw new RuntimeException(ex);
              }
              if (timerThread == null) {
                  return thread == null;
              } else {
                  return timerThread.equals(thread);
              }
          }

          /**
           * Returns @c true if the calling thread is the timer thread of @p timer.
           *
           * @param timer The timer of interest.
           * @return @c true if <code>Thread.currentThread()</code> equals the timer
           * thread of @p timer; @c false otherwise.
           * @author Daniel Trebbien
           * @date 2011-07-02
           */
          public static boolean isTimerThreadOf(Timer timer) {
              return isTimerThreadOf(timer, Thread.currentThread());
          }

            smarks Stuart Marks
            coffeys Sean Coffey
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: