概念
- 是操作系统能够进行运算调度的最小单位。
- 是进程中的实际运作单位。
- 是系统分配处理器时间资源的基本单元,即程序执行的最小单位
顺便介绍下进程
- 进程是在系统中正在运行的一个应用程序;程序一旦运行就是进程;
- 进程是资源分配的最小单位
- 进程可以包含多个线程
线程状态
- 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块,