[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[cp-patches] Patch: Thread addon for shutdown hooks.
From: |
Guilhem Lavaux |
Subject: |
[cp-patches] Patch: Thread addon for shutdown hooks. |
Date: |
Fri, 30 Jul 2004 10:42:14 +0200 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040115 |
Hi list,
While merging the VMThread model in kaffe, I've noticed that Runtime
does not check whether hooks had already been started when they are
being added. So I've added one small method to Thread and the
corresponding checking in Runtime.addShutdownHook. The error reporting
has also been improved by providing messages to exceptions.
Comments ?
Guilhem.
ChangeLog Entry:
2004-07-30 Guilhem Lavaux <address@hidden>
* java/lang/Thread.java
(hasDied): New method.
(alreadyStarted): New field.
(die): Set alreadyStarted to true.
* java/lang/Runtime.java
(addShutdownHook): Check whether the hook has already been started
and has
died. Improved error messages.
Index: java/lang/Runtime.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Runtime.java,v
retrieving revision 1.7
diff -u -r1.7 Runtime.java
--- java/lang/Runtime.java 30 Apr 2004 15:22:41 -0000 1.7
+++ java/lang/Runtime.java 30 Jul 2004 08:32:37 -0000
@@ -341,16 +341,16 @@
SecurityManager sm = securityManager; // Be thread-safe!
if (sm != null)
sm.checkPermission(new RuntimePermission("shutdownHooks"));
- if (hook.isAlive() || hook.getThreadGroup() == null)
- throw new IllegalArgumentException();
+ if (hook.isAlive() || hook.hasDied() || hook.getThreadGroup() == null)
+ throw new IllegalArgumentException("The hook thread " + hook + " must
not have been already run or started");
synchronized (libpath)
{
if (exitSequence != null)
- throw new IllegalStateException();
+ throw new IllegalStateException("The Virtual Machine is exiting. It
is not possible anymore to add any hooks");
if (shutdownHooks == null)
shutdownHooks = new HashSet(); // Lazy initialization.
if (! shutdownHooks.add(hook))
- throw new IllegalArgumentException();
+ throw new IllegalArgumentException(hook.toString() + " had already
been inserted");
}
}
Index: java/lang/Thread.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Thread.java,v
retrieving revision 1.8
diff -u -r1.8 Thread.java
--- java/lang/Thread.java 29 Jun 2004 10:04:41 -0000 1.8
+++ java/lang/Thread.java 30 Jul 2004 08:32:38 -0000
@@ -130,6 +130,9 @@
/** The next thread number to use. */
private static int numAnonymousThreadsCreated;
+ /** True if the thread has already been started */
+ private boolean alreadyStarted;
+
/**
* Allocates a new <code>Thread</code> object. This constructor has
* the same effect as <code>Thread(null, null,</code>
@@ -968,5 +971,14 @@
{
group.removeThread(this);
vmThread = null;
+ alreadyStarted = true;
+ }
+
+ /**
+ * Returns true if the thread had already been started once.
+ */
+ final boolean hasDied()
+ {
+ return alreadyStarted;
}
}