Java开发手册!mysql死锁的原因和处理方法

本文探讨了Java开发中涉及的线程同步资源管理,包括悲观锁和乐观锁的概念与区别,以及CAS算法的工作原理和潜在问题。通过对锁的深入理解,分析了死锁产生的原因,并提出了相应的处理方法,旨在帮助Java后端开发者优化并发性能。
摘要由CSDN通过智能技术生成

线程是否要锁住同步资源

  • 锁住 悲观锁
  • 不锁住 乐观锁

锁住同步资源失败 线程是否要阻塞

  • 阻塞
  • 不阻塞自旋锁,适应性自旋锁

多个线程竞争同步资源的流程细节有没有区别

  • 不锁住资源,多个线程只有一个能修改资源成功,其它线程会重试无锁
  • 同一个线程执行同步资源时自动获取资源偏向锁
  • 多个线程竞争同步资源时,没有获取资源的线程自旋等待锁释放 轻量级锁
  • 多个线程竞争同步资源时,没有获取资源的线程阻塞等待唤醒 重量级锁

4.多个线程竞争锁时是否要排队

  • 排队公平锁
  • 先尝试插队,插队失败在排队非公平锁

一个线程的多个流程能不能获取同一把锁

  • 能 可重入锁
  • 不能非可重入锁

多个线程能不能共享一把锁

  • 能 共享
  • 不能排他锁

悲观锁与乐观锁

悲观锁与乐观锁时一种广义的概念,体现的是看待线程同步的不同角度。

悲观锁

悲观锁认为自己在使用数据的时候一定有别的线程来修改数据,在获取数据的时候会先加锁,确保数据不会被别的线程修改。 锁实现:synchronized 接口Lock的实现类 适用场景:写操作多,先加锁可以保证写操作时数据正确。

乐观锁

<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Paperback: 1224 pages Data: September 8, 2008 Description: The unexpected pleasure of reading books about databases is that they are often written by authors with highly organized minds. Paul DuBois and his editors at New Riders have assembled MySQL with a clarity and lucidity that inspires confidence in the subject matter: a (nearly) freely redistributable SQL-interpreting database client/server primarily geared for Unix systems but maintained for Windows platforms as well. What isn't "free" about MySQL (the application) is its server's commercial use; all clients and noncommercial server use are free. DuBois's tome isn't free either, but its list price is modest in light of its value and the value of its namesake. The volume is superbly organized into 12 chapters and 10 appendices and contains a concise table of contents and a comprehensive 50-page index. It is peppered with references to the online HTML documentation that comes with the source and binary distributions (which are available and easy to install in stable rpm and tar releases.) The first third of MySQL is an excellent instruction tool for database newbies; the second third is a detailed reference for MySQL developers; and the last third consists of clearly annotated appendices, including C, Perl (but not Python), and PHP interfaces. Perhaps as an indication of the collective will of the developers of MySQL, DuBois does not separate Windows 95/98/NT design or development specifics from its main discussions. Platform-independent design is a goal, not a reality, and users will have to rely on newsgroups and mailing lists for details. Moreover, security issues are addressed in a mere 18 pages, a large part of which is devoted to standard Unix file and network-access permissions. Next to nothing is mentioned about defense against common hacking strategies, the use of secure shell interfaces, or access encryption. Although it is nearly 800 pages in length, DuBois's book is thankfully not encyclopedic. It is a valuable précis of the MySQL database, and its easy-to-skim look and feel will make it an excellent browse for database experts who want to know what is and is not possible within MySQL, the application.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值