classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] Thread.sleep() update


From: Mark Wielaard
Subject: [cp-patches] Thread.sleep() update
Date: Thu, 30 Dec 2004 14:16:05 +0100

Hi,

Some runtimes (most notably kaffe after they merged in our Thread
implementation [it was correct before that, oops...]) would interpret
sleep(0) to sleep forever. This came from a copy/paste error in our
sleep() documentation. It was based on the language in the JLS1 book
which used the same language to descripe join() and sleep(). I have
updated the documentation and implementation to follow how the JCL2v1
book describes the behavour of Thread.sleep(). I also added a couple of
test to Mauve (gnu.testlet.java.lang.Thread.sleep). These tests depend
on my interpretation of Thread.sleep() throwing InterruptedException
when the Thread was interrupted (and the interrupted flag wasn't yet
cleared). This seems to be how gcj and kaffe also interpret it (but not
how jamvm does it).

2004-12-30  Mark Wielaard  <address@hidden>

       * java/lang/Thread.java (sleep): Update documentation. Make sure
       VMThread.sleep() is never called with zero arguments.
       * vm/reference/java/lang/VMThread.java (sleep): Update documentation.
       * NEWS: Mention change in VMThread.sleep() interface.

Cheers,

Mark
Index: NEWS
===================================================================
RCS file: /cvsroot/classpath/classpath/NEWS,v
retrieving revision 1.60
diff -u -r1.60 NEWS
--- NEWS        28 Dec 2004 10:28:27 -0000      1.60
+++ NEWS        30 Dec 2004 13:01:05 -0000
@@ -14,6 +14,9 @@
 * String and StringBuffer now call VMSystem.arraycopy() directly and don't
   go through java.lang.System. Be careful to not initialize java.lang.System
   early in the bootstrap sequence in your VM runtime interface classes.
+* VMThread.sleep() will never be called with zero arguments (don't sleep).
+  VMThread does not have to do any extra argument checking. Some (wrong)
+  documentation about the behavior of this method has been updated.
 
 New in release 0.12 (Nov 14, 2004)
 
Index: java/lang/Thread.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Thread.java,v
retrieving revision 1.10
diff -u -r1.10 Thread.java
--- java/lang/Thread.java       6 Dec 2004 20:43:13 -0000       1.10
+++ java/lang/Thread.java       30 Dec 2004 13:01:05 -0000
@@ -769,11 +769,11 @@
    * are no guarantees which thread will be next to run, but most VMs will
    * choose the highest priority thread that has been waiting longest.
    *
-   * @param ms the number of milliseconds to sleep, or 0 for forever
-   * @throws InterruptedException if the Thread is interrupted; it's
-   *         <i>interrupted status</i> will be cleared
-   * @see #notify()
-   * @see #wait(long)
+   * @param ms the number of milliseconds to sleep.
+   * @throws InterruptedException if the Thread is (or was) interrupted;
+   *         it's <i>interrupted status</i> will be cleared
+   * @throws IllegalArgumentException if ms is negative
+   * @see #interrupt()
    */
   public static void sleep(long ms) throws InterruptedException
   {
@@ -785,27 +785,37 @@
    * time. The Thread will not lose any locks it has during this time. There
    * are no guarantees which thread will be next to run, but most VMs will
    * choose the highest priority thread that has been waiting longest.
+   * <p>
+   * Note that 1,000,000 nanoseconds == 1 millisecond, but most VMs
+   * do not offer that fine a grain of timing resolution. When ms is
+   * zero and ns is non-zero the Thread will sleep for at least one
+   * milli second. There is no guarantee that this thread can start up
+   * immediately when time expires, because some other thread may be
+   * active.  So don't expect real-time performance.
    *
-   * <p>Note that 1,000,000 nanoseconds == 1 millisecond, but most VMs do
-   * not offer that fine a grain of timing resolution. Besides, there is
-   * no guarantee that this thread can start up immediately when time expires,
-   * because some other thread may be active.  So don't expect real-time
-   * performance.
-   *
-   * @param ms the number of milliseconds to sleep, or 0 for forever
+   * @param ms the number of milliseconds to sleep
    * @param ns the number of extra nanoseconds to sleep (0-999999)
-   * @throws InterruptedException if the Thread is interrupted; it's
-   *         <i>interrupted status</i> will be cleared
-   * @throws IllegalArgumentException if ns is invalid
-   * @see #notify()
-   * @see #wait(long, int)
+   * @throws InterruptedException if the Thread is (or was) interrupted;
+   *         it's <i>interrupted status</i> will be cleared
+   * @throws IllegalArgumentException if ms or ns is negative
+   *         or ns is larger than 999999.
+   * @see #interrupt()
    */
   public static void sleep(long ms, int ns) throws InterruptedException
   {
-    if(ms < 0 || ns < 0 || ns > 999999)
-       throw new IllegalArgumentException();
+    if (ms < 0 || ns < 0 || ns > 999999)
+      throw new IllegalArgumentException();
+
+    if (ns > 0 && ms == 0)
+      {
+       ms = 1;
+       ns = 0;
+      }
 
-    VMThread.sleep(ms, ns);
+    if (ms > 0)
+      VMThread.sleep(ms, ns);
+    else if (interrupted())
+      throw new InterruptedException();
   }
 
   /**
Index: vm/reference/java/lang/VMThread.java
===================================================================
RCS file: /cvsroot/classpath/classpath/vm/reference/java/lang/VMThread.java,v
retrieving revision 1.2
diff -u -r1.2 VMThread.java
--- vm/reference/java/lang/VMThread.java        28 Jun 2004 19:39:16 -0000      
1.2
+++ vm/reference/java/lang/VMThread.java        30 Dec 2004 13:01:05 -0000
@@ -365,11 +365,10 @@
      * because some other thread may be active.  So don't expect real-time
      * performance.
      *
-     * @param ms the number of milliseconds to sleep, or 0 for forever
+     * @param ms the number of milliseconds to sleep. Will be at least 1.
      * @param ns the number of extra nanoseconds to sleep (0-999999)
-     * @throws InterruptedException if the Thread is interrupted; it's
-     *         <i>interrupted status</i> will be cleared
-     * @throws IllegalArgumentException if ns is invalid
+     * @throws InterruptedException if the Thread is (or was) interrupted;
+     *         it's <i>interrupted status</i> will be cleared
      */
     static native void sleep(long ms, int ns) throws InterruptedException;
 

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]