线程的6种状态
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方法
- RUNNABLE 就绪状态(调用start,等待调度)+正在运行
- BLOCKED 等待监视器锁时,陷入阻塞状态
- WAITING 等待状态的线程正在等待另一线程执行特定的操作(如notify)
- TIMED_WAITING 具有指定等待时间的等待状态
- TERMINATED 线程完成执行,终止状态
多线程之间如何通讯
wait、notify、notifyAll()方法
lock
停止线程
多线程之间如何实现通讯
什么是多线程之间通讯?
多线程之间通讯,其实就是多个线程在操作同一个资源,但是操作的动作不同。
画图演示
需求:第一个线程写入(input)用户,另一个线程取读取(out)用户.实现读一个,写一个操作。
package com.evan.springboot.concurrentDemo.communication;
import lombok.Data;
/**
* @author evanYang
* @version 1.0
* @date 2020/05/04 09:47
*/
@Data
public class Res {
private String userName;
private String userSex;
private boolean flagDemo=false;
}
package com.evan.springboot.concurrentDemo.communication;
/**
* @author evanYang
* @version 1.0
* @date 2020/05/04 09:48
*/
public class IntThread extends Thread {
private Res res;
public IntThread(Res res){
this.res=res;
}
@Override
public void run() {
int count=0;
while (true){
synchronized (res){
if (res.isFlagDemo()){
try {
res.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (count==0){
res.setUserName("Evan");
res.setUserSex("男");
}else{
res.setUserName("Vale");
res.setUserSex("女");
}
count=(count+1)%2;
res.setFlagDemo(true);
res.notify();
}
}
}
}
package com.evan.springboot.concurrentDemo.communication;
import com.alibaba.fastjson.JSONObject;
/**
* @author evanYang
* @version 1.0
* @date 2020/05/04 09:53
*/
public class OoutThread extends Thread {
private Res res;
public OoutThread(Res res) {
this.res = res;
}
@Override
public void run() {
while (true){
synchronized(res){
if (!res.isFlagDemo()){
try {
res.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(JSONObject.toJSONString(res));
res.setFlagDemo(false);
res.notify();
}
}
}
}
package com.evan.springboot.concurrentDemo.communication;
/**
* @author evanYang
* @version 1.0
* @date 2020/05/04 09:56
*/
public class Client {
public static void main(String[] args) {
Res res = new Res();
IntThread intThread = new IntThread(res);
OoutThread ooutThread = new OoutThread(res);
intThread.start();
ooutThread.start();
}
}
wait(),notify(),notifyAll()方法
wait()、notify()、notifyAll()是三个定义在Object类里的方法,可以用来控制线程的状态。
这三个方法最终调用的都是jvm级的native方法。随着jvm运行平台的不同可能有些许差异。
如果对象调用了wait方法就会使持有该对象的线程把该对象的控制权交出去,然后处于等待状态。
如果对象调用了notify方法就会通知某个正在等待这个对象的控制权的线程可以继续运行。
如果对象调用了notifyAll方法就会通知所有等待这个对象控制权的线程继续运行。
Wait与Sleep的区别?
对于sleep()方法,我们首先要知道该方法是属于Thread类中的。而wait()方法,则是属于Object类中的。
sleep()方法导致了程序暂停执行指定的时间,让出cpu该其他线程,但是他的监控状态依然保持者,当指定的时间到了又会自动恢复运行状态。
在调用sleep()方法的过程中,线程不会释放对象锁。
而当调用wait()方法的时候,线程会放弃对象锁,进入等待此对象的等待锁定池,只有针对此对象调用notify()方法后本线程才进入对象锁定池准备
获取对象锁进入运行状态。
JDK1.5-Lock
在 jdk1.5 之后,并发包中新增了 Lock 接口(以及相关实现类)用来实现锁功能,Lock 接口提供了与 synchronized 关键字类似的同步功能,但需要在使用时手动获取锁和释放锁。
Lock lock = new ReentrantLock();
lock.lock();
try{
//可能会出现线程安全的操作
}finally{
//一定在finally中释放锁
//也不能把获取锁在try中进行,因为有可能在获取锁的时候抛出异常
lock.ublock();
}
Lock 接口与 synchronized 关键字的区别
Lock 接口可以尝试非阻塞地获取锁 当前线程尝试获取锁。如果这一时刻锁没有被其他线程获取到,则成功获取并持有锁。
Lock 接口能被中断地获取锁 与 synchronized 不同,获取到锁的线程能够响应中断,当获取到的锁的线程被中断时,中断异常将会被抛出,同时锁会被释放。
Lock 接口在指定的截止时间之前获取锁,如果截止时间到了依旧无法获取锁,则返回