一、同步和异步&&阻塞和非阻塞:
1、主要区别:同步异步强调被调用方是否反馈结果,而阻塞非阻塞指调用方是否一直等待被调用方反馈结果;
被调用方是否主动反馈结果 | 调用方是否一直等待响应结果 | |
同步 | F | |
异步 | T | |
阻塞 | T | |
非阻塞 | F |
2、举例:烧开水
- 同步:架锅点火,开始烧,不会主动反馈已经开始烧开水了,也不会主动反馈,水烧开了;
- 异步:架锅点火,开始烧,会主动反馈已经开始烧开水了,会主动反馈,水烧开了;
- 阻塞:架锅点火,开始烧,一直等待水烧开,不干别的事情,直到水烧开了
- 非阻塞:架锅点火,开始烧,可以继续做别的事情,等水开了再通知;
==============
- 同步阻塞:调用方一直在等待,被调用方不反馈结果,需要调用方一直等待水烧开(线程执行完成),不做别的事情;
- 同步非阻塞:调用方不会一直在等待,可以去干点别的事情,被调用方不反馈结果,但调用方需要时不时查看一下执行结果;
- 异步阻塞:调用方一直在等待,被调用方会反馈结果,调用方一直等待水烧开(被调用方反馈),不做别的事情;
- 异步非阻塞:调用方不会一直在等待,可以去干点别的事情,直到被调用方反馈结果;
二、线程的三种创建方式:
继承Thread:java是单继承,如果使用这种方式,则不能再继承其他类(一般情况下不用);
public static void main(String[] args) {
//System.out.println("Hello world!");
Thread t1 = new Thread(()->{
System.out.println("====这是第一个线程======");
});
t1.start();
System.out.println("线程名称:" + t1.getName());
}
实现Runnable接口:可以解决单继承问题,线程执行完就结束,没有返回值;
/**
* void方法,沒法返回值
*/
static class ThreadTwo implements Runnable{
@Override
public void run() {
System.out.println("=====这是第二个线程=======");
}
}
Thread t2 = new Thread(new ThreadTwo());
t2.start();
System.out.println("线程名称:" + t2.getName());
实现Callable<Object>接口:实际也是实现Runnable接口,进行了扩展,有返回值,使用的是同步非阻塞模式;
static class ThreadThree implements Callable<Object> {
@Override
public Object call() throws Exception {
int count = 0;
for (int i = 0; i < 100; i++) {
count += 1;
}
System.out.println("======这是第三个线程,有返回值=======");
return count;
}
}
//第三个线程
FutureTask<Object> futureTask = new FutureTask<>(new ThreadThree());
Thread t3 = new Thread(futureTask);
t3.start();
System.out.println("线程名称:" + t3.getName());
System.out.println("获取返回值count:" + futureTask.get());
三、线程的6种状态(也可以是5种,但java种就是6种)
1、Thread源码里面的状态:
A thread state. A thread can be in one of the following states:
NEW A thread that has not yet started is in this state.
RUNNABLE A thread executing in the Java virtual machine is in this state.
BLOCKED A thread that is blocked waiting for a monitor lock is in this state.
WAITING A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
TIMED_WAITING A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
TERMINATED A thread that has exited is in this state.
2、状态切换
3、实战演练各种状态
/**
* 线程的各种状态
* */
//NEW状态
Thread t1 = new Thread();
System.out.println("t1的状态" + t1.getState());
//RUNNABLE状态
Thread t2 = new Thread(() -> {
//啥也不干,就让线程一直在执行循环
while (true) {
}
});
t2.start();
System.out.println("t2的状态:" + t2.getState());
//block状态,使用同一个锁,当一个线程在执行并sleep,另外一个线程想要获取锁,只能进入block状态,阻塞等待
String lock = "lock";
ThreadTwo threadTwo1 = new ThreadTwo(lock);
ThreadTwo threadTwo2 = new ThreadTwo(lock);
Thread t3 = new Thread(threadTwo1);
Thread t4 = new Thread(threadTwo2);
t3.start();
t4.start();
System.out.println("t3的状态:" + t3.getState());
System.out.println("t4的状态:" + t4.getState());
static class ThreadTwo implements Runnable {
private String lock;
public ThreadTwo(String lock) {
this.lock = lock;
}
@Override
public void run() {
synchronized (lock) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
四、线程中的常见方法: