thread.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,528 行 · 第 1/4 页
JAVA
1,528 行
setPriority0(priority = newPriority); } /** * Returns this thread's priority. * * @return this thread's priority. * @see #setPriority * @see java.lang.Thread#setPriority(int) */ public final int getPriority() { return priority; } /** * Changes the name of this thread to be equal to the argument * <code>name</code>. * <p> * First the <code>checkAccess</code> method of this thread is called * with no arguments. This may result in throwing a * <code>SecurityException</code>. * * @param name the new name for this thread. * @exception SecurityException if the current thread cannot modify this * thread. * @see #getName * @see java.lang.Thread#checkAccess() * @see java.lang.Thread#getName() */ public final void setName(String name) { checkAccess(); this.name = name.toCharArray(); } /** * Returns this thread's name. * * @return this thread's name. * @see #setName * @see java.lang.Thread#setName(java.lang.String) */ public final String getName() { return String.valueOf(name); } /** * Returns the thread group to which this thread belongs. * This method returns null if this thread has died * (been stopped). * * @return this thread's thread group. */ public final ThreadGroup getThreadGroup() { return group; } /** * Returns the number of active threads in the current thread's thread * group. * * @return the number of active threads in the current thread's thread * group. */ public static int activeCount() { return currentThread().getThreadGroup().activeCount(); } /** * Copies into the specified array every active thread in * the current thread's thread group and its subgroups. This method simply * calls the <code>enumerate</code> method of the current thread's thread * group with the array argument. * <p> * First, if there is a security manager, that <code>enumerate</code> * method calls the security * manager's <code>checkAccess</code> method * with the thread group as its argument. This may result * in throwing a <code>SecurityException</code>. * * @param tarray an array of Thread objects to copy to * @return the number of threads put into the array * @exception SecurityException if a security manager exists and its * <code>checkAccess</code> method doesn't allow the operation. * @see java.lang.ThreadGroup#enumerate(java.lang.Thread[]) * @see java.lang.SecurityManager#checkAccess(java.lang.ThreadGroup) */ public static int enumerate(Thread tarray[]) { return currentThread().getThreadGroup().enumerate(tarray); } /** * Counts the number of stack frames in this thread. The thread must * be suspended. * * return the number of stack frames in this thread. * exception IllegalThreadStateException if this thread is not * suspended. * deprecated The definition of this call depends on {@link #suspend}, * which is deprecated. Further, the results of this call * were never well-defined. * public native int countStackFrames(); */ /** * Waits at most <code>millis</code> milliseconds for this thread to * die. A timeout of <code>0</code> means to wait forever. * * @param millis the time to wait in milliseconds. * @exception InterruptedException if another thread has interrupted * the current thread. The <i>interrupted status</i> of the * current thread is cleared when this exception is thrown. */ public final void join(long millis) throws InterruptedException { synchronized (lock) { long base = System.currentTimeMillis(); long now = 0; if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (millis == 0) { while (isAlive()) { lock.wait(0); } } else { while (isAlive()) { long delay = millis - now; if (delay <= 0) { break; } lock.wait(delay); now = System.currentTimeMillis() - base; } } } } /** * Waits at most <code>millis</code> milliseconds plus * <code>nanos</code> nanoseconds for this thread to die. * * @param millis the time to wait in milliseconds. * @param nanos 0-999999 additional nanoseconds to wait. * @exception IllegalArgumentException if the value of millis is negative * the value of nanos is not in the range 0-999999. * @exception InterruptedException if another thread has interrupted * the current thread. The <i>interrupted status</i> of the * current thread is cleared when this exception is thrown. */ public final void join(long millis, int nanos) throws InterruptedException { if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (nanos < 0 || nanos > 999999) { throw new IllegalArgumentException( "nanosecond timeout value out of range"); } if (nanos >= 500000 || (nanos != 0 && millis == 0)) { millis++; } join(millis); } /** * Waits for this thread to die. * * @exception InterruptedException if another thread has interrupted * the current thread. The <i>interrupted status</i> of the * current thread is cleared when this exception is thrown. */ public final void join() throws InterruptedException { join(0); } /** * Prints a stack trace of the current thread. This method is used * only for debugging. * * @see java.lang.Throwable#printStackTrace() */ public static void dumpStack() { new Exception("Stack trace").printStackTrace(); } /** * Marks this thread as either a daemon thread or a user thread. The * Java Virtual Machine exits when the only threads running are all * daemon threads. * <p> * This method must be called before the thread is started. * <p> * This method first calls the <code>checkAccess</code> method * of this thread * with no arguments. This may result in throwing a * <code>SecurityException </code>(in the current thread). * <p> * If process model exists, Thread.setDaemon() should be used * as it is defined in J2SE specification. * <p> * If process model does not exist, Thread.setDaemon() should be * used to set threads as daemon threads if the programmer uses * good coding techniques to a) guarantee that the daemon thread will * exit and clean up its resources before either a System.exit() is * called and/or all the user threads have exited, and b) override the * security manager's default behavior to throw SecurityException * when System.exit() is called. * <p> * @param on if <code>true</code>, marks this thread as a * daemon thread. * @exception IllegalThreadStateException if this thread is active. * @exception SecurityException if the current thread cannot modify * this thread. * @see java.lang.Thread#isDaemon() * @see #checkAccess */ public final void setDaemon(boolean on) { checkAccess(); synchronized (lock) { if (alive) { throw new IllegalThreadStateException(); } else { daemon = on; } } } /** * Tests if this thread is a daemon thread. * * @return <code>true</code> if this thread is a daemon thread; * <code>false</code> otherwise. * @see java.lang.Thread#setDaemon(boolean) */ public final boolean isDaemon() { return daemon; } /** * Determines if the currently running thread has permission to * modify this thread. * <p> * If there is a security manager, its <code>checkAccess</code> method * is called with this thread as its argument. This may result in * throwing a <code>SecurityException</code>. * <p> * Note: This method was mistakenly non-final in JDK 1.1. * It has been made final in the Java 2 Platform. * * @exception SecurityException if the current thread is not allowed to * access this thread. * @see java.lang.SecurityManager#checkAccess(java.lang.Thread) */ public final void checkAccess() { SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkAccess(this); } } /** * Returns a string representation of this thread, including the * thread's name, priority, and thread group. * * @return a string representation of this thread. */ public String toString() { ThreadGroup group = getThreadGroup(); if (group != null) { return "Thread[" + getName() + "," + getPriority() + "," + group.getName() + "]"; } else { return "Thread[" + getName() + "," + getPriority() + "," + "" + "]"; } } /** * Returns the context ClassLoader for this Thread. The context * ClassLoader is provided by the creator of the thread for use * by code running in this thread when loading classes and resources. * If not set, the default is the ClassLoader context of the parent * Thread. The context ClassLoader of the primordial thread is * typically set to the class loader used to load the application. * * <p>First, if there is a security manager, and the caller's class * loader is not null and the caller's class loader is not the same as or * an ancestor of the context class loader for the thread whose * context class loader is being requested, then the security manager's * <code>checkPermission</code> * method is called with a * <code>RuntimePermission("getClassLoader")</code> permission * to see if it's ok to get the context ClassLoader.. * * @return the context ClassLoader for this Thread * * @throws SecurityException * if a security manager exists and its * <code>checkPermission</code> method doesn't allow * getting the context ClassLoader. * @see #setContextClassLoader * @see SecurityManager#checkPermission * @see java.lang.RuntimePermission * * @since 1.2 */ public ClassLoader getContextClassLoader() { if (contextClassLoader == null) return null; SecurityManager sm = System.getSecurityManager(); if (sm != null) { ClassLoader ccl = ClassLoader.getCallerClassLoader(); if (ccl != null && ccl != contextClassLoader && !contextClassLoader.isAncestor(ccl)) { sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); } } return contextClassLoader; } /** * Sets the context ClassLoader for this Thread. The context * ClassLoader can be set when a thread is created, and allows * the creator of the thread to provide the appropriate class loader * to code running in the thread when loading classes and resources. * * <p>First, if there is a security manager, its <code>checkPermission</code> * method is called with a * <code>RuntimePermission("setContextClassLoader")</code> permission * to see if it's ok to set the context ClassLoader.. * * @param cl the context ClassLoader for this Thread * * @exception SecurityException if the current thread cannot set the * context ClassLoader. * @see #getContextClassLoader * @see SecurityManager#checkPermission * @see java.lang.RuntimePermission * * @since 1.2 */ public void setContextClassLoader(ClassLoader cl) { SecurityManager sm = System.getSecurityManager(); if (sm != null) { sm.checkPermission(new RuntimePermission("setContextClassLoader")); } contextClassLoader = cl; } /** * Returns <tt>true</tt> if and only if the current thread holds the * monitor lock on the specified object. * * <p>This method is designed to allow a program to assert that * the current thread already holds a specified lock: * <pre> * assert Thread.holdsLock(obj); * </pre> * * @param obj the object on which to test lock ownership * @throws NullPointerException if obj is <tt>null</tt> * @return <tt>true</tt> if the current thread holds the monitor lock on * the specified object. * @since 1.4 */ public static native boolean holdsLock(Object obj); /* Some private helper methods */ private native void setPriority0(int newPriority); /* Only called by deprecated methods private native void suspend0(); private native void resume0(); */ private native void interrupt0();}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?