📄 thread.java
字号:
* * @param group the group to put the Thread into * @param target the Runnable object to execute * @param name the name for the Thread * @param size the stack size, in bytes; 0 to be ignored * @throws NullPointerException if name is null * @throws SecurityException if this thread cannot access <code>group</code> * @throws IllegalThreadStateException if group is destroyed * @since 1.4 */ public Thread(ThreadGroup group, Runnable target, String name, long size) { // Just ignore stackSize for now. this(currentThread(), group, target, name); } private Thread (Thread current, ThreadGroup g, Runnable r, String n) { // Make sure the current thread may create a new thread. checkAccess(); // The Class Libraries book says ``threadName cannot be null''. I // take this to mean NullPointerException. if (n == null) throw new NullPointerException (); if (g == null) { // If CURRENT is null, then we are bootstrapping the first thread. // Use ThreadGroup.root, the main threadgroup. if (current == null) group = ThreadGroup.root; else group = current.getThreadGroup(); } else group = g; data = null; interrupt_flag = false; alive_flag = false; startable_flag = true; if (current != null) { group.checkAccess(); daemon = current.isDaemon(); int gmax = group.getMaxPriority(); int pri = current.getPriority(); priority = (gmax < pri ? gmax : pri); contextClassLoader = current.contextClassLoader; InheritableThreadLocal.newChildThread(this); } else { daemon = false; priority = NORM_PRIORITY; } name = n; group.addThread(this); runnable = r; initialize_native (); } /** * Get the number of active threads in the current Thread's ThreadGroup. * This implementation calls * <code>currentThread().getThreadGroup().activeCount()</code>. * * @return the number of active threads in the current ThreadGroup * @see ThreadGroup#activeCount() */ public static int activeCount() { return currentThread().group.activeCount(); } /** * Check whether the current Thread is allowed to modify this Thread. This * passes the check on to <code>SecurityManager.checkAccess(this)</code>. * * @throws SecurityException if the current Thread cannot modify this Thread * @see SecurityManager#checkAccess(Thread) */ public final void checkAccess() { SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkAccess(this); } /** * Count the number of stack frames in this Thread. The Thread in question * must be suspended when this occurs. * * @return the number of stack frames in this Thread * @throws IllegalThreadStateException if this Thread is not suspended * @deprecated pointless, since suspend is deprecated */ public native int countStackFrames(); /** * Get the currently executing Thread. * * @return the currently executing Thread */ public static native Thread currentThread(); /** * Originally intended to destroy this thread, this method was never * implemented by Sun, and is hence a no-op. */ public void destroy() { throw new NoSuchMethodError(); } /** * Print a stack trace of the current thread to stderr using the same * format as Throwable's printStackTrace() method. * * @see Throwable#printStackTrace() */ public static void dumpStack() { (new Exception("Stack trace")).printStackTrace(); } /** * Copy every active thread in the current Thread's ThreadGroup into the * array. Extra threads are silently ignored. This implementation calls * <code>getThreadGroup().enumerate(array)</code>, which may have a * security check, <code>checkAccess(group)</code>. * * @param array the array to place the Threads into * @return the number of Threads placed into the array * @throws NullPointerException if array is null * @throws SecurityException if you cannot access the ThreadGroup * @see ThreadGroup#enumerate(Thread[]) * @see #activeCount() * @see SecurityManager#checkAccess(ThreadGroup) */ public static int enumerate(Thread[] array) { return currentThread().group.enumerate(array); } /** * Get this Thread's name. * * @return this Thread's name */ public final String getName() { return name; } /** * Get this Thread's priority. * * @return the Thread's priority */ public final int getPriority() { return priority; } /** * Get the ThreadGroup this Thread belongs to. If the thread has died, this * returns null. * * @return this Thread's ThreadGroup */ public final ThreadGroup getThreadGroup() { return group; } /** * Checks whether the current thread holds the monitor on a given object. * This allows you to do <code>assert Thread.holdsLock(obj)</code>. * * @param obj the object to test lock ownership on. * @return true if the current thread is currently synchronized on obj * @throws NullPointerException if obj is null * @since 1.4 */ public static native boolean holdsLock(Object obj); /** * Interrupt this Thread. First, there is a security check, * <code>checkAccess</code>. Then, depending on the current state of the * thread, various actions take place: * * <p>If the thread is waiting because of {@link #wait()}, * {@link #sleep(long)}, or {@link #join()}, its <i>interrupt status</i> * will be cleared, and an InterruptedException will be thrown. Notice that * this case is only possible if an external thread called interrupt(). * * <p>If the thread is blocked in an interruptible I/O operation, in * {@link java.nio.channels.InterruptibleChannel}, the <i>interrupt * status</i> will be set, and ClosedByInterruptException will be thrown. * * <p>If the thread is blocked on a {@link java.nio.channels.Selector}, the * <i>interrupt status</i> will be set, and the selection will return, with * a possible non-zero value, as though by the wakeup() method. * * <p>Otherwise, the interrupt status will be set. * * @throws SecurityException if you cannot modify this Thread */ public native void interrupt(); /** * Determine whether the current Thread has been interrupted, and clear * the <i>interrupted status</i> in the process. * * @return whether the current Thread has been interrupted * @see #isInterrupted() */ public static boolean interrupted() { return currentThread().isInterrupted(true); } /** * Determine whether the given Thread has been interrupted, but leave * the <i>interrupted status</i> alone in the process. * * @return whether the Thread has been interrupted * @see #interrupted() */ public boolean isInterrupted() { return interrupt_flag; } /** * Determine whether this Thread is alive. A thread which is alive has * started and not yet died. * * @return whether this Thread is alive */ public final synchronized boolean isAlive() { return alive_flag; } /** * Tell whether this is a daemon Thread or not. * * @return whether this is a daemon Thread or not * @see #setDaemon(boolean) */ public final boolean isDaemon() { return daemon; } /** * Wait forever for the Thread in question to die. * * @throws InterruptedException if the Thread is interrupted; it's * <i>interrupted status</i> will be cleared */ public final void join() throws InterruptedException { join(0, 0); } /** * Wait the specified amount of time for the Thread in question to die. * * @param ms the number of milliseconds to wait, or 0 for forever * @throws InterruptedException if the Thread is interrupted; it's * <i>interrupted status</i> will be cleared */ public final void join(long ms) throws InterruptedException { join(ms, 0); } /** * Wait the specified amount of time for the Thread in question to die. * * <p>Note that 1,000,000 nanoseconds == 1 millisecond, but most VMs do * not offer that fine a grain of timing resolution. Besides, there is * no guarantee that this thread can start up immediately when time expires, * because some other thread may be active. So don't expect real-time * performance. * * @param ms the number of milliseconds to wait, or 0 for forever * @param ns the number of extra nanoseconds to sleep (0-999999) * @throws InterruptedException if the Thread is interrupted; it's * <i>interrupted status</i> will be cleared * @throws IllegalArgumentException if ns is invalid * @XXX A ThreadListener would be nice, to make this efficient. */ public final native void join(long ms, int ns) throws InterruptedException; /** * Resume a suspended thread.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -