本文以java1.8为例,存在类java.lang.state中如下。
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:
*
* - {@link Object#wait() Object.wait} with no timeout
* - {@link #join() Thread.join} with no timeout
* - {@link LockSupport#park() LockSupport.park}
*
*
* A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called Object.wait()
* on an object is waiting for another thread to call
* Object.notify() or Object.notifyAll() on
* that object. A thread that has called Thread.join()
* 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:
*
* - {@link #sleep Thread.sleep}
* - {@link Object#wait(long) Object.wait} with timeout
* - {@link #join(long) Thread.join} with timeout
* - {@link LockSupport#parkNanos LockSupport.parkNanos}
* - {@link LockSupport#parkUntil LockSupport.parkUntil}
*
*/
TIMED_WAITING,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}
线程没有启动之前的状态.
@Test
public void New() throws InterruptedException {
Thread t1 = new Thread(()->{
System.out.println("2:"+Thread.currentThread().getState());
});
System.out.println("1:"+t1.getState());
t1.start();
t1.join();
System.out.println("3:"+t1.getState());
}
输出结果:
1:NEW
2:RUNNABLE
3:TERMINATED
等待其他来自操作系统如处理器的状态。
例子如上。
线程在等待锁的监控锁的状态,synchronized代码块或者方法。
public void Bolocked() throws InterruptedException {
final Object o = new Object();
Thread t1 = new Thread(()->{
synchronized (o){
try {
System.out.println("t1申请到了锁....");
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
new Thread(()->{
synchronized (o) {
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}).start();
TimeUnit.SECONDS.sleep(1);
t1.start();
TimeUnit.SECONDS.sleep(1);
System.out.println("t1:"+t1.getState());
}
输出状态
t1:BLOCKED
调用以下方法的时候
- {@link Object#wait() Object.wait} with no timeout
- {@link #join() Thread.join} with no timeout
- {@link LockSupport#park() LockSupport.park}
@Test
public void Waiting() throws InterruptedException {
final Lock lock = new ReentrantLock();
Thread t1 = new Thread(()->{
lock.lock();
System.out.println("t1申请到了锁....");
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
lock.unlock();
});
new Thread(()->{
lock.lock();
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
lock.unlock();
}).start();
TimeUnit.SECONDS.sleep(1);
t1.start();
TimeUnit.SECONDS.sleep(1);
System.out.println("t1:"+t1.getState());
}
输出打印结果:
t1:WAITING
- {@link #sleep Thread.sleep}
- {@link Object#wait(long) Object.wait} with timeout
- {@link #join(long) Thread.join} with timeout
- {@link LockSupport#parkNanos LockSupport.parkNanos}
- {@link LockSupport#parkUntil LockSupport.parkUntil}
@Test
public void TIMED_WAITING() throws InterruptedException {
Thread t1 = new Thread(() -> {
LockSupport.park();
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
t1.start();
SleepHelper.sleepSeconds(1);
System.out.println("1: " + t1.getState());
LockSupport.unpark(t1);
TimeUnit.SECONDS.sleep(1);
System.out.println("2: " + t1.getState());
}
输出结果
1: WAITING
2: TIMED_WAITING