thread.java

来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 1,017 行 · 第 1/3 页

JAVA
1,017
字号
     * a newly generated name. Automatically generated names are of the
     * form <code>"Thread-"+</code><i>n</i>, where <i>n</i> is an integer.
     *
     * @param      group    the thread group.
     * @param      target   the object whose <code>run</code> method is called.
     * @exception  SecurityException  if the current thread cannot create a
     *               thread in the specified thread group.
     * @see        java.lang.Thread#Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String)
     * @since      JDK1.0
     */
    public Thread(ThreadGroup group, Runnable target) {
	init(group, target, "Thread-" + nextThreadNum());
    }

    /**
     * Allocates a new <code>Thread</code> object. This constructor has
     * the same effect as <code>Thread(null, null, name)</code>.
     *
     * @param   name   the name of the new thread.
     * @see     java.lang.Thread#Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String)
     * @since   JDK1.0
     */
    public Thread(String name) {
	init(null, null, name);
    }

    /**
     * Allocates a new <code>Thread</code> object. This constructor has
     * the same effect as <code>Thread(group, null, name)</code>
     *
     * @param      group   the thread group.
     * @param      name    the name of the new thread.
     * @exception  SecurityException  if the current thread cannot create a
     *               thread in the specified thread group.
     * @see        java.lang.Thread#Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String)
     * @since   JDK1.0
     */
    public Thread(ThreadGroup group, String name) {
	init(group, null, name);
    }

    /**
     * Allocates a new <code>Thread</code> object. This constructor has
     * the same effect as <code>Thread(null, target, name)</code>.
     *
     * @param   target   the object whose <code>run</code> method is called.
     * @param   name     the name of the new thread.
     * @see     java.lang.Thread#Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String)
     * @since   JDK1.0
     */
    public Thread(Runnable target, String name) {
	init(null, target, name);
    }

    /**
     * Allocates a new <code>Thread</code> object so that it has
     * <code>target</code> as its run object, has the specified
     * <code>name</code> as its name, and belongs to the thread group
     * referred to by <code>group</code>.
     * <p>
     * If <code>group</code> is not <code>null</code>, the
     * <code>checkAccess</code> method of that thread group is called with
     * no arguments; this may result in throwing a
     * <code>SecurityException</code>; if <code>group</code> is
     * <code>null</code>, the new process belongs to the same group as
     * the thread that is creating the new thread.
     * <p>
     * If the <code>target</code> argument is not <code>null</code>, the
     * <code>run</code> method of the <code>target</code> is called when
     * this thread is started. If the target argument is
     * <code>null</code>, this thread's <code>run</code> method is called
     * when this thread is started.
     * <p>
     * The priority of the newly created thread is set equal to the
     * priority of the thread creating it, that is, the currently running
     * thread. The method <code>setPriority</code> may be used to
     * change the priority to a new value.
     * <p>
     * The newly created thread is initially marked as being a daemon
     * thread if and only if the thread creating it is currently marked
     * as a daemon thread. The method <code>setDaemon </code> may be used
     * to change whether or not a thread is a daemon.
     *
     * @param      group     the thread group.
     * @param      target   the object whose <code>run</code> method is called.
     * @param      name     the name of the new thread.
     * @exception  SecurityException  if the current thread cannot create a
     *               thread in the specified thread group.
     * @see        java.lang.Runnable#run()
     * @see        java.lang.Thread#run()
     * @see        java.lang.Thread#setDaemon(boolean)
     * @see        java.lang.Thread#setPriority(int)
     * @see        java.lang.ThreadGroup#checkAccess()
     * @since      JDK1.0
     */
    public Thread(ThreadGroup group, Runnable target, String name) {
	init(group, target, name);
    }

    /**
     * Causes this thread to begin execution; the Java Virtual Machine
     * calls the <code>run</code> method of this thread.
     * <p>
     * The result is that two threads are running concurrently: the
     * current thread (which returns from the call to the
     * <code>start</code> method) and the other thread (which executes its
     * <code>run</code> method).
     *
     * @exception  IllegalThreadStateException  if the thread was already
     *               started.
     * @see        java.lang.Thread#run()
     * @see        java.lang.Thread#stop()
     * @since      JDK1.0
     */
    public synchronized native void start();

    /**
     * If this thread was constructed using a separate
     * <code>Runnable</code> run object, then that
     * <code>Runnable</code> object's <code>run</code> method is called;
     * otherwise, this method does nothing and returns.
     * <p>
     * Subclasses of <code>Thread</code> should override this method.
     *
     * @see     java.lang.Thread#start()
     * @see     java.lang.Thread#stop()
     * @see     java.lang.Thread#Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String)
     * @see     java.lang.Runnable#run()
     * @since   JDK1.0
     */
    public void run() {
	if (target != null) {
	    target.run();
	}
    }

    /**
     * This method is called by the system to give a Thread
     * a chance to clean up before it actually exits.
     */
    private void exit() {
	if (group != null) {
	    group.remove(this);
	    group = null;
	}
	/* Aggressively null object connected to Thread: see bug 4006245 */
	target = null;
    }

    /**
     * Forces the thread to stop executing.
     * <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>
     * 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#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)
     * @since      JDK1.0
     */
    public final void stop() {
	synchronized (this) {
            /*
	     * ibm.8337 - If the Thread we are trying to stop() has already stopped then
             * its ThreadGroup variable (group) may be null: thus causing a mismatch 
             * between the ThreadGroup of the SecurityManager and the ThreadGroup of the
             * Thread to be stopped.
             *
	     * It looks as though the ThreadGroup should not be set to null by the
	     * Thread.exit() method, but this will be raised as a separate defect since
	     * it requires more investigation than is available before code-freeze.
	     *
	     * The reason this problem came to light in JDK 1.1.7 is that Sun deliberately
	     * changed Thread.stop() so that it did not invoke Thread.stop(Throwable o): thus
             * making the classLoaderDepth 1 less i.e. == 3 by the time it was checked by the
             * SecurityManager's checkAccess(). Sun wished the SecurityManager to validate the
             * ThreadGroups but did not seem to consider what would happen if the Thread to be 
             * stopped had already completed.
             */	    
	    try
	    {		
	    	checkAccess();
	    }
	    catch(SecurityException se)
	    {
		if ( (getThreadGroup() != null) )
		    throw se;
		else
		    return;	
	    }

	    resume();	// Wake up thread if it was suspended; no-op otherwise
	    stop0(new ThreadDeath());
	}
    }

    /**
     * Forces the thread to stop executing.
     * <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 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#checkAccess()
     * @see        java.lang.Thread#run()
     * @see        java.lang.Thread#start()
     * @see        java.lang.Thread#stop()
     * @since      JDK1.0
     */
    public final synchronized void stop(Throwable o) {
	checkAccess();
	resume();	// Wake up thread if it was suspended; no-op otherwise
	stop0(o);
    }

    /**
     * Interrupts this thread.
     *
     * @since   JDK1.0
     */
	// Note that this method is not synchronized.  Three reasons for this:
	// 1) It changes the API.
	// 2) It's another place where the system could hang.
	// 3) All we're doing is turning on a one-way bit.  It doesn't matter
	//    exactly when it's done WRT probes via the interrupted() method.
    public void interrupt() {
            /*
	     * ibm.8377 - If the Thread we are trying to interrupt() has already stopped then
             * its ThreadGroup variable (group) may be null: thus causing a mismatch 
             * between the ThreadGroup of the SecurityManager and the ThreadGroup of the
             * Thread to be stopped.
             *
	     * It looks as though the ThreadGroup should not be set to null by the
	     * Thread.exit() method, but this will be raised as a separate defect since
	     * it requires more investigation than is available before code-freeze.
	     *
             */	    
	    try
	    {		
	    	checkAccess();
	    }
	    catch(SecurityException se)
	    {
		if ( (getThreadGroup() != null) )
		    throw se;
		else
		    return;	
	    }

            interrupt0();
    }

    /**
     * Tests if the current thread has been interrupted.
     * Note that <code>interrupted</code> is a static method, while
     * <code>isInterrupted</code> is called on the current
     * <code>Thread</code> instance.
     *
     * @return  <code>true</code> if the current thread has been interrupted;
     *          <code>false</code> otherwise.
     * @see     java.lang.Thread#isInterrupted()
     * @since   JDK1.0
     */
    public static boolean interrupted() {
	return currentThread().isInterrupted(true);
    }

    /**
     * Tests if the current thread has been interrupted.
     * Note that <code>isInterrupted</code>
     * is called on the current <code>Thread</code> instance; by
     * contrast, <code>interrupted</code> is a static method.
     *
     * @return  <code>true</code> if this thread has been interrupted;
     *          <code>false</code> otherwise.
     * @see     java.lang.Thread#interrupted()
     * @since   JDK1.0
     */
    public boolean isInterrupted() {
	return isInterrupted(false);
    }

    /**
     * Ask 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 in
     * Java&nbsp;1.0.2.)
     *
     * @since   JDK1.0
     */
    public void destroy() {
	throw new NoSuchMethodError();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?