在线JDK地址:
中文: http://tool.oschina.net/apidocs/apidoc?api=jdk-zh
英文: http://tool.oschina.net/apidocs/apidoc?api=jdk_7u4
在线源码地址: java.lang.Runnable java.lang.Thread
java.lang.Runnable
Runnable 是一个接口interface
只有一个抽象方法 public abstract void run();
public interface Runnable {
public abstract void run(); }
java.lang.Thread
Thread类继承Runnable 接口,覆写run方法
public class Thread implements Runnable { public void run() { if (target != null) { target.run(); } } }
Thread类构造方法如下:
private char name[]; private int priority; /* Whether or not the thread is a daemon thread. */
private boolean daemon = false; /* * Thread ID */
private long tid; /* For generating thread ID */
private static long threadSeqNumber; /* What will be run. */
private Runnable target; /* The group of this thread */
private ThreadGroup group; /* * The requested stack size for this thread, or 0 if the creator did * not specify a stack size. It is up to the VM to do whatever it * likes with this number; some VMs will ignore it. */
private long stackSize; public Thread() { init(null, null, "Thread-" + nextThreadNum(), 0); } public Thread(Runnable target) { init(null, target, "Thread-" + nextThreadNum(), 0); } public Thread(ThreadGroup group, Runnable target) { init(group, target, "Thread-" + nextThreadNum(), 0); } public Thread(String name) { init(null, null, name, 0); } public Thread(ThreadGroup group, String name) { init(group, null, name, 0); } public Thread(Runnable target, String name) { init(null, target, name, 0); } public Thread(ThreadGroup group, Runnable target, String name) { init(group, target, name, 0); } public Thread(ThreadGroup group, Runnable target, String name,long stackSize) { init(group, target, name, stackSize); } /** * Initializes a Thread. * * @param g the Thread group * @param target the object whose run() method gets called * @param name the name of the new Thread * @param stackSize the desired stack size for the new thread, or * zero to indicate that this parameter is to be ignored. */
private void init(ThreadGroup g, Runnable target, String name, long stackSize) { Thread parent = currentThread(); SecurityManager security = System.getSecurityManager(); if (g == null) { /* Determine if it's an applet or not */
/* If there is a security manager, ask the security manager what to do. */
if (security != null) { g = security.getThreadGroup(); } /* If the security doesn't have a strong opinion of the matter use the parent thread group. */
if (g == null) { g = parent.getThreadGroup(); } } /* checkAccess regardless of whether or not threadgroup is explicitly passed in. */ g.checkAccess(); /* * Do we have the required permissions? */
if (security != null) { if (isCCLOverridden(getClass())) { security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION); } } g.addUnstarted(); this.group = g; this.daemon = parent.isDaemon(); this.priority = parent.getPriority(); this.name = name.toCharArray(); if (security == null || isCCLOverridden(parent.getClass())) this.contextClassLoader = parent.getContextClassLoader(); else
this.contextClassLoader = parent.contextClassLoader; this.inheritedAccessControlContext = AccessController.getContext(); this.target = target; setPriority(priority); if (parent.inheritableThreadLocals != null) this.inheritableThreadLocals =ThreadLocal.createInheritedMap(parent.inheritableThreadLocals); /* Stash the specified stack size in case the VM cares */
this.stackSize = stackSize; /* Set thread ID */ tid = nextThreadID(); this.me = this; } private static synchronized long nextThreadID() { return ++threadSeqNumber; }
Thread类优先级设置如下:
/** * The minimum priority that a thread can have. */ public final static int MIN_PRIORITY = 1; /** * The default priority that is assigned to a thread. */ public final static int NORM_PRIORITY = 5; /** * The maximum priority that a thread can have. */ public final static int MAX_PRIORITY = 10;
public final void setPriority(int newPriority) { ThreadGroup g; checkAccess(); if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) { throw new IllegalArgumentException(); } if((g = getThreadGroup()) != null) { if (newPriority > g.getMaxPriority()) { newPriority = g.getMaxPriority(); } setPriority0(priority = newPriority); } }
Thread类start方法如下:
public synchronized void start() { /** * This method is not invoked for the main method thread or "system" * group threads created/set up by the VM. Any new functionality added * to this method in the future may have to also be added to the VM. * * A zero status value corresponds to state "NEW". */ if (threadStatus != 0 || this != me) throw new IllegalThreadStateException(); group.add(this); start0(); if (stopBeforeStart) { stop0(throwableFromStop); } } private native void start0();
其他方法:
public static native Thread currentThread(); public static native void yield(); public static native void sleep(long millis) throws InterruptedException; public String toString() { ThreadGroup group = getThreadGroup(); if (group != null) { return "Thread[" + getName() + "," + getPriority() + "," + group.getName() + "]"; } else { return "Thread[" + getName() + "," + getPriority() + "," + "" + "]"; } } /** * Returns a clone if the class of this object is {@link Cloneable Cloneable}. * * @return a clone if the class of this object is {@code Cloneable} * * @throws CloneNotSupportedException * if this method is invoked on a class that does not * support {@code Cloneable} */ @Override protected Object clone() throws CloneNotSupportedException { Thread t; synchronized(this) { t = (Thread) super.clone(); t.tid = nextThreadID(); t.parkBlocker = null; t.blocker = null; t.blockerLock = new Object(); t.threadLocals = null; group.checkAccess(); if (threadStatus == 0) { group.addUnstarted(); } t.setPriority(priority); final Thread current = Thread.currentThread(); if (current.inheritableThreadLocals != null) t.inheritableThreadLocals = ThreadLocal.createInheritedMap(current.inheritableThreadLocals); } t.me = t; return t; } public final native boolean isAlive(); public void interrupt() { if (this != Thread.currentThread()) checkAccess(); synchronized (blockerLock) { Interruptible b = blocker; if (b != null) { interrupt0(); // Just to set the interrupt flag b.interrupt(); return; } } interrupt0(); } public final synchronized void join(long millis) throws InterruptedException { long base = System.currentTimeMillis(); long now = 0; if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (millis == 0) { while (isAlive()) { wait(0); } } else { while (isAlive()) { long delay = millis - now; if (delay <= 0) { break; } wait(delay); now = System.currentTimeMillis() - base; } } } public final void join() throws InterruptedException { join(0); } public final void setDaemon(boolean on) { checkAccess(); if (isAlive()) { throw new IllegalThreadStateException(); } daemon = on; } public final boolean isDaemon() { return daemon; } /* Some private helper methods */ private native void setPriority0(int newPriority); private native void stop0(Object o); private native void suspend0(); private native void resume0(); private native void interrupt0();