当Java程序启动时,JVM会为主线程分配一个特殊的栈来执行代码。同时,程序可以创建若干个子线程以支持并发执行相应的任务。线程在执行过程中,可以出现以下几种状态:
新建状态(New)
当线程对象创建以后,该线程处于新建状态。此时线程对象已经在内存中了,但是还没有分配系统资源,没有被CPU选中去执行,也没有开始执行线程中的代码。因此,新建状态的线程在内存中的状态是一片混沌。
代码示例
public class NewThreadDemo {
public static void main(String[] args) {
Thread thread = new Thread();
System.out.println("New Thread's State: " + thread.getState());
}
}
就绪状态(Runnable)
在获得了CPU时间片之后,线程开始执行,此时线程进入就绪状态。在线程进入就绪状态后,它就有机会获得系统的CPU时间片,占用CPU资源执行代码。
代码示例
public class ReadyThreadDemo {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " running");
}
}
});
System.out.println("Thread's State: " + thread.getState());
thread.start();
System.out.println("Thread's State: " + thread.getState());
}
}
在上述示例中,“Thread's State: RUNNABLE”代表线程已经被启动,进入就绪状态。由于线程是并发执行的,因此,运行结果是不确定的。
运行状态(Running)
当就绪状态的线程获得CPU时间片,开始执行run()方法(或者是重新执行run()方法)之后,线程进入运行状态。此时,线程会执行它的任务代码。
代码示例
public class RunningThreadDemo {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " running");
}
}
});
System.out.println("Thread's State: " + thread.getState());
thread.start();
while (thread.getState() != Thread.State.TERMINATED) {
System.out.println("Thread's State: " + thread.getState());
}
System.out.println("Thread's State: " + thread.getState());
}
}
在上述示例中,“Thread's State: RUNNING”代表线程正在运行。为了让线程运行一定的时间,我们使用了while循环来输出线程的状态,直到线程运行结束(TERMINATED)。
阻塞状态(Blocked)
线程在执行过程中,可能因为某些原因,比如等待输入输出、等待某个锁、休眠等,使CPU无法继续执行该线程。此时,线程就从运行状态进入了阻塞状态,等待相应的条件被满足后重新进入就绪状态以等待获取资源继续执行。
代码示例
public class BlockedThreadDemo {
public static void main(String[] args) throws InterruptedException {
final Object lock = new Object();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println(Thread.currentThread().getName() + " acquired lock");
try {
System.out.println(Thread.currentThread().getName() + " waiting for lock...");
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " released lock");
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println(Thread.currentThread().getName() + " acquired lock");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.notify();
System.out.println(Thread.currentThread().getName() + " sent signal");
}
System.out.println(Thread.currentThread().getName() + " released lock");
}
});
thread1.start();
thread2.start();
TimeUnit.SECONDS.sleep(1);
System.out.println("Thread 1 State: " + thread1.getState());
System.out.println("Thread 2 State: " + thread2.getState());
TimeUnit.SECONDS.sleep(3);
System.out.println("Thread 1 State: " + thread1.getState());
System.out.println("Thread 2 State: " + thread2.getState());
}
}
在上述示例中,线程thread1试图获得一个锁,但是该锁被线程thread2占用,因此,线程thread1将进入阻塞状态,等待线程thread2释放锁并发送信号唤醒它。
终止状态(Terminated)
线程执行完自己的任务后,自动结束执行,进入终止状态。此时,线程释放占用的资源,例如对象锁、IO资源等。终止状态的线程不可再次调用start()方法。
代码示例
public class TerminatedThreadDemo {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " running");
}
}
});
System.out.println("Thread's State: " + thread.getState());
thread.start();
TimeUnit.SECONDS.sleep(3);
System.out.println("Thread's State: " + thread.getState());
}
}
在上述示例中,在线程执行完自己的任务之后,线程进入终止状态。由于终止状态的线程不可再次调用start()方法,因此,运行结果如下:
Thread's State: NEW
Thread's State: RUNNABLE
Thread-0 running
Thread-0 running
Thread-0 running
Thread-0 running
Thread-0 running
Thread's State: TERMINATED
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:简单谈谈Java 中的线程的几种状态 - Python技术站