很多人对于线程的状态不是太了解,本次我想把我理解的有关线程的相关知识和大家分享一下,上面的图片是我从网上找到的,这张图片对于线程的知识了解有很大的帮助。
线程的状态:
- 新建状态(new):新创建了一个线程对象,该对象继承了Therad类或实现了Runnable接口,但是此时该对象没有调用start()方法。是一个初始状态。
- 就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法。该状态的线程位于可运行的线程池中,可以运行,等待获取CPU的时间片。
- 运行状态(Running):此时线程获取了CPU,执行代码。
- 阻塞状态(Blocked):阻塞状态是线程因为某种原因放弃CPU的使用权,暂时停止运行。知道线程进入就绪状态(Runnable),此时才有机会转入到运行状态。阻塞状态分为以下几种:
4.1. 等待阻塞(watiting):运行的线程执行wait()方法,JVM会把该线程放入等待池中。
4.2. 超时等待阻塞(time-waiting):运行时的线程会执行带有超时时间的wait()方法,JVM会把该线程放入等待池中。当超时后,线程重新转入就绪状态。
4.3. 同步阻塞(synchronized):运行时的线程获取对象的同步锁时,若该同步锁被别的线程占用,则JVM会把该线程放入锁池中。
4.4. 其他阻塞:运行的线程执行sleep或者join方法,或者执行I/O请求时,JVM会把该线程置为阻塞状态,当sleep状态超时,join等待线程终止或者超时是,线程重新转入就绪状态。 - 死亡状态:线程执行完毕,该线程结束生命周期。
以下是阻塞状态的实例,通过实例,并且配合jdk自带的jvisualvm,我们可以看到线程的状态。
package com.unionfin.thread;
import java.util.concurrent.TimeUnit;
public class JavaThreadState
{
public static void main(String[] args)
{
Thread waittingBlocked = new Thread(new WaitingBlocked(),
"waittingBlocked");
Thread timeWaittingBlocked = new Thread(new TimeWaitBlocked(),
"timeWaittingBlocked");
// synchronizedBlocked1,synchronizedBlocked2抢同一把锁,只有一个可以抢到
Thread synchronizedBlocked1 = new Thread(new SynchronizedBlocked(),
"synchronizedBlocked1");
Thread synchronizedBlocked2 = new Thread(new SynchronizedBlocked(),
"synchronizedBlocked2");
// sleepBlocked线程睡了100秒,在这一百秒中,其处于阻塞状态
Thread sleepBlocked = new Thread(new sleepBlocked(), "sleepBlocked");
// 获取主线程
Thread mainTherad = Thread.currentThread();
// 主线程调用join方法,只有joinBlocked线程死亡之后,主线程才会从阻塞中返回
Thread joinBlocked = new Thread(new JoinBlocked(mainTherad),
"joinBlocked");
waittingBlocked.start();
timeWaittingBlocked.start();
synchronizedBlocked1.start();
synchronizedBlocked2.start();
sleepBlocked.start();
joinBlocked.start();
}
}
/**
* 等待阻塞,一直处于等待状态,知道有其他线程将其唤醒
*
* @author xiaotao
*
*/
class WaitingBlocked implements Runnable
{
public void run()
{
synchronized (WaitingBlocked.class)
{
try
{
//调用wait()方法时线程会放弃对象锁
WaitingBlocked.class.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
/**
* 在五十秒内处于等待状态,过了五十秒之后会被唤醒,进入runnable状态
*
* @author xiaotao
*
*/
class TimeWaitBlocked implements Runnable
{
public void run()
{
try
{
synchronized (TimeWaitBlocked.class)
{
TimeWaitBlocked.class.wait(50000);
System.out
.println("l have wait about 50 s,so i don't want to wait");
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
/**
* 该实例是一个抢锁的阻塞实例,两个线程争抢一个锁,只有一个线程抢到锁,另外一个线程抢不到锁,处于阻塞状态
*
* @author xiaotao
*
*/
class SynchronizedBlocked implements Runnable
{
public void run()
{
synchronized (SynchronizedBlocked.class)
{
while (true)
{
}
}
}
}
/**
* 处于sleep阻塞状态,睡了100秒之后处于runnable状态,之后争抢cpu。
*
* @author xiaotao
*
*/
class sleepBlocked implements Runnable
{
public void run()
{
try
{
while (true)
{
TimeUnit.SECONDS.sleep(100);
System.out.println("l have sleep 100 s");
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
class JoinBlocked implements Runnable
{
private Thread thread;
public JoinBlocked(Thread thread)
{
this.thread = thread;
}
public void run()
{
try
{
thread.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
while (true)
{
}
}
}
"joinBlocked" prio=6 tid=0x0000000006a2f000 nid=0x2604 runnable [0x00000000077ef000]
java.lang.Thread.State: RUNNABLE
at com.unionfin.thread.JoinBlocked.run(JavaThreadState.java:170)
at java.lang.Thread.run(Thread.java:662)
Locked ownable synchronizers:
- None
"sleepBlocked" prio=6 tid=0x0000000006a33000 nid=0x65c waiting on condition [0x00000000076ef000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:302)
at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:328)
at com.unionfin.thread.sleepBlocked.run(JavaThreadState.java:137)
at java.lang.Thread.run(Thread.java:662)
Locked ownable synchronizers:
- None
"synchronizedBlocked2" prio=6 tid=0x0000000006a35000 nid=0x2198 runnable [0x00000000075ef000]
java.lang.Thread.State: RUNNABLE
at com.unionfin.thread.SynchronizedBlocked.run(JavaThreadState.java:113)
- locked <0x000000077caf7278> (a java.lang.Class for com.unionfin.thread.SynchronizedBlocked)
at java.lang.Thread.run(Thread.java:662)
Locked ownable synchronizers:
- None
"synchronizedBlocked1" prio=6 tid=0x0000000006a24000 nid=0x251c waiting for monitor entry [0x00000000074ef000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.unionfin.thread.SynchronizedBlocked.run(JavaThreadState.java:113)
- waiting to lock <0x000000077caf7278> (a java.lang.Class for com.unionfin.thread.SynchronizedBlocked)
at java.lang.Thread.run(Thread.java:662)
Locked ownable synchronizers:
- None
"timeWaittingBlocked" prio=6 tid=0x0000000006a23000 nid=0x144c in Object.wait() [0x00000000073ef000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000077caf6af8> (a java.lang.Class for com.unionfin.thread.TimeWaitBlocked)
at com.unionfin.thread.TimeWaitBlocked.run(JavaThreadState.java:87)
- locked <0x000000077caf6af8> (a java.lang.Class for com.unionfin.thread.TimeWaitBlocked)
at java.lang.Thread.run(Thread.java:662)
Locked ownable synchronizers:
- None
"waittingBlocked" prio=6 tid=0x0000000006a21000 nid=0x1ba4 in Object.wait() [0x00000000072ef000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000077caf5960> (a java.lang.Class for com.unionfin.thread.WaitingBlocked)
at java.lang.Object.wait(Object.java:485)
at com.unionfin.thread.WaitingBlocked.run(JavaThreadState.java:61)
- locked <0x000000077caf5960> (a java.lang.Class for com.unionfin.thread.WaitingBlocked)
at java.lang.Thread.run(Thread.java:662)
Locked ownable synchronizers:
- None
以上是dump线程信息,我们可以看到各个线程目前所处状态。其中有一个线程比较有意思,就是joinBlocked线程,在该线程中, 我传入一个A线程,A线程调用join()方法,此时只有joinBlocked线程死亡之后,A线程才会进入runnable阶段。
以上就是线程状态的几个例子,希望对大家有所帮助。