《java并发编程》那本书其实反复的看过很多遍了,也扒了很多多线程相关文章,但是不用的话很快就又忘记了 ,想到一个概念于是动手操作了一遍,以便加深认知。
- 先看一下Thread类的源码中线程状态的枚举说明,共有六种线程状态。
` /** * A thread state. A thread can be in one of the following states: * <ul>
代码语言:javascript复制 * <li>{[@link](https://my.oschina.net/u/393) #NEW}<br>
* A thread that has not yet started is in this state.
一个线程在创建对象后,以及调用start()之前,状态为new
* </li>
* <li>{[@link](https://my.oschina.net/u/393) #RUNNABLE}<br>
* A thread executing in the Java virtual machine is in this state.
线程在虚拟机中执行,进入RUNABLE状态(又细分为READY(就绪)-中间等待分配时间片段-以及RUNNING(运行中))
* </li>
* <li>{[@link](https://my.oschina.net/u/393) #BLOCKED}<br>
* A thread that is blocked waiting for a monitor lock
* is in this state.
一个线程在等待获取监视器锁的时候进入线程阻塞状态,即排队获取锁的状态。
* </li>
* <li>{[@link](https://my.oschina.net/u/393) #WAITING}<br>
* A thread that is waiting indefinitely for another thread to
* perform a particular action is in this state.
等待其它线程唤醒时处于等待状态。
* </li>
* <li>{[@link](https://my.oschina.net/u/393) #TIMED_WAITING}<br>
* A thread that is waiting for another thread to perform an action
* for up to a specified waiting time is in this state.
在一定时间里等待其它线程唤醒,超过时间唤醒线程重新获取锁。
* </li>
* <li>{@link #TERMINATED}<br>
* A thread that has exited is in this state.
一个线程的结束状态。
* </li>
* </ul>
*
* <p>
* A thread can be in only one state at a given point in time.
* These states are virtual machine states which do not reflect
* any operating system thread states.
*
* @since 1.5
* @see #getState
*/
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()之前,线程状态处于NEW),调用start()之后进入RUNABLE,线程执行结束进入BLOCKD状态
`package com.example.demo.thread;
/**
- 线程状态转换演示 NEW RUNABLE */ public class ThreadNewRunable extends Thread { private byte[] lock = new byte[0]; public ThreadNewRunable(byte[] lock) { this.lock = lock; } @Override public void run() { // doNothing
// synchronized (lock) { // try { // Thread.sleep(1000);// 休眠十秒,防止过快结束 // } catch (InterruptedException e) { // e.printStackTrace(); // } // } }
代码语言:javascript复制public static void main(String[] args) throws InterruptedException {
byte[] lock = new byte[0];
ThreadNewRunable threadNewRunable = new ThreadNewRunable(lock);
System.out.println("threadStatu-new :" threadNewRunable.getState());
threadNewRunable.start();
System.out.println("threadStatus-start:" threadNewRunable.getState());
ThreadNewRunable threadNewRunable1 = new ThreadNewRunable(lock);
threadNewRunable1.start();
Thread.sleep(1000);
System.out.println("threadStatu1-blocked : " threadNewRunable1.getState());
}
}
`
- 输出结果
threadStatu-new :NEW threadStatus-start:RUNNABLE threadStatu1-blocked : TERMINATED
- BLOCKED 在线程等待获取锁,synchronized块或者synchronized方法时,线程进入阻塞状态
`package com.example.demo.thread;
public class ThreadBlocked extends Thread {
代码语言:javascript复制private byte[] lock = new byte[0];
public ThreadBlocked(byte[] lock) {
this.lock = lock;
}
@Override
public void run() {
synchronized (lock) {
try {
Thread.sleep(10000);// 休眠十秒,防止过快结束
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
byte[] lock = new byte[0];
ThreadBlocked threadBlocked = new ThreadBlocked(lock);
threadBlocked.start();
ThreadBlocked threadBlocked1 = new ThreadBlocked(lock);
threadBlocked1.start();
Thread.sleep(1000);
System.out.println("threadBlocked1-blocked : " threadBlocked1.getState());
}
}
`
- 输出
threadBlocked1-blocked : BLOCKED
- WAITING (当执行 Object.wait(); Thread.join(); LockSupport.park();时,线程计入等待状态)
- Object.wait()
`package com.example.demo.thread;
public class ThreadWaitingObjectWait extends Thread {
代码语言:javascript复制private byte[] lock = new byte[0];
public ThreadWaitingObjectWait(byte[] lock) {
this.lock = lock;
}
@Override
public void run() {
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
byte[] lock = new byte[0];
ThreadWaitingObjectWait waitingObjectWait = new ThreadWaitingObjectWait(lock);
waitingObjectWait.start();
Thread.sleep(100);
System.out.println("waitingObjectWait wait.start(): " waitingObjectWait.getState());
synchronized (lock) {
lock.notify();
}
Thread.sleep(100);
System.out.println("waitingObjectWait lock.notify() :" waitingObjectWait.getState());
}
} `
- 输出
waitingObjectWait wait.start(): WAITING waitingObjectWait lock.notify() :TERMINATED
- LockSupport.park()
`package com.example.demo.thread;
import java.util.concurrent.locks.LockSupport;
public class ThreadWaitingPark extends Thread {
代码语言:javascript复制private byte[] lock = new byte[0];
public ThreadWaitingPark(byte[] lock) {
this.lock = lock;
}
@Override
public void run() {
// 以下三种均可使线程进入waiting状态
// Object.wait(); // Thread.join();; LockSupport.park();
代码语言:javascript复制}
public static void main(String[] args) throws InterruptedException {
byte[] lock = new byte[0];
ThreadWaitingPark threadWaitingPark = new ThreadWaitingPark(lock);
threadWaitingPark.start();
Thread.sleep(1000);
System.out.println("threadWaiting-Waiting-when LockSupport.park() : " threadWaitingPark.getState());
LockSupport.unpark(threadWaitingPark);
Thread.sleep(1000);
System.out.println("threadWaiting-Waiting-when LockSupport.unpark(thread) : " threadWaitingPark.getState());
}
} `
- 输出
threadWaiting-Waiting-when LockSupport.park() : WAITING threadWaiting-Waiting-when LockSupport.unpark(thread) : TERMINATED
- Thread.join()
`package com.example.demo.thread;
import org.jetbrains.annotations.NotNull;
public class ThreadWaitingThreadJoin extends Thread {
代码语言:javascript复制private Thread thread = new Thread();
public ThreadWaitingThreadJoin(Thread thread) {
this.thread = thread;
}
@Override
public void run() {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
byte[] lock = new byte[0];
ThreadNewRunable threadNewRunable = new ThreadNewRunable(lock);
threadNewRunable.start();
ThreadWaitingThreadJoin threadWaitingThreadJoin = new ThreadWaitingThreadJoin(threadNewRunable);
threadWaitingThreadJoin.start();
Thread.sleep(100);
System.out.println("threadWaitingThreadJoin.join() : " threadWaitingThreadJoin.getState());
}
} `
- 输出
threadWaitingThreadJoin.join() : TERMINATED
- TIMEDWAITING (WAITING等待只要没有线程唤醒,它就一直等待下去,而TIMEDWAITING则会在超出等待时间后唤醒线程去获取锁)
当执行 Thread.sleep(long); Object.wait(long); Thread.join(long); LockSupport.parkNanos(); LockSupport.parkUntil(); 时线程会进入延时等待状态。
- Thread.sleep(long)
`package com.example.demo.thread;
public class ThreadTimedWaitingThreadSleep extends Thread {
代码语言:javascript复制private byte[] lock = new byte[0];
public ThreadTimedWaitingThreadSleep(byte[] lock) {
this.lock = lock;
}
@Override
public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
byte[] lock = new byte[0];
ThreadTimedWaitingThreadSleep timedWaitingThreadSleep = new ThreadTimedWaitingThreadSleep(lock);
timedWaitingThreadSleep.start();
Thread.sleep(100);
System.out.println("timedWaitingThreadSleep Thread.sleep(long) : " timedWaitingThreadSleep.getState());
}
} `
- 输出
timedWaitingThreadSleep Thread.sleep(long) : TIMED_WAITING
- Object.wait(long)
`package com.example.demo.thread;
public class ThreadTimedWaitingObjectWait extends Thread { private byte[] lock = new byte[0];
代码语言:javascript复制public ThreadTimedWaitingObjectWait(byte[] lock) {
this.lock = lock;
}
@Override
public void run() {
synchronized (lock) {
// 其它使线程进入timedWaiting
// Thread.join(long)
//LockSupport.parkNanos()
//LockSupport.parkUntil()
try {
lock.wait(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
byte[] lock = new byte[0];
ThreadTimedWaitingObjectWait timedWaitingObjectWait = new ThreadTimedWaitingObjectWait(lock);
timedWaitingObjectWait.start();
Thread.sleep(100);
System.out.println("timedWaitingObjectWait wait.start(): " timedWaitingObjectWait.getState());
Thread.sleep(2000);
System.out.println("timedWaitingObjectWait after sleep(20000) :" timedWaitingObjectWait.getState());
}
} `
- 输出
timedWaitingObjectWait wait.start(): TIMED_WAITING
- 源码在下面这个工程的tread包里,需要自取
源码仓库地址