📄 thread.java
字号:
* * @throws SecurityException if you cannot resume the Thread * @see #checkAccess() * @see #suspend() * @deprecated pointless, since suspend is deprecated */ public final native void resume(); private final native void finish_(); /** * Determine whether the given Thread has been interrupted, but leave * the <i>interrupted status</i> alone in the process. * * @return whether the current Thread has been interrupted * @see #interrupted() */ private boolean isInterrupted(boolean clear_flag) { boolean r = interrupt_flag; if (clear_flag && r) { // Only clear the flag if we saw it as set. Otherwise this could // potentially cause us to miss an interrupt in a race condition, // because this method is not synchronized. interrupt_flag = false; } return r; } /** * 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) */ 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 void setDaemon(boolean daemon) { if (!startable_flag) 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() { if (contextClassLoader == null) contextClassLoader = ClassLoader.getSystemClassLoader(); SecurityManager sm = System.getSecurityManager(); // FIXME: we can't currently find the caller's class loader. ClassLoader callers = null; if (sm != null && callers != null) { // See if the caller's class loader is the same as or an // ancestor of this thread's class loader. while (callers != null && callers != contextClassLoader) { // FIXME: should use some internal version of getParent // that avoids security checks. callers = callers.getParent(); } if (callers != contextClassLoader) 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 = System.getSecurityManager(); 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 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(); this.name = name; } /** * Causes the currently executing thread object to temporarily pause * and allow other threads to execute. */ public static native void 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, or 0 for forever * @throws InterruptedException if the Thread is interrupted; it's * <i>interrupted status</i> will be cleared * @see #notify() * @see #wait(long) */ 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. 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 sleep, 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 * @see #notify() * @see #wait(long, int) */ public static native void sleep(long timeout, int nanos) throws InterruptedException; /** * 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 native void start(); /** * 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() { // Argument doesn't matter, because this is no longer // supported. stop(null); } /** * Cause this Thread to stop abnormally and throw the specified exception. * If you stop a Thread that has not yet started, it will stop immediately * when it is actually started. <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 native void stop(Throwable 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 native void 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 native void setPriority(int newPriority); /** * 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()) + "]"); } private final native void initialize_native(); private final native static String gen_name();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -