概念
- 是操作系統能夠進行運算調度的最小單位。
- 是進程中的實際運作單位。
- 是系統分配處理器時間資源的基本單元,即程序執行的最小單位
順便介紹下進程
- 進程是在系統中正在運行的一個應用程序;程序一旦運行就是進程;
- 進程是資源分配的最小單位
- 進程可以包含多個線程
線程狀態
- java線程狀態定義在java.lang.Thread.State枚舉類中;分別是
- NEW(初始狀態)
- RUNNABLE (就緒、執行中狀態)
- BLOCKED(阻塞狀態)
- WAITING(等待狀態)
- TIMED_WAITING (超時等待狀態)
- TERMINATED(終止狀態)
- 源碼如下:
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;
}
- 狀態詳解
NEW(初始狀態)
定義
線程剛創建,但是沒有調用start方法時候的狀態
實例
ThreadDemo threadDemo = new ThreadDemo ();
System.out.println(threadDemo .getState());//NEW
RUNNABLE (就緒、執行中狀態)
定義
調用start()方法之後.(線程狀態爲 RUNNABLE 就緒、執行中狀態)
1.此狀態下線程可能真的在運行RUNNING(執行中), 也有可能是在等待CPU資源(READY就緒)
2.RUNNING(執行中)和(READY就緒)只是方便理解添加的兩個狀態,實際上線程對象沒有這兩個狀態
實例
ThreadDemo threadDemo = new ThreadDemo ();
threadDemo .start();
System.out.println(threadDemo .getState());//RUNNABLE
BLOCKED(阻塞狀態)
定義
等待獲取一個監視器鎖以便進入synchronized塊或者方法時候的狀態。
實例
- 線程類MyThread
package com.example.thread;
import com.example.util.DateUtil;
import java.util.Date;
public class MyThread extends Thread {
private Object lock;
public MyThread(Object lock) {
this.lock = lock;
}
@Override
public void run() {
synchronized (lock) {
System.out.println(Thread.currentThread().getName() + " entry synchronized " + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
try {
Thread.sleep(10000);//休眠10s
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " done " + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
}
}
}
- 測試類
package com.example.springdemo01.thread;
public class ThreadDemo02 {
public static void main(String[] args) throws InterruptedException {
Object lock = new Object();
MyThread thread1 = new MyThread(lock);
thread1.start();
MyThread thread2 = new MyThread(lock);
thread2.start();
Thread.sleep(1000);//等1s再檢查狀態,以便線程thread2完全啓動成功,進入獲取lock對象的鎖階段
System.out.println(thread2.getName() + " 線程狀態 " + thread2.getState());
}
}
- 控制檯打印
Thread-0 entry synchronized 2020-06-14 18:02:12
Thread-1 線程狀態 BLOCKED
Thread-0 done 2020-06-14 18:02:22
Thread-1 entry synchronized 2020-06-14 18:02:22
Thread-1 done 2020-06-14 18:02:32
Process finished with exit code 0
上面實例用了synchronized塊,