1.死锁(Deadlock)
==================
什么是死锁
死锁是当线程进入无限期等待状态时发生的情况,因为所请求的锁被另一个线程持有,而另一个线程又等待第一个线程持有的另一个锁 导致互相等待。总结:多个线程互相等待对方释放锁。
例如在现实中的十字路口,锁就像红绿灯指示器,一旦锁坏了,就会导致交通瘫痪。
那么该如何避免这个问题呢
死锁的解决和预防
============
1.超时释放锁
顾名思义,这种避免死锁的方式是在尝试获取锁的时候加一个超时时间,这就意味着,如果一个线程在获取锁的门口等待太久这个线程就会放弃这次请求,退还并释放所有已经获得的锁,再在等待一段随机时间后再次尝试,这段时间其他的线程伙伴可以去尝试拿锁.
public interface Lock {
//自定义异常类
public static class TimeOutException extends Exception{
public TimeOutException(String message){
super(message);
}
}
//无超时锁,可以被打断
void lock() throws InterruptedException;
//超时锁,可以被打断
void lock(long molls) throws InterruptedException,TimeOutException;
//解锁
void unlock();
//获取当前等待的线程
Collection getBlockedThread();
//获取当前阻塞的线程数目
int getBlockSize();
}
public class BooleanLock implements Lock{
private boolean initValue;
private Thread currenThread;
public BooleanLock(){
this.initValue = false;
}
private Collection blockThreadCollection = new ArrayList<>();
@Override
public synchronized void lock() throws InterruptedException {
while (initValue){
blockThreadCollection.add(Thread.currentThread());
this.wait();
}
//表明此时正在用,别人进来就要锁住
this.initValue = true;
currenThread = Thread.currentThread();
blockThreadCollection.remove(Thread.currentThread());//从集合中删除
}
@Override
public synchronized void lock(long mills) throws InterruptedException, TimeOutException {
if (mills<=0){
lock();
}else {
long hasRemain = mills;
long endTime = System.currentTimeMillis()+mills;
while (initValue){
if (hasRemain<=0)
throw new TimeOutException(“Time out”);
blockThreadCollection.add(Thread.currentThread());
hasRemain = endTime-System.currentTimeMillis();
}
this.initValue = true;
currenThread = Thread.currentThread();
}
}
@Override
public synchronized void unlock() {
if (currenThread==Thread.currentThread()){
this.initValue = false; //表明锁已经释放
Optional.of(Thread.currentThread().getName()+ " release the lock monitor").ifPresent(System.out::println);
this.notifyAll();
}
}
@Override
public Collection getBlockedThread() {
return Collections.unmodifiableCollection(blockThreadCollection);
}
@Override
public int getBlockSize() {
return blockThreadCollection.size();
}
}
public class BlockTest {
public static void main(String[] args) throws InterruptedException {
final BooleanLock booleanLock = new BooleanLock();
// 使用Stream流的方式创建四个线程
Stream.of(“T1”,“T2”,“T3”,“T4”).forEach(name->{
new Thread(()->{
try {
booleanLock.lock(10);
Optional.of(Thread.currentThread().getName()+" have the lock Monitor").ifPresent(System.out::println);
work();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Lock.TimeOutException e) {
Optional.of(Thread.currentThread().getName()+" time out").ifPresent(System.out::println);
} finally {
booleanLock.unlock();
}
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
最后的话
无论是哪家公司,都很重视Spring框架技术,重视基础,所以千万别小看任何知识。面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。
同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,好了希望这篇文章对大家有帮助!
部分截图:
有帮助,可以添加下面V无偿领取!(备注Java)**
[外链图片转存中…(img-xKcjIjjN-1711053838122)]
最后的话
无论是哪家公司,都很重视Spring框架技术,重视基础,所以千万别小看任何知识。面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。
同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,好了希望这篇文章对大家有帮助!
部分截图:
[外链图片转存中…(img-lmCxDid5-1711053838123)]