1. Thread
1.1 线程属性与状态流
- 线程ID:线程用ID来标识出不同线程
- 线程名字(Name):让用户或者程序猿开发调试或运行中定位线程的问题等。
- 守护线程(isDaemon):当为true时,代表该线程为守护线程,false为非守护线程,也可以称作用户线程。
- 线程优先级(Priority):作用是告诉线程调度器,希望那个线程多运行,那个线程少运行。
1.1.1 线程ID
- 线程ID从1开始,JVM运行起来后,我们自己创建的线程Id 早已不是0
/*** @author yiren*/public class ID {public static void main(String[] args) {Thread thread = new Thread();System.out.println(Thread.currentThread().getName() + ": " + Thread.currentThread().getId());System.out.println(thread.getName() + ": " + thread.getId());}}
main: 1Thread-0: 13
- 在线程初始化中,有tid赋值
/* For generating thread ID */private static long threadSeqNumber;private void init(ThreadGroup g, Runnable target, String name,long stackSize, AccessControlContext acc,boolean inheritThreadLocals) {....../* Set thread ID */tid = nextThreadID();}private static synchronized long nextThreadID() {return ++threadSeqNumber;}
threadSeqNumber没有赋值,所以为0,而nextThreadID()的++threadSeqNumber使得线程从ID从1开始.- 那么为什么答应出来的子线程ID是13呢?
- 如果你是用的IDEA,可以通过debug下个断点在最后一句上面,然后看一下当前线程的情况。
- 实际就是JVM帮我们起了一些线程。

Finalizer线程: JVM进行GC时,首先使用可达性分析算法,找出不在GC Roots引用链上的对象,这时进行一次标记(标记出需要回收的对象)并筛选(对需要回收对象进行筛选),筛选条件就是是否有必要执行finalize方法。当对象没有覆盖或已执行过finalize方法,则没有必要执行;否则,将对象放到由JVM创建的Finalizer线程维护的F-Queue(java.lang.ref.Finalizer.ReferenceQueue)队列中,Finalizer线程会遍历执行队列中对象的finalize方法,只有当F-Queue中对象finalize执行完成后,并且下次GC时可达性分析不再GC Roots的引用链上,则这些对象占用的内存才能被真正回收。重写finalize方法可以方便我们去重新建立对象的引用关系,避免被回收。
Reference Handler 线程:ReferenceHandler线程是一个拥有最高优先级的守护线程,它是Reference类的一个内部类,在Reference类加载执行cinit的时候被初始化并启动;它的任务就是当pending队列不为空的时候,循环将pending队列里面的头部的Reference移除出来,如果这个对象是个Cleaner实例,那么就直接执行它的clean方法来执行清理工作;
Signal Dispatcher 线程: Attach Listener 线程的职责是接收外部jvm命令,当命令接收成功后,会交给signal dispather线程去进行分发到各个不同的模块处理命令,并且返回处理结果。signal dispather线程也是在第一次接收外部jvm命令时,进行初始化工作。类似的还有Attach Listner线程。
1.1.2 线程名字
- 默认线程名字源码分析:
- 在没有指定线程名字的时候,线程默认传一个
"Thread-" + nextThreadNum()作为名字 - 而nextThreadNum获取到的threadInitNumber则是一个从0自增的一个静态变量。因为用了synchronized,所以是不会重复的。
/* For autonumbering anonymous threads. */private static int threadInitNumber;private static synchronized int nextThreadNum() {return threadInitNumber++;}public Thread() {init(null, null, "Thread-" + nextThreadNum(), 0);}
- 如何修改线程名字
- 一个是通过构造方法
另一个是通过
setName(String name)设置public Thread(String name) {init(null, null, name, 0);}private void init(ThreadGroup g, Runnable target, String name,long stackSize) {init(g, target, name, stackSize, null, true);}private void init(ThreadGroup g, Runnable target, String name,long stackSize, AccessControlContext acc,boolean inheritThreadLocals) {if (name == null) {throw new NullPointerException("name cannot be null");}this.name = name;...}public final synchronized void setName(String name) {checkAccess();if (name == null) {throw new NullPointerException("name cannot be null");}this.name = name;if (threadStatus != 0) {setNativeName(name);}}
注意
setName(String name)调用的时候,当线程是NEW状态的时候,设置name会一同把JVM里面的CPP的线程名字设置好,而如果不是NEW状态则只能修改java中我们可以看到的名称。
1.1.3 守护线程(Daemon)
- 作用:给用户线程提供服务
- 三个特性:
- 线程默认类型继承自父线程
- 被谁启动
- 不影响JVM退出
- 守护线程和普通线程的区别
- 整体没有太大区别
- 唯一的区别是是否影响JVM的退出
- 常见面试问题
- 守护线程和用户线程的区别
- 我们是否需要给线程设置为守护线程?
- 不需要,如果设置生守护线程,在JVM退出时会忽略你正在执行的任务,如果你正在执行一些数据操作,那么就会造成数据不一致了。
1.1.4 线程优先级
- 在Java中优先级有个10个等级,默认为5,通过
setPriority(int newPriority)设置 - 但是我们程序在编码的时候,不应该依赖于优先级
- 高度依赖于操作系统,不同的操作系统在实现优先级执行的时候不一样(windows中只有7个等级,更甚有的没有优先级。)设置优先级是不可靠的
- 优先级可能会被操作系统改变
/*** The minimum priority that a thread can have.*/public final static int MIN_PRIORITY = 1;/*** The default priority that is assigned to a thread.*/public final static int NORM_PRIORITY = 5;/*** The maximum priority that a thread can have.*/public final static int MAX_PRIORITY = 10;public final void setPriority(int newPriority) {ThreadGroup g;checkAccess();if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {throw new IllegalArgumentException();}if((g = getThreadGroup()) != null) {if (newPriority > g.getMaxPriority()) {newPriority = g.getMaxPriority();}setPriority0(priority = newPriority);}}
1.1.5 状态流
- Java线程的6个状态

- NEW 已创建还未启动 对应new Thread()过后就是这个状态
- RUNNABLE 调用了start() 过后 马上进入RUNNABLE 对应操作系统READY和RUNNING
- BLOCKED 当一个线程进入synchronized代码块的时候并且该锁被其他线程占用就是BLOCKED状态
- WAITING 没有设置time参数的wait()、join()等方法
- TIMED-WATING 设置time参数的wait()、join()、sleep()等方法
- TERMINATED
- 阻塞状态
- 一般习惯把 BLOCKED WAITING TIME_WAITING 都称为阻塞状态
1.2. 线程的启动
1.2.1 启动线程 - start()和run()方法调用对比
/*** 对比start和run两种启动线程的方式* @author yiren*/public class StartAndRunThread {public static void main(String[] args) {// 直接使用run方法Runnable runnable = () -> System.out.println(Thread.currentThread().getName());runnable.run();Thread thread = new Thread(runnable);thread.run();// 使用startthread.start();}}
mainmainThread-0Process finished with exit code 0
- 由上可知, 无论是
Runnable还是Thread的调用run()方法都是在当前线程直接运行,就是方法调用。 - 而调用
start()方法的时候,则是另起线程来运行run()方法中的内容。
1.2.2 关于start()方法
- 启动新线程:
- 他会涉及到两个线程,要有一个当前线程调用
start()方法,常见的为主线程main,也可以是其他线程;另外一个新线程在核实的时候执行run()方法 Thread的对象通过start()方法告诉JVM,我有一个线程需要启动,你在合适的时候运行它。
- 他会涉及到两个线程,要有一个当前线程调用
- 准备工作
- 首先它要让自己进入就绪状态,就绪状态是指我已经获取到除了CPU意外的其他资源(如上下文、栈、线程状态、PC程序计数器等)
- 不能重复的
start()/*** @author yiren*/public class DoubleStartThread {public static void main(String[] args) {Thread thread = new Thread(() -> System.out.println(Thread.currentThread().getName()));thread.start();thread.start();}}
Exception in thread "main" java.lang.IllegalThreadStateExceptionat java.lang.Thread.start(Thread.java:708)at com.imyiren.concurrency.threadcore.startthread.DoubleStartThread.main(DoubleStartThread.java:10)Thread-0Process finished with exit code 1
IllegalThreadStateException非法线程状态如果
start()开始,正常线程线程就会按照new->runnable->running->dead,如果一个线程执行完毕就会变成终止,就无法返回回去。所以才会抛出非法线程状态异常1.2.3 start()方法源码分析/* Java thread status for tools,* initialized to indicate thread 'not yet started'*/private volatile int threadStatus = 0;// Thread的start方法public synchronized void start() {/*** This method is not invoked for the main method thread or "system"* group threads created/set up by the VM. Any new functionality added* to this method in the future may have to also be added to the VM.** A zero status value corresponds to state "NEW".*/if (threadStatus != 0)throw new IllegalThreadStateException();/* Notify the group that this thread is about to be started* so that it can be added to the group's list of threads* and the group's unstarted count can be decremented. */group.add(this);boolean started = false;try {start0();started = true;} finally {try {if (!started) {group.threadStartFailed(this);}} catch (Throwable ignore) {/* do nothing. If start0 threw a Throwable thenit will be passed up the call stack */}}}
- 启动新线程检查线程状态
- 线程的状态
threadState,还没有启动的时候,默认值就是0,也就是还没有启动 - 代码中的状态判断,如果线程状态不为0,那就抛出
IllegalThreadStateException异常。 - 注释上
A zero status value corresponds to state "NEW".可知,0就为NEW状态。
- 线程的状态
- 加入线程组
- 通过状态检查后就把当前线程放入到属性
ThreadGroup group的threads数组中,
- 通过状态检查后就把当前线程放入到属性
- 调用
start()- 然后就去执行
private native void start0()方法,注意,此方法是native方法(C++实现)1.2.4 run()方法源码分析/*** If this thread was constructed using a separate* <code>Runnable</code> run object, then that* <code>Runnable</code> object's <code>run</code> method is called;* otherwise, this method does nothing and returns.* <p>* Subclasses of <code>Thread</code> should override this method.** @see #start()* @see #stop()* @see #Thread(ThreadGroup, Runnable, String)*/@Overridepublic void run() {if (target != null) {target.run();}}
- 然后就去执行
- 在多线程中
Thread的run()方法会有两种情况,一种是如果重写了就调用重写Thread类的run()方法,一种是调用Runnable实现的run()方法 如果直接调用
run()方法的话,就只是调用一个普通的方法而已。要启动一个线程还是只能调用start方法去间接调用我们的run()方法。1.3 创建线程到底有几种方法
1.3.1 Thread源码分析/* What will be run. */private Runnable target;public Thread(Runnable target) {init(null, target, "Thread-" + nextThreadNum(), 0);}private void init(ThreadGroup g, Runnable target, String name,long stackSize) {init(g, target, name, stackSize, null, true);}private void init(ThreadGroup g, Runnable target, String name,long stackSize, AccessControlContext acc,boolean inheritThreadLocals) {......this.target = target;......}@Overridepublic void run() {if (target != null) {target.run();}}
- 我们可以看到平时我们通过实现Runnable接口和继承Thread来重写run方法,最终归结到了run方法的调用上。一个是重写,一个是调用接口的方法。
1.3.2 Oracle官方文档对创建线程的说明
- Java SE 8 API文档: https://docs.oracle.com/javase/8/docs/api/
请查看java.lang.Thread的类说明文档。
- 将类继承Thread类重写run方法
官方原话:There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread.
/*** 实现线程的第一个方式 继承Thread* @author yiren*/public class MyThread extends Thread {@Overridepublic void run() {System.out.println(Thread.currentThread().getName() + " Thread running...");}public static void main(String[] args) throws IOException {new MyThread().start();System.in.read();}}
- 实现Runnable接口
官方原话:The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method.
/*** 实现线程的第二个方式 实现Runnable接口* @author yiren*/public class MyRunnable implements Runnable {@Overridepublic void run() {System.out.println(Thread.currentThread().getName() + " Runnable running...");}public static void main(String[] args) throws IOException {new Thread(new MyRunnable()).start();System.in.read();}}
Runnable的优点- 业务代码与线程类创建启动等逻辑解耦。
- 依赖倒置原则:抽象不应该依赖具体,具体应该依赖抽象
Runnable可复用,Thread则需要每次创建。- 类可以实现多个接口,而不可以继承多个对象。所以接口更好
- 业务代码与线程类创建启动等逻辑解耦。
如果两种方式都用会有什么效果呢?
/*** @author yiren*/public class MyThreadAndRunnable {public static void main(String[] args) {Thread thread = new Thread(new Runnable() {@Overridepublic void run() {System.out.println(Thread.currentThread().getName() + " runnable running...");}}) {@Overridepublic void run() {System.out.println(Thread.currentThread().getName() + " thread running...");}};// 这个地方应该是执行重写Thread类的run方法中的逻辑!thread.start();}}
- 很明显,上面说了不重写
Thread的run()方法就是调用target.run(),如果重写那也就没有调用target.run()了。
1.3.3 归根结底
- 创建线程只有一种方式,就是创建
Thread类的对象,而构建一个线程的方式则有多种:比如创建线程类、实现Runnable接口、创建线程池、FutureTask等等。 线程池创建线程:实际是由默认的工厂代为创建Thread类来实现。
// Executors中的DefaultThreadFactorystatic class DefaultThreadFactory implements ThreadFactory {private static final AtomicInteger poolNumber = new AtomicInteger(1);private final ThreadGroup group;private final AtomicInteger threadNumber = new AtomicInteger(1);private final String namePrefix;DefaultThreadFactory() {SecurityManager s = System.getSecurityManager();group = (s != null) ? s.getThreadGroup() :Thread.currentThread().getThreadGroup();namePrefix = "pool-" +poolNumber.getAndIncrement() +"-thread-";}public Thread newThread(Runnable r) {Thread t = new Thread(group, r,namePrefix + threadNumber.getAndIncrement(),0);if (t.isDaemon())t.setDaemon(false);if (t.getPriority() != Thread.NORM_PRIORITY)t.setPriority(Thread.NORM_PRIORITY);return t;}}
- 由上
newThread()方法可知,即使是线程池,本质上还是使用Thread的创建线程。
- Callable和FutureTask创建线程,本质其实也是Thread
public interface RunnableFuture<V> extends Runnable, Future<V> {/*** Sets this Future to the result of its computation* unless it has been cancelled.*/void run();}
public class FutureTask<V> implements RunnableFuture<V> {......private volatile Thread runner;......
- 定时器Timer:它的TimerTask其实也是实现了Runnable接口,可以看下
TimerTask这个抽象类 ```java
/**
- @author yiren
*/
public class TimerExample {
public static void main(String[] args) {
} } ```Timer timer = new Timer();// 每隔1s打印下自己的名字timer.scheduleAtFixedRate(new TimerTask() {@Overridepublic void run() {System.out.println(Thread.currentThread().getName() + " timer running...");}}, 1000, 1000);
1.4. 停止线程
1.4.1 使用Interrupt来停止线程
- 使用interrupt来通知线程停止,而不是强制停止。
- 注意只是通知,并不是让线程立即停止。
- 只需要通知线程,你需要停止,线程通过响应interrupt来在合适的地方停止或者退出线程的执行。
- 为什么要这样做呢?
线程在停止时,所使用的资源没有释放造成资源浪费甚至BUG,数据处理没有完成造成数据不一致,这样的问题往往会令我们头疼。而如果使用interrupt来通知它,线程可以进行停止前的释放资源,完成必须要处理的数据任务,诸如此类的事情,就会令我们的程序的健壮性提升,也减少了系统出现问题的几率 停止普通线程
/*** run 方法内没有sleep或者wait方法时,停止线程。** @author yiren*/public class RightStopThreadWithoutSleep {public static void main(String[] args) throws InterruptedException {Thread thread = new Thread(() -> {int num = 0;long start = System.currentTimeMillis();while (num <= Integer.MAX_VALUE / 2) {if (num % 1000 == 0) {System.out.println(num + " 是10000的倍数!");}// 注意 如果不interrupted的响应处理,线程不会处理interruptif (Thread.currentThread().isInterrupted()) {System.out.println(Thread.currentThread().getName() + " was interrupted");break;}num++;}long end = System.currentTimeMillis();System.out.println("Task was finished! " + (end - start) / 1000.0 + "s");});thread.start();Thread.sleep(2000);thread.interrupt();}}
```sql …… 401797000 是10000的倍数! Thread-0 was interrupted Task was finished! 2.004s
Process finished with exit code 0
- 停止阻塞线程- 如果线程在阻塞状态,比如调用`sleep()`方法时,响应`interrupt`的方式是抛出异常。- 所以停止阻塞线程使用`try-catch`来实现```java/*** run 方法内有sleep时,停止线程。* @author yiren*/public class RightStopThreadWithSleep {public static void main(String[] args) throws InterruptedException {Thread thread = new Thread(() -> {int num = 0;long start = System.currentTimeMillis();while (num <= 300) {if (num % 100 == 0) {System.out.println(num + " 是100的倍数!");}num++;// 注意 如果不interrupted的响应处理,线程不会处理interruptif (Thread.currentThread().isInterrupted()) {System.out.println(Thread.currentThread().getName() + " was interrupted");break;}}try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();System.out.println(Thread.currentThread().getName() + " thread was interrupted by sleep!");}long end = System.currentTimeMillis();System.out.println("Task was finished! " + (end - start) / 1000.0 + "s");});thread.start();Thread.sleep(500);thread.interrupt();}}
0 是100的倍数!100 是100的倍数!200 是100的倍数!300 是100的倍数!java.lang.InterruptedException: sleep interruptedat java.lang.Thread.sleep(Native Method)at com.imyiren.concurrency.threadcore.stopthread.RightStopThreadWithSleep.lambda$main$0(RightStopThreadWithSleep.java:26)at java.lang.Thread.run(Thread.java:748)Thread-0 thread was interrupted by sleep!Task was finished! 0.505sProcess finished with exit code 0
每个循环中都有sleep
如果每个循环都有阻塞, 我们就可以不用每个循环都判断一次interrupted了,只需要处理catch的异常即可。
/*** 在执行过程中每次循环都会调用sleep获wait等方法** @author yiren*/public class RightStopThreadWithSleepInLoop {public static void main(String[] args) throws InterruptedException {Thread thread = new Thread(() -> {int num = 0;long start = System.currentTimeMillis();try {while (num <= 10000) {if (num % 100 == 0) {System.out.println(num + " 是100的倍数!");}Thread.sleep(10);num++;}} catch (InterruptedException e) {e.printStackTrace();System.out.println(Thread.currentThread().getName() + " thread was interrupted by sleep!");}long end = System.currentTimeMillis();System.out.println("Task was finished! " + (end - start) / 1000.0 + "s");});thread.start();Thread.sleep(5000);thread.interrupt();}}
```sql 0 是100的倍数! 100 是100的倍数! 200 是100的倍数! 300 是100的倍数! 400 是100的倍数! java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at com.imyiren.concurrency.threadcore.stopthread.RightStopThreadWithSleepInLoop.lambda$main$0(RightStopThreadWithSleepInLoop.java:19) at java.lang.Thread.run(Thread.java:748) Thread-0 thread was interrupted by sleep! Task was finished! 5.005s
Process finished with exit code 0
- 这个地方需要注意一个地方,`try-catch`的位置,这个不难看出,如果是下列代码,则不能`interrupt`,会死循环。。。```java/*** @author yiren*/public class CantInterrupt {public static void main(String[] args) throws InterruptedException {Thread thread = new Thread(() -> {int num = 0;long start = System.currentTimeMillis();while (num <= 10000) {if (num % 100 == 0) {System.out.println(num + " 是100的倍数!");}try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();System.out.println(Thread.currentThread().getName() + " thread was interrupted by sleep!");}num++;}long end = System.currentTimeMillis();System.out.println("Task was finished! " + (end - start) / 1000.0 + "s");});thread.start();Thread.sleep(5000);thread.interrupt();}}
0 是100的倍数!100 是100的倍数!200 是100的倍数!300 是100的倍数!400 是100的倍数!java.lang.InterruptedException: sleep interruptedat java.lang.Thread.sleep(Native Method)at com.imyiren.concurrency.threadcore.stopthread.CantInterrupt.lambda$main$0(CantInterrupt.java:17)at java.lang.Thread.run(Thread.java:748)Thread-0 thread was interrupted by sleep!500 是100的倍数!600 是100的倍数!700 是100的倍数!800 是100的倍数!......
InterruptedException处理最佳实践(业务中如何使用?)- 绝对不应屏蔽中断请求
- 非
run()方法直接抛出**interruptedException**,不做处理- 首先我们不能在业务方法中直接处理掉异常,不能
try-catch,需要直接抛出。 - 那么我们在业务方法中处理了这个异常会怎么样呢?那么如果
run()方法中有循环,则无法退出循环。。 - 最佳实践:在业务代码中有
InterruptedException优先选择 在方法签名中抛出异常,不处理。那么就会使InterruptedException在run()方法中强制try-catch。如下代码
- 首先我们不能在业务方法中直接处理掉异常,不能
/*** 生产中如何处理interrupted** @author yiren*/public class RightStopThreadInProd implements Runnable {@Overridepublic void run() {try {while (true) {System.out.println("business code...");// 假设调用其他方法throwInMethod();System.out.println("business code...");}} catch (InterruptedException e) {e.printStackTrace();System.out.println("catch interruptedException handle interrupted! ...");}}private void throwInMethod() throws InterruptedException {Thread.sleep(1000);}public static void main(String[] args) throws InterruptedException {Thread thread = new Thread(new RightStopThreadInProd());thread.start();Thread.sleep(500);thread.interrupt();}}
business code...java.lang.InterruptedException: sleep interruptedat java.lang.Thread.sleep(Native Method)at com.imyiren.concurrency.threadcore.stopthread.RightStopThreadInProd.throwInMethod(RightStopThreadInProd.java:28)at com.imyiren.concurrency.threadcore.stopthread.RightStopThreadInProd.run(RightStopThreadInProd.java:18)at java.lang.Thread.run(Thread.java:748)catch interruptedException handle interrupted! ...Process finished with exit code 0
直接在业务方法中恢复中断(当业务方法无法抛出或不想抛出时)
- 就是利用中断机制,调用
Thread.currentThread().interrupt()来恢复中断 ```java /**
- 生产中如何处理interrupted 2
- 最佳实践:在业务代码中有InterruptedException 在catch语句中调用Thread.currentThread().interrupt()
- 以便于在后续的执行中,能够检测到发生了中断。
@author yiren */ public class RightStopThreadInProd2 implements Runnable {
@Override public void run() { while (!Thread.currentThread().isInterrupted()) {
System.out.println("business code...");// 假设调用其他方法reInterrupted();System.out.println("business code...");
} }
private void reInterrupted() { try {
System.out.println("reInterrupted method business! ");Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " reInterrupted interrupt");Thread.currentThread().interrupt();
} }
public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(new RightStopThreadInProd2()); thread.start(); Thread.sleep(1500); thread.interrupt(); } } ```
- 就是利用中断机制,调用
business code...reInterrupted method business!business code...business code...reInterrupted method business!Thread-0 reInterrupted interruptbusiness code...Process finished with exit code 0
响应中断的一些方法
Object.wait(...)Thraed.sleep(...)Thread.join(...)java.util.concurrent.BlockingQueue.take()/put(E)java.util.concurrent.locks.Lock.lockInterruptibly()java.util.concurrent.CountDownLatch.await()java.util.CyclicBarrier.await()java.util.concurrent.Exchanger.exchange(V)java.nio.channels.InterruptibleChannel的相关方法java.nio.channels.Selector的相关方法1.4.2 错误停止线程的方式
被弃用的方法:
stop()、suspend()、resume()stop方法停止
- 由下代码可看到,很有可能,代码在计算过程中,最后一部分数据没被计算进去。
- 代码具有偶然性,可能出错,可能不会出错。
- 可想如果发生在银行转账过程中,那么最终的金额对不上。。。这就是个大故障了。。 ```java /**
- 错误的停止方法,用stop来停止线程,会导致线程运行一半突然停止
- 没办法完成一个基本单位的操作。会造成脏数据等问题 *
- @author yiren
*/
public class ThreadStop {
public static void main(String[] args) throws InterruptedException {
final Data data = new Data();
Thread thread = new Thread(() -> {
}); thread.start(); Thread.sleep(931); thread.stop(); System.out.println(data);while (true) {int randomInt = (int) (Math.random() * 11);int sum = 0, temp;for (int i = 1; i < data.nums.length + 1; i++) {temp = randomInt * i;sum += temp;data.nums[i-1] += temp;System.out.println("i=" + i + ", num=" + temp);try {Thread.sleep(10);} catch (InterruptedException e) {//...}}data.total -= sum;}
} } class Data{ int total = Integer.MAX_VALUE; int[] nums = new int[5];
@Override public String toString() { int sum = 0; for (int i = 0; i < nums.length; i++) {
sum += nums[i];
} return “Data{“ +
"total=" + total +", nums=" + Arrays.toString(nums) +", sumNums=" + sum +", sum=" + (sum + total) +", Integer.MAX_VALUE=" + Integer.MAX_VALUE +'}';
} }
i=5, num=40 i=1, num=7 i=2, num=14 i=3, num=21 i=4, num=28 Data{total=2147482402, nums=[90, 180, 270, 360, 415], sumNums=1315, sum=-2147483579, Integer.MAX_VALUE=2147483647}
Process finished with exit code 0
1. suspend和resume- `suspend()`方法会使得目标线程停下来,但却仍然持有在这之前获得的锁定。这样一来很容造成死锁。- 而`resume()`方法则是用于 恢复通过调用`suspend()`方法而停止运行的线程- 这两个方法都已被废弃,所以不推荐使用。- 用volatile设置boolean标志位2. 案例一:可以停止```sql/*** 看似可行的一个用volatile关键字案例* @author yiren*/public class VolatileWrong implements Runnable{private volatile boolean canceled = false;@Overridepublic void run() {int num = 0;while (!canceled) {num++;if (num % 100 == 0) {System.out.println("num = " + num);}try {Thread.sleep(10);} catch (InterruptedException e) {//...}}}public static void main(String[] args) throws InterruptedException {VolatileWrong volatileWrong = new VolatileWrong();Thread thread = new Thread(volatileWrong);thread.start();Thread.sleep(2345);System.out.println("开始停止线程...");volatileWrong.canceled = true;}}
num = 100num = 200开始停止线程...Process finished with exit code 0
不可以停止
/*** 看似可行的一个用volatile关键字案例 二* 阻塞时,volatile时无法停止线程的* 实现一个生产者很快消费者很慢的案例** @author yiren*/public class VolatileWrongCantStop {public static void main(String[] args) throws InterruptedException {BlockingQueue storage = new ArrayBlockingQueue(10);Producer producer = new Producer(storage);Thread producerThread = new Thread(producer);producerThread.start();Thread.sleep(1000);Consumer consumer = new Consumer(storage);while (consumer.needMore()) {System.out.println(consumer.storage.take() + " 被消费了");Thread.sleep(200);}System.out.println("consumer 不需要数据了");producer.canceled = true;}static class Producer implements Runnable {BlockingQueue<Integer> storage;public volatile boolean canceled = false;public Producer(BlockingQueue storage) {this.storage = storage;}@Overridepublic void run() {int num = 0;try {while (!canceled) {num++;if (num % 100 == 0) {System.out.println("num = " + num);storage.put(num);}Thread.sleep(1);}} catch (InterruptedException e) {System.out.println("??");//...} finally {System.out.println("Provider end!");}}}static class Consumer {BlockingQueue<Integer> storage;public Consumer(BlockingQueue<Integer> storage) {this.storage = storage;}public boolean needMore() {return Math.random() < 0.9;}}}
volatile用于停止线程,如果遇到线程阻塞时,是无法停止线程的。如上案例二,运行过后Consumer已经发出信号停止线程,但是由于我们的BlockingQueue满了,停在了storage.put(num);方法上中,所以finally中的输出语句始终没有出现,程序也没有停止。我们可以看到上面的put方法是抛出了
InterruptedException的,所以我们可以利用异常处理来实现。如下代码:/*** 看似可行的一个用volatile关键字案例 二 使用interrupted修复问题** @author yiren*/public class VolatileWrongCantStopFix {public static void main(String[] args) throws InterruptedException {BlockingQueue storage = new ArrayBlockingQueue(10);Producer producer = new Producer(storage);Thread producerThread = new Thread(producer);producerThread.start();Thread.sleep(1000);Consumer consumer = new Consumer(storage);while (consumer.needMore()) {System.out.println(consumer.storage.take() + " 被消费了");Thread.sleep(200);}System.out.println("consumer 不需要数据了");producerThread.interrupt();}static class Producer implements Runnable {BlockingQueue<Integer> storage;public Producer(BlockingQueue storage) {this.storage = storage;}@Overridepublic void run() {int num = 0;try {while (true) {num++;if (num % 100 == 0) {System.out.println("num = " + num);storage.put(num);}Thread.sleep(1);}} catch (InterruptedException e) {System.out.println("interrupt !!!");//...} finally {System.out.println("Provider end!");}}}static class Consumer {BlockingQueue<Integer> storage;public Consumer(BlockingQueue<Integer> storage) {this.storage = storage;}public boolean needMore() {return Math.random() < 0.9;}}}
1.4.3 关键方法源码
interrupt()方法- 该方法很简单,里面并没有直接处理中断的代码,而是调用了
native方法interrupt0() interrupt0()它在JVM中实际是调用系统的方法public void interrupt() {if (this != Thread.currentThread())checkAccess();synchronized (blockerLock) {Interruptible b = blocker;if (b != null) {interrupt0(); // Just to set the interrupt flagb.interrupt(this);return;}}interrupt0();}private native void interrupt0();
- 该方法很简单,里面并没有直接处理中断的代码,而是调用了
isInterruped()方法该方法返回中断状态,并清除中断,设置为
falsepublic boolean isInterrupted() {return isInterrupted(false);}private native boolean isInterrupted(boolean ClearInterrupted);
Thread#interrupted()未捕获异常处理
UncaughtException使用UncaughtExceptionHandler处理
- 为什么要使用
UncaughtExceptionHandler来处理?- 主线程可以轻松发现异常,而子线程却不行 ```java /**
- 单线程抛出处理有异常堆栈
- 而多线程,子线程发生异常有什么不同? *
- @author yiren
*/
public class ExceptionInChild {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
}); thread.start(); for (int i = 0; i < 5; i++) {throw new RuntimeException();
} } }System.out.println(Thread.currentThread().getName() + ": " +i);
Exception in thread “Thread-0” java.lang.RuntimeException at com.imyiren.concurrency.thread.uncaughtexception.ExceptionInChild.lambda$main$0(ExceptionInChild.java:14) at java.lang.Thread.run(Thread.java:748) main: 0 main: 1 main: 2 main: 3 main: 4
Process finished with exit code 0
- 由上可看出,子线程报错,丝毫不印象主线程的执行。- 子线程的异常无法用传统的方法捕获
/**
- 不加try-catch 抛出四个异常
- 加了try-catch 期望捕获第一个线程的异常,线程234应该不运行,希望看到CaughtException
- 执行时发现,根本没有CaughtException,线程234依旧运行并抛出异常 *
@author yiren */ public class CantCatchDirectly { public static void main(String[] args) throws InterruptedException {
Runnable runnable = () -> {throw new RuntimeException();};Thread thread1 = new Thread(runnable);Thread thread2 = new Thread(runnable);Thread thread3 = new Thread(runnable);Thread thread4 = new Thread(runnable);try {thread1.start();Thread.sleep(200);thread2.start();Thread.sleep(200);thread3.start();Thread.sleep(200);thread4.start();} catch (RuntimeException e) {System.out.println("caught exception");}
} }
Exception in thread “Thread-0” java.lang.RuntimeException at com.imyiren.concurrency.thread.uncaughtexception.CantCatchDirectly.lambda$main$0(CantCatchDirectly.java:15) at java.lang.Thread.run(Thread.java:748) Exception in thread “Thread-1” java.lang.RuntimeException at com.imyiren.concurrency.thread.uncaughtexception.CantCatchDirectly.lambda$main$0(CantCatchDirectly.java:15) at java.lang.Thread.run(Thread.java:748) Exception in thread “Thread-2” java.lang.RuntimeException at com.imyiren.concurrency.thread.uncaughtexception.CantCatchDirectly.lambda$main$0(CantCatchDirectly.java:15) at java.lang.Thread.run(Thread.java:748) Exception in thread “Thread-3” java.lang.RuntimeException at com.imyiren.concurrency.thread.uncaughtexception.CantCatchDirectly.lambda$main$0(CantCatchDirectly.java:15) at java.lang.Thread.run(Thread.java:748)
Process finished with exit code 0
- 如上,无法用传统的方法来捕获异常信息。- `try-catch`是针对主线程的,而不是针对子线程的。`throw new RuntimeException()`是运行在子线程的。2. 解决上面的主线程无法捕获的问题:- 方案一(不推荐):在`run()`方法中进行`try-catch````sql/*** 方案一:在run方法中try-catch* @author yiren*/public class CatchExceptionInRun {public static void main(String[] args) {new Thread(() -> {try {throw new RuntimeException();} catch (RuntimeException e) {System.out.println("Caught Exception ...");}}).start();}}
Caught Exception ...Process finished with exit code 0
方案二:利用
UncaughtExceptionHandler接口处理- 先看下线程异常处理器的调用策略 在
ThreadGroup类中 - 它会检查是否有父线程,如果父线程不为空就一直向上找到最顶层。
- 如果没有,那就尝试获取默认的异常处理器。如果取到的实现不为空,那就调用实现的处理方式,如果为空那就打印异常堆栈信息。
- 从上面的案例可知 没有实现的时候是直接打印异常堆栈。
public void uncaughtException(Thread t, Throwable e) {if (parent != null) {parent.uncaughtException(t, e);} else {Thread.UncaughtExceptionHandler ueh =Thread.getDefaultUncaughtExceptionHandler();if (ueh != null) {ueh.uncaughtException(t, e);} else if (!(e instanceof ThreadDeath)) {System.err.print("Exception in thread \""+ t.getName() + "\" ");e.printStackTrace(System.err);}}}
给程序统一设置
- 首先自定义一个
Handler```java /**
- 自定义异常Handler
- @author yiren */ public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
private String name;
public MyUncaughtExceptionHandler(String name) { this.name = name; }
@Override public void uncaughtException(Thread t, Throwable e) { Logger logger = Logger.getAnonymousLogger(); logger.log(Level.WARNING, name + “caught thread exception : “ + t.getName()); } }
- 然后设置默认处理器```java/*** 使用自定义的handler* @author yiren*/public class CatchByOwnUncaughtExceptionHandler {public static void main(String[] args) throws InterruptedException {Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler("catch-handler"));Runnable runnable = () -> {throw new RuntimeException();};Thread thread1 = new Thread(runnable);Thread thread2 = new Thread(runnable);thread1.start();Thread.sleep(200);thread2.start();}}
``` 二月 12, 2020 2:09:20 下午 com.imyiren.concurrency.thread.uncaughtexception.MyUncaughtExceptionHandler uncaughtException 警告: catch-handlercaught thread exception : Thread-0 二月 12, 2020 2:09:20 下午 com.imyiren.concurrency.thread.uncaughtexception.MyUncaughtExceptionHandler uncaughtException 警告: catch-handlercaught thread exception : Thread-1 二月 12, 2020 2:09:21 下午 com.imyiren.concurrency.thread.uncaughtexception.MyUncaughtExceptionHandler uncaughtException 警告: catch-handlercaught thread exception : Thread-2 二月 12, 2020 2:09:21 下午 com.imyiren.concurrency.thread.uncaughtexception.MyUncaughtExceptionHandler uncaughtException 警告: catch-handlercaught thread exception : Thread-3
- 首先自定义一个
- 先看下线程异常处理器的调用策略 在
Process finished with exit code 0
- 如上可以看到 线程异常处理是使用我们自定义的处理器。1. 可以给每个线程单独设置- 可以通过`thread.setUncaughtExceptionHandler(handler)`设置2. 给线程池设置- 可以通过`ThreadPoolExecutor`来处理<a name="B3Mop"></a>### 1.4 yield方法详解- 释放当前CPU占用,状态依旧是RUNNABLE- JVM不保证遵循yield,如CPU资源不紧张,极端点没有线程使用,即使调用yield也有可能不释放CPU资源- 与sleep的区别:是否可以随时再次被调度<a name="WkR4m"></a>### 1.5 `Thread.currentThread()方法`- 主要是返回当前线程的引用。```sql/*** 打印main thread-0 thread-1* @author yiren*/public class CurrentThread {public static void main(String[] args) {Runnable runnable = () -> System.out.println(Thread.currentThread().getName());// 主线程直接调用函数runnable.run();new Thread(runnable).start();new Thread(runnable).start();}}
2. Object
2.1. wait,notify,notifyAll方法详解
2.1.1 wait-notify用法
- 我们创建两个线程类,用一个
object对象加锁,然后一个线程调用object.wati(),另一个调用object.notify(),且wait先执行。 - 先看一段代码和结果
/*** wait和notify的基本用法* 1. 代码的执行顺序* 2. wait释放锁** @author yiren*/public class Wait {private final static Object object = new Object();static class ThreadOne extends Thread {@Overridepublic void run() {synchronized (object) {try {System.out.println(Thread.currentThread().getName() + " in run before wait");object.wait();} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + " in run after wait");}}}static class ThreadTwo extends Thread {@Overridepublic void run() {synchronized (object) {System.out.println(Thread.currentThread().getName() + " in run before notify");object.notify();System.out.println(Thread.currentThread().getName() + " in run after notify");}}}public static void main(String[] args) throws InterruptedException {ThreadOne threadOne = new ThreadOne();ThreadTwo threadTwo = new ThreadTwo();threadOne.start();Thread.sleep(100);threadTwo.start();}}
Thread-0 in run before waitThread-1 in run before notifyThread-1 in run after notifyThread-0 in run after waitProcess finished with exit code 0
- 执行顺序如上结果,执行解释如下
Thread-0先进入执行,然后wait()进入等待唤醒的WAITING状态,并释放锁。- Thread-1后进入执行,发现加锁了,然后等待
Thread-0释放锁过后调用notify()通知Thread-0不用等了,不过此时由于Thread-1持有了object的锁,所以Thread-1先执行完毕后释放锁,然后Thread-0再拿到锁,把wait()后面的代码执行完毕。
2.1.2 wait-notifyAll 用法
- 创建三个线程,两个线程wait,然后用第三个线程调用notifyAll唤醒
- 代码和即如果如下
/*** 三个线程 2个被wait阻塞,另一个来唤醒他们** @author yiren*/public class WaitNotifyAll implements Runnable {private static final Object objectOne = new Object();@Overridepublic void run() {synchronized (objectOne) {Thread currentThread = Thread.currentThread();System.out.println(currentThread.getName() + " in run before wait, state is " + currentThread.getState());try {objectOne.wait();} catch (InterruptedException e) {e.printStackTrace();}System.out.println(currentThread.getName() + " in run after wait, state is " + currentThread.getState());}}public static void main(String[] args) throws InterruptedException {WaitNotifyAll waitNotifyAll = new WaitNotifyAll();Thread threadOne = new Thread(waitNotifyAll,"thread-one");Thread threadTwo = new Thread(waitNotifyAll,"thread-two");Thread threadThree = new Thread(() -> {synchronized (objectOne) {Thread currentThread = Thread.currentThread();System.out.println(currentThread.getName() + " in run before notifyAll, state is " + currentThread.getState());objectOne.notifyAll();System.out.println(currentThread.getName() + " in run after notifyAll, state is " + currentThread.getState());}}, "thread-three");threadOne.start();threadTwo.start();Thread.sleep(200);threadThree.start();}}
thread-one in run before wait, state is RUNNABLEthread-two in run before wait, state is RUNNABLEthread-three in run before notifyAll, state is RUNNABLEthread-three in run after notifyAll, state is RUNNABLEthread-two in run after wait, state is RUNNABLEthread-one in run after wait, state is RUNNABLEProcess finished with exit code 0
- 线程1和2分别先后进入到WAITING状态后释放锁,
线程3进入run方法后调用notifyAll唤醒,执行完毕run方法 释放锁,线程1和2抢占锁然后执行wait方法后面的代码。
2.1.3 wait释放锁
我们在使用wait的时候,它只会释放它的那把锁,代码入下:
/*** 证明wait 只释放当前的那把锁** @author yiren*/public class WaitNotifyReleaseOwnMonitor {private static final Object objectOne = new Object();private static final Object objectTwo = new Object();public static void main(String[] args) throws InterruptedException {Thread threadOne = new Thread(() -> {synchronized (objectOne) {System.out.println(Thread.currentThread().getName() + " got objectOne lock ");synchronized (objectTwo) {System.out.println(Thread.currentThread().getName() + " got objectTwo lock ");try {System.out.println(Thread.currentThread().getName() + " release objectOne lock ");objectOne.wait();} catch (InterruptedException e) {e.printStackTrace();}}}});Thread threadTwo = new Thread(() -> {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}synchronized (objectOne) {System.out.println(Thread.currentThread().getName() + " got lock objectOne");synchronized (objectTwo) {System.out.println(Thread.currentThread().getName() + " got lock objectTwo");}}});threadOne.start();threadTwo.start();}}
Thread-0 got objectOne lockThread-0 got objectTwo lockThread-0 release objectOne lockThread-1 got lock objectOne
- 注意上面的运行并没有结束。因为两个线程都还没有执行完毕。
2.1.4 wait、notify、notifyAll特点和性质
- 使用时必须先拥有monitor,也就是获取到这个对象的锁
- notify只唤醒一个,取决于JVM。notifyAll则是唤醒全部。
- 都是数据Object的对象的方法,且都是final修饰的native方法。
- 类似功能的有一个Condition对象
- 如果线程同时持有多把锁一定要注意释放顺序,不然容易产生死锁。
2.1.5 生产者消费者模式实现
/*** 用wait notify实现生产者消费者模式* @author yiren*/public class ProducerConsumer {public static void main(String[] args) {EventStorage storage = new EventStorage();Thread producerThread = new Thread(new Producer(storage));Thread consumerThread = new Thread(new Consumer(storage));producerThread.start();consumerThread.start();}private static class Producer implements Runnable{EventStorage storage;public Producer(EventStorage storage) {this.storage = storage;}@Overridepublic void run() {for (int i = 0; i < 100; i++) {storage.put();}}}private static class Consumer implements Runnable{EventStorage storage;public Consumer(EventStorage storage) {this.storage = storage;}@Overridepublic void run() {for (int i = 0; i < 100; i++) {storage.take();}}}private static class EventStorage {private int maxSize;private LinkedList<LocalDateTime> storage;public EventStorage() {maxSize = 10;storage = new LinkedList<>();}public synchronized void put() {while (storage.size() == maxSize) {try {wait();} catch (InterruptedException e) {e.printStackTrace();}}storage.add(LocalDateTime.now());System.out.println("storage has " + storage.size() + " product(s).");notify();}public synchronized void take() {while (storage.size() == 0) {try {wait();} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("get date " + storage.poll() + ", storage has " + storage.size() + " product(s).");notify();}}}
storage has 1 product(s).storage has 2 product(s).storage has 3 product(s).storage has 4 product(s).storage has 5 product(s).storage has 6 product(s).storage has 7 product(s).storage has 8 product(s).storage has 9 product(s).storage has 10 product(s).get date 2020-02-11T15:46:43.554, storage has 9 product(s).get date 2020-02-11T15:46:43.554, storage has 8 product(s).get date 2020-02-11T15:46:43.554, storage has 7 product(s).get date 2020-02-11T15:46:43.554, storage has 6 product(s).get date 2020-02-11T15:46:43.554, storage has 5 product(s).get date 2020-02-11T15:46:43.554, storage has 4 product(s).get date 2020-02-11T15:46:43.554, storage has 3 product(s).get date 2020-02-11T15:46:43.554, storage has 2 product(s).get date 2020-02-11T15:46:43.554, storage has 1 product(s).get date 2020-02-11T15:46:43.555, storage has 0 product(s).storage has 1 product(s).storage has 2 product(s).storage has 3 product(s).storage has 4 product(s).get date 2020-02-11T15:46:43.555, storage has 3 product(s).get date 2020-02-11T15:46:43.555, storage has 2 product(s).get date 2020-02-11T15:46:43.555, storage has 1 product(s).get date 2020-02-11T15:46:43.555, storage has 0 product(s).
2.2. Sleep方法详解
- 让线程在预期的时间执行,其他事件不要占用CPU资源
wait()会释放锁,但是sleep()方法不释放锁,包括synchronized和lock
2.2.1 sleep不释放锁
synchronized/*** sleep不释放锁* @author yiren*/public class SleepDontReleaseMonitor {public static void main(String[] args) {final Object object = new Object();Runnable runnable = new Runnable() {@Overridepublic void run() {synchronized (object) {System.out.println(Thread.currentThread().getName() + " into synchronized !");try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + " out to synchronized !");}}};Thread thread1 = new Thread(runnable);Thread thread2 = new Thread(runnable);thread1.start();thread2.start();}}
Thread-0 into synchronized !Thread-0 out to synchronized !Thread-1 into synchronized !Thread-1 out to synchronized !Process finished with exit code 0
Lock
public class SleepDontReleaseLock {private static final Lock LOCK = new ReentrantLock();public static void main(String[] args) {Runnable runnable = new Runnable() {@Overridepublic void run() {LOCK.lock();try {System.out.println(Thread.currentThread().getName() + " into LOCK !");Thread.sleep(3000);System.out.println(Thread.currentThread().getName() + " out to LOCK !");} catch (InterruptedException e) {e.printStackTrace();} finally {LOCK.unlock();}}};Thread thread1 = new Thread(runnable);Thread thread2 = new Thread(runnable);thread1.start();thread2.start();}}
Thread-0 into LOCK !Thread-0 out to LOCK !Thread-1 into LOCK !Thread-1 out to LOCK !Process finished with exit code 0
2.2.2 响应中断
- 在调用时,会抛出InterruptedException,并且清除中断状态
/*** sleep响应中断案例* Thread.sleep()* TimeUnit.SECONDS.sleep()** @author yiren*/public class SleepInterrupted {public static void main(String[] args) throws InterruptedException {Thread thread = new Thread(() -> {for (int i = 0; i < 10; i++) {System.out.println(Thread.currentThread().getName() + ": " + LocalDateTime.now());try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {System.out.println(Thread.currentThread().getName() + ": was interrupted!");}}});thread.start();Thread.sleep(3500);thread.interrupt();}}
sleep(time)可以通过TimeUnit.时间单位.sleep(time)调用;此方法优于Thread.sleep(time)我们可以看下它的源码,它做了一个大于零的判断,以免传入负数,而Thread.sleep(time)中如果传入负数则会报IllegalArgumentException错误。public void sleep(long timeout) throws InterruptedException {if (timeout > 0) {long ms = toMillis(timeout);int ns = excessNanos(timeout, ms);Thread.sleep(ms, ns);}}
2.2.3 一句话总结
sleep(time)方法可以让线程进入到WAITING状态,并停止占用CPU资源,但是不释放锁,直到规定事件后再执行,休眠期间如果被中断,会抛出异常并清除中断状态。2.3. join方法详解
2.3.1 作用及用法
作用:新线程加入,所以要等待它执行完再出发
- 用法:main等待thread1、thread2等线程执行完毕
- 普通用法
``` start to wait child threads. Thread-0 was finished! Thread-1 was finished! all threads run completed!/*** 普通用法* @author yiren*/public class JoinSimple {public static void main(String[] args) throws InterruptedException {Runnable runnable = () -> {try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + " was finished!");};Thread thread1 = new Thread(runnable);Thread thread2 = new Thread(runnable);thread1.start();thread2.start();System.out.println("start to wait child threads.");thread1.join();thread2.join();System.out.println("all threads run completed!");}}
Process finished with exit code 0
- 如果两个线程不join的话就会先打印最后一句话。2. 中断- `thread.join()`响应的中断是执行join方法的这个线程的中断 而不是thread,就如下方代码,中断的是主线程。```java/*** 响应中断* @author yiren*/public class JoinInterrupt {public static void main(String[] args) {final Thread mainThread = Thread.currentThread();Runnable runnable = () -> {try {mainThread.interrupt();TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + " was finished!");};Thread thread1 = new Thread(runnable);thread1.start();System.out.println("start to wait child thread.");try {thread1.join();} catch (InterruptedException e) {// 实际是主线程中断System.out.println(Thread.currentThread().getName() + " was interrupted!");e.printStackTrace();}}}
start to wait child thread.main was interrupted!java.lang.InterruptedExceptionat java.lang.Object.wait(Native Method)at java.lang.Thread.join(Thread.java:1252)at java.lang.Thread.join(Thread.java:1326)at com.imyiren.concurrency.thread.method.JoinInterrupt.main(JoinInterrupt.java:25)Thread-0 was finished!Process finished with exit code 0
- join期间线程状态
```sql waiting child thread main thread state: WAITING Thread-0 finished completed child thread/*** join发生后 主线程的状态* @author yiren*/public class JoinState {public static void main(String[] args) {Thread mainThread = Thread.currentThread();Thread thread = new Thread(() -> {try {TimeUnit.SECONDS.sleep(3);System.out.println("main thread state: " + mainThread.getState());System.out.println(Thread.currentThread().getName() + " finished");} catch (InterruptedException e) {e.printStackTrace();}});thread.start();try {System.out.println("waiting child thread");thread.join();System.out.println("completed child thread");} catch (InterruptedException e) {e.printStackTrace();}}}
Process finished with exit code 0
<a name="kFZgV"></a>#### 2.3.2 join源码分析```javapublic final void join() throws InterruptedException {join(0);}public final synchronized void join(long millis) throws InterruptedException {long base = System.currentTimeMillis();long now = 0;if (millis < 0) {throw new IllegalArgumentException("timeout value is negative");}if (millis == 0) {while (isAlive()) {wait(0);}} else {while (isAlive()) {long delay = millis - now;if (delay <= 0) {break;}wait(delay);now = System.currentTimeMillis() - base;}}}
join(0)是代表无限期等待- 它的根本方法就是调用的
wait(time) - 但是没有notify?其实是JVM的Thread执行完毕会自动执行一次notifyAll。
- 既然知道它是通过wait-notify实现的,那么我们可以写一下等价的代码:
/*** 自己实现 等价代码* @author yiren*/public class JoinImplements {public static void main(String[] args) throws InterruptedException {Runnable runnable = () -> {try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + " was finished!");};Thread thread1 = new Thread(runnable);thread1.start();System.out.println("start to wait child threads.");// thread1.join(); // 等价于下方代码synchronized (thread1) {thread1.wait();}System.out.println("all threads run completed!");}}
2.3.3 常见面试题
在join期间线程会处于那种状态?
什么时候我们需要设置守护线程?
- 我们应该如何应用线程优先级来帮助程序运行?有哪些禁忌?
不同的操作系统如何处理优先级问题?
一个线程两次调用start方法会出现什么情况?为什么?
- start方法会间接调用run方法,为什么我们不直接调用run方法?
- 为什么线程通信的方法wait(),notify()和notifyAl()被定义在Object类里面?而sleep却定义在Thread类中
- 用三种方法实现生产者模式
join和sleep和wait期间线程的状态分别是什么?为什么?
Java异常体系
- 实际工作中,如何处理全局异常?为什么要处理全局异常?不处理行不行?
- run方法是否可以抛出异常?如果抛出异常,线程状态会怎么样?
-
3.2 Object相关面试题
两个线程交替打印0-100的奇偶数
- 手写生产者消费者设计模式 (前面有代码)
- 为什么wait()需要在同步代码块中实现,而sleep不需要
- wait设计是针对多个线程的,如果多个线程运行的时候,在执行wait前就切换到了另外一个线程,恰好把notify执行掉了,那么就会形成死锁。
- 而sleep则是针对单个线程本身,不涉及到其他线程。
- 为什么线程通信的方法wait(),notify()和notifyAll()被定义在Object类里面?而Sleep定义在Thread类里面?
wait(),notify(),notifyAll()属于锁级别的操作,而锁一般是针对某个对象的,所以就定义在了Object中。每一个对象,在对象头中,都是有几位来表示当前锁的状态的,所以这个锁是绑定到某个对象上面,而并不是线程中。- 如果把这些方法放在了Thread中,那么如果一个线程中持有了多把锁,就没有办法灵活的实现这样的多锁逻辑,也会增加编程难度。
- wait()方法属于Object对象,那如果调用Thread.wait方法会怎样?
- 对于Thread类特别特殊,因为在JVM中,线程在退出的现实中,它会自己去执行notify,这样会使我们设计的程序受到干扰。
- 如何选择用notify和notifyAll
- 主要考虑是我们需要唤醒的是单个线程还是多个线程
- notifyAll之后所有的线程都会再次抢夺锁,如果某线程抢夺锁失败怎么办?
- notifyAll线程执行完同步块中代码后,其他线程会同时竞争这把锁,只有一个线程会竞争成功,其他线程会进入到WAITING状态,等待竞争成功的这个线程执行结束再次竞争锁
- 能不能用suspend()和resume()来阻塞线程?为什么?
- Java官方是不推荐使用suspend来阻塞线程的,并且两个方法以及注明了过时,推荐使用wait-notify来实现
- wait/notify、sleep异同
- 思路:方法属于哪个对象?线程状态怎么切换。
- 相同:都进入阻塞,都响应中断
- 不同:wait/notify需要同步块,sleep不需要;wait释放锁,sleep不释放;wait可不指定时间,sleep必须指定;所属类不同
-
3.x 较长的答案
两个线程交替打印0-100的奇偶数
用synchronized来实现
/*** 两个线程交替打印0-100* @author yiren*/public class OddEvenBySync {/*两个线程1. 一个处理偶数(Even),另一个处理奇数(Odd) 用位运算来实现判断2. 用synchronized 来实现*/private static volatile int count = 0;private static final Object lock = new Object();public static void main(String[] args) {Thread threadEven = new Thread(() -> {while (count < 100) {synchronized (lock) {// if (count % 2 == 0) {if (0 == (count & 1)) {System.out.println(Thread.currentThread().getName() + ": " + count++);}}}}, "thread-even");Thread threadOdd = new Thread(() -> {while (count < 100) {synchronized (lock) {// if (count % 2 == 0) {if (1 == (count & 1)) {System.out.println(Thread.currentThread().getName() + ": " + count++);}}}}, "thread-odd");threadEven.start();threadOdd.start();}}
用wait-notify实现
/*** 使用wait-notify 实现奇偶打印* @author yiren*/public class OddEvenByWaitNotify {private static final Object lock = new Object();private static int count = 0;private static final int MAX_COUNT = 100;public static void main(String[] args) {Runnable runnable = new Runnable() {@Overridepublic void run() {while (count <= MAX_COUNT ) {synchronized (lock) {try {System.out.println(Thread.currentThread().getName() + ": " + count++);lock.notify();// 如果任务还没结束 就让出锁 自己休眠lock.wait();} catch (InterruptedException e) {e.printStackTrace();}}}}};Thread thread1 = new Thread(runnable);Thread thread2 = new Thread(runnable);thread1.start();thread2.start();}}
