thread.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,528 行 · 第 1/4 页
JAVA
1,528 行
} } /** * This method is called by the system to manage thread * startup and exit. */ private void startup(boolean nativeOverride) { try { try { if (!stillborn && ThreadRegistry.threadCreationAllowed()) { if (nativeOverride) { CVM.setContextArtificial(); runNative(); CVM.postThreadExit(); } else { CVM.setDebugEvents(true); run(); CVM.postThreadExit(); CVM.setDebugEvents(false); } } } finally { // disable any future async/remote exceptions CVM.disableRemoteExceptions(); CVM.maskInterrupts(); stillborn = true; } } catch (Throwable uncaughtException) { exit(uncaughtException); return; } exit(null); } /** * Used to indicate thread shutdown. */ private static native void setThreadExiting(); private native void runNative(); /** * Forces the thread to stop executing. * <p> * If there is a security manager installed, its <code>checkAccess</code> * method is called with <code>this</code> * as its argument. This may result in a * <code>SecurityException</code> being raised (in the current thread). * <p> * If this thread is different from the current thread (that is, the current * thread is trying to stop a thread other than itself), the * security manager's <code>checkPermission</code> method (with a * <code>RuntimePermission("stopThread")</code> argument) is called in * addition. * Again, this may result in throwing a * <code>SecurityException</code> (in the current thread). * <p> * The thread represented by this thread is forced to stop whatever * it is doing abnormally and to throw a newly created * <code>ThreadDeath</code> object as an exception. * <p> * It is permitted to stop a thread that has not yet been started. * If the thread is eventually started, it immediately terminates. * <p> * An application should not normally try to catch * <code>ThreadDeath</code> unless it must do some extraordinary * cleanup operation (note that the throwing of * <code>ThreadDeath</code> causes <code>finally</code> clauses of * <code>try</code> statements to be executed before the thread * officially dies). If a <code>catch</code> clause catches a * <code>ThreadDeath</code> object, it is important to rethrow the * object so that the thread actually dies. * <p> * The top-level error handler that reacts to otherwise uncaught * exceptions does not print out a message or otherwise notify the * application if the uncaught exception is an instance of * <code>ThreadDeath</code>. * * exception SecurityException if the current thread cannot * modify this thread. * see java.lang.Thread#interrupt() * see java.lang.Thread#checkAccess() * see java.lang.Thread#run() * see java.lang.Thread#start() * see java.lang.ThreadDeath * see java.lang.ThreadGroup#uncaughtException(java.lang.Thread, * java.lang.Throwable) * see SecurityManager#checkAccess(Thread) * see SecurityManager#checkPermission * deprecated This method is inherently unsafe. Stopping a thread with * Thread.stop causes it to unlock all of the monitors that it * has locked (as a natural consequence of the unchecked * <code>ThreadDeath</code> exception propagating up the stack). If * any of the objects previously protected by these monitors were in * an inconsistent state, the damaged objects become visible to * other threads, potentially resulting in arbitrary behavior. Many * uses of <code>stop</code> should be replaced by code that simply * modifies some variable to indicate that the target thread should * stop running. The target thread should check this variable * regularly, and return from its run method in an orderly fashion * if the variable indicates that it is to stop running. If the * target thread waits for long periods (on a condition variable, * for example), the <code>interrupt</code> method should be used to * interrupt the wait. * For more information, see * <a href="{docRoot}/../guide/misc/threadPrimitiveDeprecation.html">Why * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. * public final void stop() { stop1(new ThreadDeath(), true); } */ /** * Forces the thread to stop executing. * <p> * If there is a security manager installed, the <code>checkAccess</code> * method of this thread is called, which may result in a * <code>SecurityException</code> being raised (in the current thread). * <p> * If this thread is different from the current thread (that is, the current * thread is trying to stop a thread other than itself) or * <code>obj</code> is not an instance of <code>ThreadDeath</code>, the * security manager's <code>checkPermission</code> method (with the * <code>RuntimePermission("stopThread")</code> argument) is called in * addition. * Again, this may result in throwing a * <code>SecurityException</code> (in the current thread). * <p> * If the argument <code>obj</code> is null, a * <code>NullPointerException</code> is thrown (in the current thread). * <p> * The thread represented by this thread is forced to complete * whatever it is doing abnormally and to throw the * <code>Throwable</code> object <code>obj</code> as an exception. This * is an unusual action to take; normally, the <code>stop</code> method * that takes no arguments should be used. * <p> * It is permitted to stop a thread that has not yet been started. * If the thread is eventually started, it immediately terminates. * * param obj the Throwable object to be thrown. * exception SecurityException if the current thread cannot modify * this thread. * see java.lang.Thread#interrupt() * see java.lang.Thread#checkAccess() * see java.lang.Thread#run() * see java.lang.Thread#start() * see java.lang.Thread#stop() * see SecurityManager#checkAccess(Thread) * see SecurityManager#checkPermission * deprecated This method is inherently unsafe. See {@link #stop} * (with no arguments) for details. An additional danger of this * method is that it may be used to generate exceptions that the * target thread is unprepared to handle (including checked * exceptions that the thread could not possibly throw, were it * not for this method). * For more information, see * <a href="{@docRoot}/../guide/misc/threadPrimitiveDeprecation.html">Why * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. * public final void stop(Throwable obj) { stop1(obj, obj instanceof ThreadDeath); } */ /* Only used by deprecated methods private final void stop1(Throwable obj, boolean death) { if (obj == null) { throw new NullPointerException(); } synchronized (lock) { SecurityManager security = System.getSecurityManager(); if (security != null) { checkAccess(); if ((this != Thread.currentThread()) || !death) { if (stopThreadPermission == null) stopThreadPermission = new RuntimePermission("stopThread"); security.checkPermission(stopThreadPermission); } if ((this != Thread.currentThread()) || !death) { if (stopThreadPermission == null) stopThreadPermission = new RuntimePermission("stopThread"); security.checkPermission(stopThreadPermission); } } if (alive && !stillborn) { CVM.throwRemoteException(this, obj); stillborn = death; resume(); // Wake up thread if it was suspended; no-op otherwise } } } */ /** * Interrupts this thread. * * <p> First the {@link #checkAccess() checkAccess} method of this thread * is invoked, which may cause a {@link SecurityException} to be thrown. * * <p> If this thread is blocked in an invocation of the {@link * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link * Object#wait(long, int) wait(long, int)} methods of the {@link Object} * class, or of the {@link #join()}, {@link #join(long)}, {@link * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)}, * methods of this class, then its interrupt status will be cleared and it * will receive an {@link InterruptedException}. * * <p> If none of the previous conditions hold then this thread's interrupt * status will be set. </p> * * @throws SecurityException * if the current thread cannot modify this thread * * @revised 1.4 * @spec JSR-51 */ public void interrupt() { checkAccess(); synchronized (lock) { wasInterrupted = true; interrupt0(); } } /** * Tests whether the current thread has been interrupted. The * <i>interrupted status</i> of the thread is cleared by this method. In * other words, if this method were to be called twice in succession, the * second call would return false (unless the current thread were * interrupted again, after the first call had cleared its interrupted * status and before the second call had examined it). * * @return <code>true</code> if the current thread has been interrupted; * <code>false</code> otherwise. * @see java.lang.Thread#isInterrupted() */ public static boolean interrupted() { Thread self = currentThread(); synchronized (self.lock) { if (self.wasInterrupted) { self.wasInterrupted = false; return self.isInterrupted(true); } } return false; } /** * Tests whether this thread has been interrupted. The <i>interrupted * status</i> of the thread is unaffected by this method. * * @return <code>true</code> if this thread has been interrupted; * <code>false</code> otherwise. * @see java.lang.Thread#interrupted() */ public boolean isInterrupted() { return wasInterrupted && isInterrupted(false); } /** * Tests if some Thread has been interrupted. The interrupted state * is reset or not based on the value of ClearInterrupted that is * passed. */ private native boolean isInterrupted(boolean ClearInterrupted); /** * Destroys this thread, without any cleanup. Any monitors it has * locked remain locked. (This method is not implemented.) */ public void destroy() { throw new NoSuchMethodError(); } /** * Tests if this thread is alive. A thread is alive if it has * been started and has not yet died. * * @return <code>true</code> if this thread is alive; * <code>false</code> otherwise. */ public final boolean isAlive() { synchronized (lock) { return alive; } } /** * Suspends this thread. * <p> * First, the <code>checkAccess</code> method of this thread is called * with no arguments. This may result in throwing a * <code>SecurityException </code>(in the current thread). * <p> * If the thread is alive, it is suspended and makes no further * progress unless and until it is resumed. * * exception SecurityException if the current thread cannot modify * this thread. * see #checkAccess * deprecated This method has been deprecated, as it is * inherently deadlock-prone. If the target thread holds a lock on the * monitor protecting a critical system resource when it is suspended, no * thread can access this resource until the target thread is resumed. If * the thread that would resume the target thread attempts to lock this * monitor prior to calling <code>resume</code>, deadlock results. Such * deadlocks typically manifest themselves as "frozen" processes. * For more information, see * <a href="{docRoot}/../guide/misc/threadPrimitiveDeprecation.html">Why * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. * public final void suspend() { checkAccess(); suspend0(); } */ /** * Resumes a suspended thread. * <p> * First, the <code>checkAccess</code> method of this thread is called * with no arguments. This may result in throwing a * <code>SecurityException</code> (in the current thread). * <p> * If the thread is alive but suspended, it is resumed and is * permitted to make progress in its execution. * * exception SecurityException if the current thread cannot modify this * thread. * see #checkAccess * see java.lang.Thread#suspend() * deprecated This method exists solely for use with {@link #suspend}, * which has been deprecated because it is deadlock-prone. * For more information, see * <a href="{docRoot}/../guide/misc/threadPrimitiveDeprecation.html">Why * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. * public final void resume() { checkAccess(); resume0(); } */ /** * Changes the priority of this thread. * <p> * First the <code>checkAccess</code> method of this thread is called * with no arguments. This may result in throwing a * <code>SecurityException</code>. * <p> * Otherwise, the priority of this thread is set to the smaller of * the specified <code>newPriority</code> and the maximum permitted * priority of the thread's thread group. * * @param newPriority priority to set this thread to * @exception IllegalArgumentException If the priority is not in the * range <code>MIN_PRIORITY</code> to * <code>MAX_PRIORITY</code>. * @exception SecurityException if the current thread cannot modify * this thread. * @see #getPriority * @see java.lang.Thread#checkAccess() * @see java.lang.Thread#getPriority() * @see java.lang.Thread#getThreadGroup() * @see java.lang.Thread#MAX_PRIORITY * @see java.lang.Thread#MIN_PRIORITY * @see java.lang.ThreadGroup#getMaxPriority() */ public final void setPriority(int newPriority) { checkAccess(); if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) { throw new IllegalArgumentException(); } if (newPriority > group.getMaxPriority()) { newPriority = group.getMaxPriority(); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?