📄 thread.java
字号:
public void run() { if (runnable != null) runnable.run(); } /** * Set the daemon status of this Thread. If this is a daemon Thread, then * the VM may exit even if it is still running. This may only be called * before the Thread starts running. There may be a security check, * <code>checkAccess</code>. * * @param daemon whether this should be a daemon thread or not * @throws SecurityException if you cannot modify this Thread * @throws IllegalThreadStateException if the Thread is active * @see #isDaemon() * @see #checkAccess() */ public final synchronized void setDaemon(boolean daemon) { if (vmThread != null) throw new IllegalThreadStateException(); checkAccess(); this.daemon = daemon; } /** * Returns the context classloader of this Thread. The context * classloader can be used by code that want to load classes depending * on the current thread. Normally classes are loaded depending on * the classloader of the current class. There may be a security check * for <code>RuntimePermission("getClassLoader")</code> if the caller's * class loader is not null or an ancestor of this thread's context class * loader. * * @return the context class loader * @throws SecurityException when permission is denied * @see #setContextClassLoader(ClassLoader) * @since 1.2 */ public synchronized ClassLoader getContextClassLoader() { // Bypass System.getSecurityManager, for bootstrap efficiency. SecurityManager sm = SecurityManager.current; if (sm != null) // XXX Don't check this if the caller's class loader is an ancestor. sm.checkPermission(new RuntimePermission("getClassLoader")); return contextClassLoader; } /** * Sets the context classloader for this Thread. When not explicitly set, * the context classloader for a thread is the same as the context * classloader of the thread that created this thread. The first thread has * as context classloader the system classloader. There may be a security * check for <code>RuntimePermission("setContextClassLoader")</code>. * * @param classloader the new context class loader * @throws SecurityException when permission is denied * @see #getContextClassLoader() * @since 1.2 */ public synchronized void setContextClassLoader(ClassLoader classloader) { SecurityManager sm = SecurityManager.current; if (sm != null) sm.checkPermission(new RuntimePermission("setContextClassLoader")); this.contextClassLoader = classloader; } /** * Set this Thread's name. There may be a security check, * <code>checkAccess</code>. * * @param name the new name for this Thread * @throws NullPointerException if name is null * @throws SecurityException if you cannot modify this Thread */ public final synchronized void setName(String name) { checkAccess(); // The Class Libraries book says ``threadName cannot be null''. I // take this to mean NullPointerException. if (name == null) throw new NullPointerException(); VMThread t = vmThread; if (t != null) t.setName(name); else this.name = name; } /** * Yield to another thread. The Thread will not lose any locks it holds * during this time. There are no guarantees which thread will be * next to run, and it could even be this one, but most VMs will choose * the highest priority thread that has been waiting longest. */ public static void yield() { VMThread.yield(); } /** * Suspend the current Thread's execution for the specified amount of * time. The Thread will not lose any locks it has during this time. There * are no guarantees which thread will be next to run, but most VMs will * choose the highest priority thread that has been waiting longest. * * @param ms the number of milliseconds to sleep. * @throws InterruptedException if the Thread is (or was) interrupted; * it's <i>interrupted status</i> will be cleared * @throws IllegalArgumentException if ms is negative * @see #interrupt() */ public static void sleep(long ms) throws InterruptedException { sleep(ms, 0); } /** * Suspend the current Thread's execution for the specified amount of * time. The Thread will not lose any locks it has during this time. There * are no guarantees which thread will be next to run, but most VMs will * choose the highest priority thread that has been waiting longest. * <p> * Note that 1,000,000 nanoseconds == 1 millisecond, but most VMs * do not offer that fine a grain of timing resolution. When ms is * zero and ns is non-zero the Thread will sleep for at least one * milli second. 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 sleep * @param ns the number of extra nanoseconds to sleep (0-999999) * @throws InterruptedException if the Thread is (or was) interrupted; * it's <i>interrupted status</i> will be cleared * @throws IllegalArgumentException if ms or ns is negative * or ns is larger than 999999. * @see #interrupt() */ public static void sleep(long ms, int ns) throws InterruptedException { // Check parameters if (ms < 0 ) throw new IllegalArgumentException("Negative milliseconds: " + ms); if (ns < 0 || ns > 999999) throw new IllegalArgumentException("Nanoseconds ouf of range: " + ns); // Really sleep VMThread.sleep(ms, ns); } /** * Start this Thread, calling the run() method of the Runnable this Thread * was created with, or else the run() method of the Thread itself. This * is the only way to start a new thread; calling run by yourself will just * stay in the same thread. The virtual machine will remove the thread from * its thread group when the run() method completes. * * @throws IllegalThreadStateException if the thread has already started * @see #run() */ public synchronized void start() { if (vmThread != null || group == null) throw new IllegalThreadStateException(); VMThread.create(this, stacksize); } /** * Cause this Thread to stop abnormally because of the throw of a ThreadDeath * error. If you stop a Thread that has not yet started, it will stop * immediately when it is actually started. * * <p>This is inherently unsafe, as it can interrupt synchronized blocks and * leave data in bad states. Hence, there is a security check: * <code>checkAccess(this)</code>, plus another one if the current thread * is not this: <code>RuntimePermission("stopThread")</code>. If you must * catch a ThreadDeath, be sure to rethrow it after you have cleaned up. * ThreadDeath is the only exception which does not print a stack trace when * the thread dies. * * @throws SecurityException if you cannot stop the Thread * @see #interrupt() * @see #checkAccess() * @see #start() * @see ThreadDeath * @see ThreadGroup#uncaughtException(Thread, Throwable) * @see SecurityManager#checkAccess(Thread) * @see SecurityManager#checkPermission(Permission) * @deprecated unsafe operation, try not to use */ public final void stop() { stop(new ThreadDeath()); } /** * Cause this Thread to stop abnormally and throw the specified exception. * If you stop a Thread that has not yet started, the stop is ignored * (contrary to what the JDK documentation says). * <b>WARNING</b>This bypasses Java security, and can throw a checked * exception which the call stack is unprepared to handle. Do not abuse * this power. * * <p>This is inherently unsafe, as it can interrupt synchronized blocks and * leave data in bad states. Hence, there is a security check: * <code>checkAccess(this)</code>, plus another one if the current thread * is not this: <code>RuntimePermission("stopThread")</code>. If you must * catch a ThreadDeath, be sure to rethrow it after you have cleaned up. * ThreadDeath is the only exception which does not print a stack trace when * the thread dies. * * @param t the Throwable to throw when the Thread dies * @throws SecurityException if you cannot stop the Thread * @throws NullPointerException in the calling thread, if t is null * @see #interrupt() * @see #checkAccess() * @see #start() * @see ThreadDeath * @see ThreadGroup#uncaughtException(Thread, Throwable) * @see SecurityManager#checkAccess(Thread) * @see SecurityManager#checkPermission(Permission) * @deprecated unsafe operation, try not to use */ public final synchronized void stop(Throwable t) { if (t == null) throw new NullPointerException(); // Bypass System.getSecurityManager, for bootstrap efficiency. SecurityManager sm = SecurityManager.current; if (sm != null) { sm.checkAccess(this); if (this != currentThread()) sm.checkPermission(new RuntimePermission("stopThread")); } VMThread vt = vmThread; if (vt != null) vt.stop(t); else stillborn = t; } /** * Suspend this Thread. It will not come back, ever, unless it is resumed. * * <p>This is inherently unsafe, as the suspended thread still holds locks, * and can potentially deadlock your program. Hence, there is a security * check: <code>checkAccess</code>. * * @throws SecurityException if you cannot suspend the Thread * @see #checkAccess() * @see #resume() * @deprecated unsafe operation, try not to use */ public final synchronized void suspend() { checkAccess(); VMThread t = vmThread; if (t != null) t.suspend(); } /** * Set this Thread's priority. There may be a security check, * <code>checkAccess</code>, then the priority is set to the smaller of * priority and the ThreadGroup maximum priority. * * @param priority the new priority for this Thread * @throws IllegalArgumentException if priority exceeds MIN_PRIORITY or * MAX_PRIORITY * @throws SecurityException if you cannot modify this Thread * @see #getPriority() * @see #checkAccess() * @see ThreadGroup#getMaxPriority() * @see #MIN_PRIORITY * @see #MAX_PRIORITY */ public final synchronized void setPriority(int priority) { checkAccess(); if (priority < MIN_PRIORITY || priority > MAX_PRIORITY) throw new IllegalArgumentException("Invalid thread priority value " + priority + "."); priority = Math.min(priority, group.getMaxPriority()); VMThread t = vmThread; if (t != null) t.setPriority(priority); else this.priority = priority; } /** * Returns a string representation of this thread, including the * thread's name, priority, and thread group. * * @return a human-readable String representing this Thread */ public String toString() { return ("Thread[" + name + "," + priority + "," + (group == null ? "" : group.getName()) + "]"); } /** * Clean up code, called by VMThread when thread dies. */ synchronized void die() { group.removeThread(this); vmThread = null; locals = null; } /** * Returns the map used by ThreadLocal to store the thread local values. */ static Map getThreadLocals() { Thread thread = currentThread(); Map locals = thread.locals; if (locals == null) { locals = thread.locals = new WeakHashMap(); } return locals; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -