本文主要是介绍Android 线程死锁的案例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
模拟java 线程死锁
ExecutorService executorService = Executors.newFixedThreadPool(2);Object lockA =new Object();Object lockB =new Object();executorService.submit(new Runnable() {@Overridepublic void run() {synchronized (lockA){try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}synchronized (lockB){Log.d(TAG, "aaa");}}}});executorService.submit(new Runnable() {@Overridepublic void run() {synchronized (lockB){synchronized (lockA){Log.d(TAG, "bbb");}}}});
我们启动了一个线程A 在里面先拿到LockA 这把锁,然后睡眠2S.
在线程B 里面先拿到LockB 这把锁,然后去拿线程A 的锁。
线程A睡醒之后,去拿B的锁,这时候就会发生死锁。
我们看下dump 出来的线程信息:
线程A
"pool-3-thread-1@12151" prio=5 tid=0x99 nid=NA waiting for monitor entryjava.lang.Thread.State: BLOCKEDblocks pool-3-thread-2@12152waiting for pool-3-thread-2@12152 to release lock on <0x2f7e> (a java.lang.Object)at com.example.fragment.ConcurrentFragment$10.run(ConcurrentFragment.java:180)- locked <0x2f7d> (a java.lang.Object)at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)at java.util.concurrent.FutureTask.run(FutureTask.java:266)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)at java.lang.Thread.run(Thread.java:914)
线程B
"pool-3-thread-2@12152" prio=5 tid=0x9a nid=NA waiting for monitor entryjava.lang.Thread.State: BLOCKEDblocks pool-3-thread-1@12151waiting for pool-3-thread-1@12151 to release lock on <0x2f7d> (a java.lang.Object)at com.example.fragment.ConcurrentFragment$11.run(ConcurrentFragment.java:192)- locked <0x2f7e> (a java.lang.Object)at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)at java.util.concurrent.FutureTask.run(FutureTask.java:266)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)at java.lang.Thread.run(Thread.java:914)
解决方法:
那怎么避免死锁呢?
- 争取一个业务不使用两个以上的锁,只有一个锁,不会发生死锁。
- 使用Lock 接口锁,使用超时锁,或者尝试获取锁
- 使用可重入锁
/**** <p>An implementation can favor responding to an interrupt over normal* method return, or reporting a timeout.** <p>A {@code Lock} implementation may be able to detect* erroneous use of the lock, such as an invocation that would cause* deadlock, and may throw an (unchecked) exception in such circumstances.* The circumstances and the exception type must be documented by that* {@code Lock} implementation.** @param time the maximum time to wait for the lock* @param unit the time unit of the {@code time} argument* @return {@code true} if the lock was acquired and {@code false}* if the waiting time elapsed before the lock was acquired** @throws InterruptedException if the current thread is interrupted* while acquiring the lock (and interruption of lock* acquisition is supported)*/boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
这篇关于Android 线程死锁的案例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!