線程的知識點真的很多,想要全面掌握昂拂,需要日積月累,今天就請大家跟隨我枕面,一起走進(jìn)Thread的世界愿卒。
想要走進(jìn)Thread,那么就從源碼開始吧缚去。
Thead State 線程的狀態(tài)
這個知識點潮秘,是你必須知道的,線程那些狀態(tài)呢易结?
public enum State {
/**
* Thread state for a thread which has not yet started.
*/
NEW,
/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,
/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called <tt>Object.wait()</tt>
* on an object is waiting for another thread to call
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
* that object. A thread that has called <tt>Thread.join()</tt>
* is waiting for a specified thread to terminate.
*/
WAITING,
/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
TIMED_WAITING,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}
上面的枚舉類是Thread內(nèi)部枚舉枕荞,里面包含了線程的幾種狀態(tài),那么我們來看看搞动,都是什么狀態(tài)躏精。
NEW
Thread state for a thread which has not yet started
public class ThreadTest {
public static void main(String[] args){
Thread thread = new Thread();
Thread.State state = thread.getState();
System.out.println(state.toString());
}
}
當(dāng)運行以上代碼的時候,打印結(jié)果是 NEW鹦肿,這樣我們可以理解矗烛,線程創(chuàng)建以后,在沒有調(diào)用start方法以前箩溃,Thread的狀態(tài)是NEW.
RUNNABLE
Thread state for a runnable thread. A thread in the runnable state is executing in the Java virtual machine but it may be waiting for other resources from the operating system such as processor.
public class ThreadTest {
public static void main(String[] args){
Thread thread = new Thread();
thread.start();
Thread.State state = thread.getState();
System.out.println(state.toString());
}
}
當(dāng)運行以上代碼的時候瞭吃,打印的結(jié)果是RUNNABLE,當(dāng)前線程從NEW狀態(tài)轉(zhuǎn)到了RUNNABLE狀態(tài)涣旨。
在網(wǎng)上看到好多人在寫Thread狀態(tài)的時候歪架,還有運行中狀態(tài),這是不正確的霹陡,如何用代碼證明呢和蚪?
public class ThreadTest {
public static void main(String[] args){
Thread thread = new Thread(new MyThread());
thread.start();
Thread.State state = thread.getState();
System.out.println(state.toString());
}
static class MyThread implements Runnable{
public void run() {
int count = 0;
for (int i=0;i<10000;i++){
count = count+i;
System.out.println(Thread.currentThread().getState().toString());
}
}
}
}
運行以上代碼,你會發(fā)現(xiàn)烹棉,線程根本沒有所謂的運行中的狀態(tài)攒霹,CPU已經(jīng)計算結(jié)果了
BLOCKED
Thread state for a thread blocked waiting for a monitor lock.
A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method after calling {@link Object#wait() Object.wait}
阻塞狀態(tài),當(dāng)前線程在等待一個monitor lock。
1.調(diào)用synchronized浆洗,使線程進(jìn)入阻塞狀態(tài)催束。
2.調(diào)用wait方法,使線程進(jìn)入阻塞狀態(tài)辅髓。
WAITING
使線程進(jìn)入WAITING狀態(tài)的幾種場景:
1.調(diào)用了wait方法泣崩,沒有設(shè)置過期時間
2.調(diào)用Thread.join方法,沒有設(shè)置過期時間
3.調(diào)用了LockSupport#park
TIMED_WAITING
使線程進(jìn)入TIMED_WAITING狀態(tài)的幾種場景:
1.調(diào)用了Thread.sleep方法
2.調(diào)用了Object.wati方法洛口,設(shè)置了過期時間
3.調(diào)用了Thread.join方法矫付,設(shè)置了過期時間
4.調(diào)用了LockSupport#parkNanos
5.調(diào)用了LockSupport#parkUntil
TERMINATED
線程已經(jīng)執(zhí)行完成,就進(jìn)入了線程終止?fàn)顟B(tài)第焰。
線程的優(yōu)先級
當(dāng)多個線程的優(yōu)先級相同的時候买优,它們使搶奪CPU資源。如果,你想讓某個線程有限執(zhí)行杀赢,你就可以設(shè)置它的優(yōu)先級烘跺,可以通過Thread.setPriority設(shè)置線程優(yōu)先級,優(yōu)先級一共有10個級別脂崔,線程默認(rèn)的優(yōu)先級使5
/**
* 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);
}
}
守護(hù)線程