📄 thread.java
字号:
} else if (sm != null) sm.checkAccess(group); this.group = group; // Use toString hack to detect null. this.name = name.toString(); this.runnable = target; this.stacksize = size; priority = current.priority; daemon = current.daemon; contextClassLoader = current.contextClassLoader; group.addThread(this); InheritableThreadLocal.newChildThread(this); } /** * Used by the VM to create thread objects for threads started outside * of Java. Note: caller is responsible for adding the thread to * a group and InheritableThreadLocal. * * @param vmThread the native thread * @param name the thread name or null to use the default naming scheme * @param priority current priority * @param daemon is the thread a background thread? */ Thread(VMThread vmThread, String name, int priority, boolean daemon) { this.vmThread = vmThread; this.runnable = null; if (name == null) name = "Thread-" + ++numAnonymousThreadsCreated; this.name = name; this.priority = priority; this.daemon = daemon; this.contextClassLoader = ClassLoader.getSystemClassLoader(); } /** * 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() { // Bypass System.getSecurityManager, for bootstrap efficiency. SecurityManager sm = SecurityManager.current; 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 int countStackFrames() { VMThread t = vmThread; if (t == null || group == null) throw new IllegalThreadStateException(); return t.countStackFrames(); } /** * Get the currently executing Thread. In the situation that the * currently running thread was created by native code and doesn't * have an associated Thread object yet, a new Thread object is * constructed and associated with the native thread. * * @return the currently executing Thread */ public static Thread currentThread() { return VMThread.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 Throwable().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() { VMThread t = vmThread; return t == null ? name : t.getName(); } /** * Get this Thread's priority. * * @return the Thread's priority */ public final synchronized int getPriority() { VMThread t = vmThread; return t == null ? priority : t.getPriority(); } /** * 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 boolean holdsLock(Object obj) { return VMThread.holdsLock(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 synchronized void interrupt() { checkAccess(); VMThread t = vmThread; if (t != null) t.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 VMThread.interrupted(); } /** * 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() { VMThread t = vmThread; return t != null && t.isInterrupted(); } /** * 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 boolean isAlive() { return vmThread != null && group != null; } /** * 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() { VMThread t = vmThread; return t == null ? daemon : t.isDaemon(); } /** * 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 */ public final void join(long ms, int ns) throws InterruptedException { if(ms < 0 || ns < 0 || ns > 999999) throw new IllegalArgumentException(); VMThread t = vmThread; if(t != null) t.join(ms, ns); } /** * Resume this Thread. If the thread is not suspended, this method does * nothing. To mirror suspend(), there may be a security check: * <code>checkAccess</code>. * * @throws SecurityException if you cannot resume the Thread * @see #checkAccess() * @see #suspend() * @deprecated pointless, since suspend is deprecated */ public final synchronized void resume() { checkAccess(); VMThread t = vmThread; if (t != null) t.resume(); } /** * The method of Thread that will be run if there is no Runnable object * associated with the Thread. Thread's implementation does nothing at all. * * @see #start() * @see #Thread(ThreadGroup, Runnable, String) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -