📄 thread.java
字号:
/* Thread -- an independent thread of executable code Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software FoundationThis file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.lang;import gnu.gcj.RawData;import gnu.gcj.RawDataManaged;/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3 * "The Java Language Specification", ISBN 0-201-63451-1 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com. * Status: Believed complete to version 1.4, with caveats. We do not * implement the deprecated (and dangerous) stop, suspend, and resume * methods. Security implementation is not complete. *//** * Thread represents a single thread of execution in the VM. When an * application VM starts up, it creates a non-daemon Thread which calls the * main() method of a particular class. There may be other Threads running, * such as the garbage collection thread. * * <p>Threads have names to identify them. These names are not necessarily * unique. Every Thread has a priority, as well, which tells the VM which * Threads should get more running time. New threads inherit the priority * and daemon status of the parent thread, by default. * * <p>There are two methods of creating a Thread: you may subclass Thread and * implement the <code>run()</code> method, at which point you may start the * Thread by calling its <code>start()</code> method, or you may implement * <code>Runnable</code> in the class you want to use and then call new * <code>Thread(your_obj).start()</code>. * * <p>The virtual machine runs until all non-daemon threads have died (either * by returning from the run() method as invoked by start(), or by throwing * an uncaught exception); or until <code>System.exit</code> is called with * adequate permissions. * * <p>It is unclear at what point a Thread should be added to a ThreadGroup, * and at what point it should be removed. Should it be inserted when it * starts, or when it is created? Should it be removed when it is suspended * or interrupted? The only thing that is clear is that the Thread should be * removed when it is stopped. * * @author Tom Tromey * @author John Keiser * @author Eric Blake (ebb9@email.byu.edu) * @see Runnable * @see Runtime#exit(int) * @see #run() * @see #start() * @see ThreadLocal * @since 1.0 * @status updated to 1.4 */public class Thread implements Runnable{ /** The minimum priority for a Thread. */ public static final int MIN_PRIORITY = 1; /** The priority a Thread gets by default. */ public static final int NORM_PRIORITY = 5; /** The maximum priority for a Thread. */ public static final int MAX_PRIORITY = 10; /** * The group this thread belongs to. This is set to null by * ThreadGroup.removeThread when the thread dies. */ ThreadGroup group; /** The object to run(), null if this is the target. */ private Runnable runnable; /** The thread name, non-null. */ String name; /** Whether the thread is a daemon. */ private boolean daemon; /** The thread priority, 1 to 10. */ private int priority; boolean interrupt_flag; private boolean alive_flag; private boolean startable_flag; /** The context classloader for this Thread. */ private ClassLoader contextClassLoader; // This describes the top-most interpreter frame for this thread. RawData interp_frame; // Our native data - points to an instance of struct natThread. private RawDataManaged data; /** * Allocates a new <code>Thread</code> object. This constructor has * the same effect as <code>Thread(null, null,</code> * <i>gname</i><code>)</code>, where <b><i>gname</i></b> is * a newly generated name. Automatically generated names are of the * form <code>"Thread-"+</code><i>n</i>, where <i>n</i> is an integer. * <p> * Threads created this way must have overridden their * <code>run()</code> method to actually do anything. An example * illustrating this method being used follows: * <p><blockquote><pre> * import java.lang.*; * * class plain01 implements Runnable { * String name; * plain01() { * name = null; * } * plain01(String s) { * name = s; * } * public void run() { * if (name == null) * System.out.println("A new thread created"); * else * System.out.println("A new thread with name " + name + * " created"); * } * } * class threadtest01 { * public static void main(String args[] ) { * int failed = 0 ; * * <b>Thread t1 = new Thread();</b> * if (t1 != null) * System.out.println("new Thread() succeed"); * else { * System.out.println("new Thread() failed"); * failed++; * } * } * } * </pre></blockquote> * * @see java.lang.Thread#Thread(java.lang.ThreadGroup, * java.lang.Runnable, java.lang.String) */ public Thread() { this(null, null, gen_name()); } /** * Allocates a new <code>Thread</code> object. This constructor has * the same effect as <code>Thread(null, target,</code> * <i>gname</i><code>)</code>, where <i>gname</i> is * 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 target the object whose <code>run</code> method is called. * @see java.lang.Thread#Thread(java.lang.ThreadGroup, * java.lang.Runnable, java.lang.String) */ public Thread(Runnable target) { this(null, target, gen_name()); } /** * 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) */ public Thread(String name) { this(null, null, name); } /** * Allocates a new <code>Thread</code> object. This constructor has * the same effect as <code>Thread(group, target,</code> * <i>gname</i><code>)</code>, where <i>gname</i> is * 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 group to put the Thread into * @param target the Runnable object to execute * @throws SecurityException if this thread cannot access <code>group</code> * @throws IllegalThreadStateException if group is destroyed * @see #Thread(ThreadGroup, Runnable, String) */ public Thread(ThreadGroup group, Runnable target) { this(group, target, gen_name()); } /** * Allocates a new <code>Thread</code> object. This constructor has * the same effect as <code>Thread(group, null, name)</code> * * @param group the group to put the Thread into * @param name the name for the Thread * @throws NullPointerException if name is null * @throws SecurityException if this thread cannot access <code>group</code> * @throws IllegalThreadStateException if group is destroyed * @see #Thread(ThreadGroup, Runnable, String) */ public Thread(ThreadGroup group, String name) { this(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 Runnable object to execute * @param name the name for the Thread * @throws NullPointerException if name is null * @see #Thread(ThreadGroup, Runnable, String) */ public Thread(Runnable target, String name) { this(null, target, name); } /** * Allocate a new Thread object, with the specified ThreadGroup and name, and * using the specified Runnable object's <code>run()</code> method to * execute. If the Runnable object is null, <code>this</code> (which is * a Runnable) is used instead. * * <p>If the ThreadGroup is null, the security manager is checked. If a * manager exists and returns a non-null object for * <code>getThreadGroup</code>, that group is used; otherwise the group * of the creating thread is used. Note that the security manager calls * <code>checkAccess</code> if the ThreadGroup is not null. * * <p>The new Thread will inherit its creator's priority and daemon status. * These can be changed with <code>setPriority</code> and * <code>setDaemon</code>. * * @param group the group to put the Thread into * @param target the Runnable object to execute * @param name the name for the Thread * @throws NullPointerException if name is null * @throws SecurityException if this thread cannot access <code>group</code> * @throws IllegalThreadStateException if group is destroyed * @see Runnable#run() * @see #run() * @see #setDaemon(boolean) * @see #setPriority(int) * @see SecurityManager#checkAccess(ThreadGroup) * @see ThreadGroup#checkAccess() */ public Thread(ThreadGroup group, Runnable target, String name) { this(currentThread(), group, target, name); } /** * Allocate a new Thread object, as if by * <code>Thread(group, null, name)</code>, and give it the specified stack * size, in bytes. The stack size is <b>highly platform independent</b>, * and the virtual machine is free to round up or down, or ignore it * completely. A higher value might let you go longer before a * <code>StackOverflowError</code>, while a lower value might let you go * longer before an <code>OutOfMemoryError</code>. Or, it may do absolutely * nothing! So be careful, and expect to need to tune this value if your * virtual machine even supports it.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -