线程
线程状态
- 新建(New)
- 创建一个线程对象
- 就绪(Runnable)
- start(),yield()方法,该线程处于就绪状态,等待获取cpu的使用权。
- 运行(running)
- 可运行状态的线程获得了cpu时间片(timeslice),执行程序代码。注:就绪状态是进入到运行状态的唯一入口
- 阻塞(Blocked)
- 等待获取阻塞锁,如::synchronized
- 等待(Waiting)
- 超时等待(TimedWaiting)
- 死亡(Terminated)
- 线程run()方法执行结束,或者因异常退出了run()方法,则该线程结束生命周期
枚举
展开
/**
* A thread state. A thread can be in one of the following states:
* <ul>
* <li>{@link #NEW}<br>
* A thread that has not yet started is in this state.
* </li>
* <li>{@link #RUNNABLE}<br>
* A thread executing in the Java virtual machine is in this state.
* </li>
* <li>{@link #BLOCKED}<br>
* A thread that is blocked waiting for a monitor lock
* is in this state.
* </li>
* <li>{@link #WAITING}<br>
* A thread that is waiting indefinitely for another thread to
* perform a particular action is in this state.
* </li>
* <li>{@link #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;
}
状态图
展开
例子
展开
private static final Object object = new Object();
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
Thread thread1 = Thread.currentThread();
Utils.print("进入run的线程状态", thread1.getState());
Utils.sleep(1000, () -> {
Utils.print("sleep中线程状态", thread1.getState());
});
Utils.print("结束sleep线程状态", thread1.getState());
// 阻塞某个对象的锁,执行run方法,前后sleep一秒
Utils.block(object,()->{
Utils.print("尝试获取锁线程状态", thread1.getState());
});
Utils.sleep(100);
synchronized (object){
Utils.print("获取到锁线程状态", thread1.getState());
Utils.iWait(object,()->{
Utils.print("wait后锁线程状态", thread1.getState());
});
}
});
Utils.print("start之前线程状态", thread.getState());
thread.start();
//阻塞主线程,等到thread结束
thread.join();
Utils.print("start结束线程状态", thread.getState());
}
// 输出:
// ------start之前线程状态 : NEW------
// ------进入run的线程状态 : RUNNABLE------
// ------sleep中线程状态 : TIMED_WAITING------
// ------结束sleep线程状态 : RUNNABLE------
// ------尝试获取锁线程状态 : BLOCKED------
// ------获取到锁线程状态 : RUNNABLE------
// ------wait后锁线程状态 : WAITING------
// ------start结束线程状态 : TERMINATED------
线程创建方式
- 创建一个Thread类的对象
- 实现runnable
- 实现callable,然后交给FutureTask包装一下(FutureTask也是实现了runnable的类),用法:
FutureTask<String> task = new FutureTask<>(() -> {
Utils.sleep(1000);
System.out.println("开始执行任务,计划返回:123");
return "123";
});
//创建线程
new Thread(task).start();
System.out.println("当前执行"+(task.isDone()?"已完成":"未完成"));
//调用get()方法阻塞主线程
String str = task.get();
System.out.println("执行结果 :" + str);
- 实现runnable
- 创建一个继承Thread类的对象
- 使用线程池
- 注解:@Async,@Schedules
- Executors
Thread的一些方法
- yield():让渡,让出执行优先级,让可能的线程抢占,感觉属于微操了。(实践的示例:todo)
- join(long millis):暂停当前线程,等到被调用线程执行结束(有一个等待时间)