thread.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,528 行 · 第 1/4 页
JAVA
1,528 行
/** * 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 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) */ public Thread(ThreadGroup group, Runnable target) { init(group, target, "Thread-" + nextThreadNum(), 0); } /** * 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) { init(null, null, name, 0); } /** * 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) */ public Thread(ThreadGroup group, String name) { init(group, null, name, 0); } /* * Private methods and constructor for attaching to existing thread, * used by JNI AttachCurrentThread, etc. */ private static ThreadGroup systemThreadGroup; private static ThreadGroup mainThreadGroup; private Thread(ThreadGroup group, String name, int priority, long eetop) { this.eetop = eetop; this.priority = priority; init(group, null, name, 0); } /* JNI_CreateJavaVM() invokes this */ private static void initMainThread(int priority, long eetop) { systemThreadGroup = new ThreadGroup(); mainThreadGroup = new ThreadGroup(systemThreadGroup, "main"); Thread mainThread = new Thread(mainThreadGroup, "main", priority, eetop); /* The main thread will be alive (alive==true) by default. * Thus need to perform operations normally done in Thread.start(). */ mainThreadGroup.add(mainThread); ThreadRegistry.add(mainThread); mainThread.alive = true; } /* CVMjniAttachCurrentThread() invokes this */ private static Thread initAttachedThread(ThreadGroup group, String name, int priority, long eetop, boolean isDaemon) { if (group == null) { group = mainThreadGroup; } if (name == null) { name = "Thread-" + nextThreadNum(); } Thread attachedThread = new Thread(group, name, priority, eetop); attachedThread.daemon = isDaemon; ThreadRegistry.add(attachedThread); return attachedThread; } /* CVMjvmpi_CreateSystemThread() invokes this */ private static Thread initDaemonThread(String name, int priority) { Thread daemonThread = new Thread(systemThreadGroup, name); daemonThread.priority = priority; daemonThread.daemon = true; ThreadRegistry.add(daemonThread); return daemonThread; } // %begin lvm /* CVMLVMjobjectInitSystemClasses() invokes this to initialize * the newly created LVM context. */ private void reinitLVMStartupThread() { systemThreadGroup = new ThreadGroup(); mainThreadGroup = new ThreadGroup(systemThreadGroup, "main"); if (group != null) { group.remove(this); } group = systemThreadGroup; group.add(this); contextClassLoader = null; inheritedAccessControlContext = null; //threadLocals = Collections.EMPTY_MAP; threadLocals = null; //inheritableThreadLocals = Collections.EMPTY_MAP; inheritableThreadLocals = null; } // %end lvm /** * 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) */ public Thread(Runnable target, String name) { init(null, target, name, 0); } /** * 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 <code>null</code> and there is a * security manager, the group is determined by the security manager's * <code>getThreadGroup</code> method. If <code>group</code> is * <code>null</code> and there is not a security manager, or the * security manager's <code>getThreadGroup</code> method returns * <code>null</code>, the group is set to be the same ThreadGroup * as the thread that is creating the new thread. * * <p>If there is a security manager, its <code>checkAccess</code> * method is called with the ThreadGroup as its argument. * This may result in a SecurityException. * <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() * @see SecurityManager#checkAccess */ public Thread(ThreadGroup group, Runnable target, String name) { init(group, target, name, 0); } /** * 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, belongs to the thread group referred to * by <code>group</code>, and has the specified <i>stack size</i>. * * <p>This constructor is identical to {@link * #Thread(ThreadGroup,Runnable,String)} with the exception of the fact * that it allows the thread stack size to be specified. The stack size * is the approximate number of bytes of address space that the virtual * machine is to allocate for this thread's stack. <b>The effect of the * <tt>stackSize</tt> parameter, if any, is highly platform dependent.</b> * * <p>On some platforms, specifying a higher value for the * <tt>stackSize</tt> parameter may allow a thread to achieve greater * recursion depth before throwing a {@link StackOverflowError}. * Similarly, specifying a lower value may allow a greater number of * threads to exist concurrently without throwing an an {@link * OutOfMemoryError} (or other internal error). The details of * the relationship between the value of the <tt>stackSize</tt> parameter * and the maximum recursion depth and concurrency level are * platform-dependent. <b>On some platforms, the value of the * <tt>stackSize</tt> parameter may have no effect whatsoever.</b> * * <p>The virtual machine is free to treat the <tt>stackSize</tt> * parameter as a suggestion. If the specified value is unreasonably low * for the platform, the virtual machine may instead use some * platform-specific minimum value; if the specified value is unreasonably * high, the virtual machine may instead use some platform-specific * maximum. Likewise, the virtual machine is free to round the specified * value up or down as it sees fit (or to ignore it completely). * * <p>Specifying a value of zero for the <tt>stackSize</tt> parameter will * cause this constructor to behave exactly like the * <tt>Thread(ThreadGroup, Runnable, String)</tt> constructor. * * <p><i>Due to the platform-dependent nature of the behavior of this * constructor, extreme care should be exercised in its use. * The thread stack size necessary to perform a given computation will * likely vary from one JRE implementation to another. In light of this * variation, careful tuning of the stack size parameter may be required, * and the tuning may need to be repeated for each JRE implementation on * which an application is to run.</i> * * <p>Implementation note: Java platform implementers are encouraged to * document their implementation's behavior with respect to the * <tt>stackSize parameter</tt>. * * @param group the thread group. * @param target the object whose <code>run</code> method is called. * @param name the name of the new thread. * @param stackSize the desired stack size for the new thread, or * zero to indicate that this parameter is to be ignored. * @exception SecurityException if the current thread cannot create a * thread in the specified thread group. */ public Thread(ThreadGroup group, Runnable target, String name, long stackSize) { init(group, target, name, stackSize); } /** * 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() */ public void start() { synchronized (lock) { if (alive || hasStartedOnce) { throw new IllegalThreadStateException(); } if (!stillborn) { CVM.disableRemoteExceptions(); // Just in case we encounter hidden calls // to wait() boolean unmask = !CVM.maskInterrupts(); try { hasStartedOnce = true; group.add(this); // access to ThreadRegistry is synced in exit(). ThreadRegistry.add(this); // NOTE: start0() will allow the native thread to run. // There's a chance that the thread will finish running // and its exit() method called before start0() returns. // Hence, we should only call start0() after the above // initialization code is done so that there is no race // condition between exit() and this method in setting // these values. // Lock our own critical section so that we do not // need to deal with interrupt() calls in the native // code that waits for the child to start. // This is redundant now that we call // CVM.maskInterrupts() above. synchronized (currentThread().lock) { start0(priority); } alive = true; } finally { if (!alive) { try { ThreadRegistry.remove(this); } catch (Throwable t) { } try { group.remove(this); } catch (Throwable t) { } } if (unmask) { CVM.unmaskInterrupts(); } CVM.enableRemoteExceptions(); } } } } private native void start0(int priority); /** * 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#Thread(java.lang.ThreadGroup, * java.lang.Runnable, java.lang.String) * @see java.lang.Runnable#run() */ 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(Throwable t) { // disable any future async/remote exceptions CVM.disableRemoteExceptions(); CVM.maskInterrupts(); stillborn = true; if (group != null) { if (t != null) { try { group.uncaughtException(this, t); } catch (Throwable t0) { // Probably OutOfMemoryError, ignore } } // unlocked reserved resources to prevent // out-of-memory errors setThreadExiting(); try { group.remove(this); } catch (Throwable t0) { // Probably OutOfMemoryError, ignore } group = null; } else { // unlocked reserved resources to prevent // out-of-memory errors setThreadExiting(); } /* Aggressively null object connected to Thread: see bug 4006245 */ target = null; synchronized (lock) { ThreadRegistry.remove(this); eetop = 0; alive = false; lock.notifyAll();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?