/**
* The minimum priority that a thread can have.
*/publicfinalstaticint MIN_PRIORITY = 1;
/**
* The default priority that is assigned to a thread.
*/publicfinalstaticint NORM_PRIORITY = 5;
/**
* The maximum priority that a thread can have.
*/publicfinalstaticint MAX_PRIORITY = 10;
线程最小优先级别:1
线程默认优先级别:5
线程最大优先级别:10
/**
* Returns a reference to the currently executing thread object.
*
* @return the currently executing thread.
*/publicstaticnative Thread currentThread();
当前正在执行的线程对象的引用
start()
/**
* Causes this thread to begin execution; the Java Virtual Machine
* calls the <code>run</code> method of this thread.
* <p>
* The result is that two threads are running concurrently: the
* current thread (which returns from the call to the
* <code>start</code> method) and the other thread (which executes its
* <code>run</code> method).
* <p>
* It is never legal to start a thread more than once.
* In particular, a thread may not be restarted once it has completed
* execution.
*
* @exception IllegalThreadStateException if the thread was already
* started.
* @see #run()
* @see #stop()
*/publicsynchronizedvoidstart() {
/**
* 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".
*/// threadStatus = 0 ,线程创建状态// 若同一个线程对象,已经执行过 start 方法,再次执行此 start 方法会抛出此异常if (threadStatus != 0)
thrownew IllegalThreadStateException();
/* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this); // 添加进线程组boolean started = false;
try {
start0(); // 调用 native 方法启动新的线程,由 JVM 执行 run 方法
started = true;
} finally {
try {
if (!started) {
// 线程启动失败,将线程从线程组中移除
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}
privatenativevoidstart0();
/**
* Waits at most {@code millis} milliseconds forthis thread to
* die. A timeout of {@code0} means to wait forever.
*
* <p> This implementation uses a loopof {@codethis.wait} calls
* conditioned on {@codethis.isAlive}. As a thread terminates the
* {@codethis.notifyAll} method is invoked. It is recommended that
* applications not use {@code wait}, {@code notify}, or
* {@code notifyAll} on {@code Thread} instances.
*
* @param millis
* the time to wait in milliseconds
*
* @throws IllegalArgumentException
* if the value of {@code millis} is negative
*
* @throws InterruptedException
* if any thread has interrupted the current thread. The
* <i>interrupted status</i> of the current thread is
* cleared whenthis exception is thrown.
*/
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
thrownew 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;
}
}
}