当前位置: 首页 > news >正文

重庆高铁建设网站上海关键词排名提升

重庆高铁建设网站,上海关键词排名提升,在招聘网站做销售,wordpress判断分类死锁#xff0c;一组互相竞争的资源的线程之间相互等待#xff0c;导致永久阻塞的现象。 如下图所示#xff1a; 与死锁对应的#xff0c;还有活锁#xff0c;是指线程没有出现阻塞#xff0c;但是无限循环。 有一个经典的银行转账例子如下#xff1a; 我们有个账户类…死锁一组互相竞争的资源的线程之间相互等待导致永久阻塞的现象。 如下图所示 与死锁对应的还有活锁是指线程没有出现阻塞但是无限循环。 有一个经典的银行转账例子如下 我们有个账户类其中有两个属性账户名和余额。 它有两个方法转入、转出。 public class Account {private String countName;private int balance;public Account(String countName, int balance) {this.countName countName;this.balance balance;}/*** 转出金额更新转出方余额金额减少* param amount*/public void debit(int amount){this.balance - amount;}/*** 存入金额更新转入方余额,金额增多* param amount*/public void credit(int amount){this.balance amount;}public String getCountName() {return countName;}public void setCountName(String countName) {this.countName countName;}public int getBalance() {return balance;}public void setBalance(int balance) {this.balance balance;} }还有一个转账操作类它有三个属性转入账户、转出账户、转账金额。 它实现了Runnable接口run方法中是转账逻辑代码。 我们使用 while(true) 让他不停的进行转账操作如果账户余额大于转账金额就让转出账号减少amount转入账户增加amount。打印出线程名、金额转移方向、以及每个账户余额。 我们在main方法中进行测试创建两个转账账户使用两个线程操作这两个账户让他们相互转账。 public class TransferAccount implements Runnable{private Account fromAccount;private Account toAccount;private int amount;public TransferAccount(Account fromAccount, Account toAccount, int amount) {this.fromAccount fromAccount;this.toAccount toAccount;this.amount amount;}Overridepublic void run() {while(true){synchronized (fromAccount){synchronized (toAccount){if(fromAccount.getBalance()amount) {fromAccount.debit(amount);toAccount.credit(amount);}System.out.println(Thread.currentThread().getName());System.out.println(fromAccount.getCountName()-toAccount.getCountName():amount );System.out.println(fromAccount.getCountName() 账户余额: fromAccount.getBalance());System.out.println(toAccount.getCountName() 账户余额: toAccount.getBalance());}}try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}public static void main(String[] args) {Account bigHead new Account(冤大头,100000);Account smallKill new Account(门小抠,200000);new Thread(new TransferAccount(bigHead,smallKill,10)).start();new Thread(new TransferAccount(smallKill,bigHead,20)).start();}}执行main方法后的输出结果 Thread-0 冤大头-门小抠:10 冤大头账户余额:99990 门小抠账户余额:200010 Thread-1 门小抠-冤大头:20 门小抠账户余额:199990 冤大头账户余额:100010 Thread-1 门小抠-冤大头:20 门小抠账户余额:199970 冤大头账户余额:100030 Thread-0 冤大头-门小抠:10 冤大头账户余额:100020 门小抠账户余额:199980 Thread-0 冤大头-门小抠:10 冤大头账户余额:100010 门小抠账户余额:199990 Thread-1 门小抠-冤大头:20 门小抠账户余额:199970 冤大头账户余额:100030 Thread-0 冤大头-门小抠:10 冤大头账户余额:100020 门小抠账户余额:199980 Thread-1 门小抠-冤大头:20 门小抠账户余额:199960 冤大头账户余额:100040 Thread-0 冤大头-门小抠:10 冤大头账户余额:100030 门小抠账户余额:199970 Thread-1 门小抠-冤大头:20 门小抠账户余额:199950 冤大头账户余额:100050 Thread-0 冤大头-门小抠:10 冤大头账户余额:100040 门小抠账户余额:199960 Thread-1 门小抠-冤大头:20 门小抠账户余额:199940 冤大头账户余额:100060 这时我们发现进程还在运行但是控台停止输出了它停在那里了。 截图如下 这时我们使用 jps 来查看一下java进程 D:\open_source\MyBatis\MyThread\target\classes\demojps 23600 24676 Launcher 40244 Jps 23672 TransferAccount然后输入 使用jstack来看详情 D:\open_source\MyBatis\MyThread\target\classes\demojstack 23672 2023-02-26 18:37:57 Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.351-b10 mixed mode):DestroyJavaVM #14 prio5 os_prio0 tid0x000001b4fc785800 nid0x6530 waiting on condition [0x0000000000000000]java.lang.Thread.State: RUNNABLEThread-1 #13 prio5 os_prio0 tid0x000001b4fc77f800 nid0x887c waiting for monitor entry [0x000000467adff000]java.lang.Thread.State: BLOCKED (on object monitor)at deadlock.TransferAccount.run(TransferAccount.java:20)- waiting to lock 0x000000076c5a4610 (a deadlock.Account)- locked 0x000000076c5a4658 (a deadlock.Account)at java.lang.Thread.run(Thread.java:750)Thread-0 #12 prio5 os_prio0 tid0x000001b4fc77c800 nid0x8c80 waiting for monitor entry [0x000000467acff000]java.lang.Thread.State: BLOCKED (on object monitor)at deadlock.TransferAccount.run(TransferAccount.java:20)- waiting to lock 0x000000076c5a4658 (a deadlock.Account)- locked 0x000000076c5a4610 (a deadlock.Account)at java.lang.Thread.run(Thread.java:750)......Found one Java-level deadlock:Thread-1:waiting to lock monitor 0x000001b4fa208eb8 (object 0x000000076c5a4610, a deadlock.Account),which is held by Thread-0 Thread-0:waiting to lock monitor 0x000001b4fa208e08 (object 0x000000076c5a4658, a deadlock.Account),which is held by Thread-1Java stack information for the threads listed above:Thread-1:at deadlock.TransferAccount.run(TransferAccount.java:20)- waiting to lock 0x000000076c5a4610 (a deadlock.Account)- locked 0x000000076c5a4658 (a deadlock.Account)at java.lang.Thread.run(Thread.java:750) Thread-0:at deadlock.TransferAccount.run(TransferAccount.java:20)- waiting to lock 0x000000076c5a4658 (a deadlock.Account)- locked 0x000000076c5a4610 (a deadlock.Account)at java.lang.Thread.run(Thread.java:750)Found 1 deadlock.我们可以看到其中Thread-0和Thread-1都已经处于BLOCK状态发生了死锁。 导致死锁发生必须同时满足四个条件互斥、占有且等待、不可抢占、循环等待。 互斥共享资源A和B只能被一个线程占用。占有且等待线程Thread-1 已经取得共享资源X在等待共享资源Y的时候不释放共享资源X。不可抢占其他线程不能强行抢占线程Thread-1 占有的资源。循环等待线程Thread-1 等待线程Thread-2 占有的资源线程Thread-2等待Thread-1 占有的资源。 如何解决 如果要解决死锁问题只需要破坏其中一个条件使其不满足即可。 除了互斥多线程的基础之外其他三个条件都可以考虑破坏。 实际中遇到死锁只能重启如果还是死锁要定位问题点然后修复代码破坏掉死锁必须满足的条件之一后重新发布。 我们来尝试破坏占有且等待的方式来破坏死锁 新增加一个分配的类Allocator它有一个账户池。每个转账线程中要判断账户池中是否已有此账户如果没有可以转账有的话就不转账。就可以避免共享资源同时存在于两个线程。 import java.util.ArrayList; import java.util.List;public class Allocator {private ListObject list new ArrayList();synchronized boolean apply(Object fromAccount,Object toAccount){if(list.contains(fromAccount)||list.contains(toAccount)){return false;}list.add(fromAccount);list.add(toAccount);return true;}synchronized void free(Object fromAccount,Object toAccount){list.remove(fromAccount);list.remove(toAccount);} }//相应的TransAcount也要做修改 public class TransferAccount implements Runnable {private Account fromAccount;private Account toAccount;private int amount;private Allocator allocator;public TransferAccount(Account fromAccount, Account toAccount, int amount, Allocator allocator) {this.fromAccount fromAccount;this.toAccount toAccount;this.amount amount;this.allocator allocator;}Overridepublic void run() {while (true) {//判断账户是否已经分配过if (allocator.apply(fromAccount, toAccount)) {try {synchronized (fromAccount) {synchronized (toAccount) {if (fromAccount.getBalance() amount) {fromAccount.debit(amount);toAccount.credit(amount);}System.out.println(Thread.currentThread().getName());System.out.println(fromAccount.getCountName() - toAccount.getCountName() : amount);System.out.println(fromAccount.getCountName() 账户余额: fromAccount.getBalance());System.out.println(toAccount.getCountName() 账户余额: toAccount.getBalance());}}try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}finally {allocator.free(fromAccount,toAccount);}}}}public static void main(String[] args) {Account bigHead new Account(冤大头, 100000);Account smallKill new Account(门小抠, 200000);Allocator allocator new Allocator();new Thread(new TransferAccount(bigHead, smallKill, 10, allocator)).start();new Thread(new TransferAccount(smallKill, bigHead, 20, allocator)).start();}}我们来避免第三个条件使用Lock来替换掉 Synchronized。 Synchronized加锁后要等到资源释放而且锁是不可抢占的。 Lock中有个方法 tryLock可以返回布尔值如果返回fasle就不会进去。tryLock不会持续持有锁。 public class TransferAccount2 implements Runnable{private Account fromAccount;private Account toAccount;private int amount;private Lock fromAccountLock new ReentrantLock();private Lock toAccountLock new ReentrantLock();public TransferAccount2(Account fromAccount, Account toAccount, int amount) {this.fromAccount fromAccount;this.toAccount toAccount;this.amount amount;}Overridepublic void run() {while(true){//synchronized (fromAccount){if(fromAccountLock.tryLock()){if(toAccountLock.tryLock()){if(fromAccount.getBalance()amount) {fromAccount.debit(amount);toAccount.credit(amount);}System.out.println(Thread.currentThread().getName());System.out.println(fromAccount.getCountName()-toAccount.getCountName():amount );System.out.println(fromAccount.getCountName() 账户余额: fromAccount.getBalance());System.out.println(toAccount.getCountName() 账户余额: toAccount.getBalance());}}try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}public static void main(String[] args) {Account bigHead new Account(冤大头,100000);Account smallKill new Account(门小抠,200000);new Thread(new TransferAccount2(bigHead,smallKill,10)).start();new Thread(new TransferAccount2(smallKill,bigHead,20)).start();}}
http://www.dnsts.com.cn/news/175543.html

相关文章:

  • 赣州网站制作公司惠州有没有做网站
  • 网站建设实验的总结北京装饰公司排行 2019
  • 南约社区网站建设wordpress后台为什么这么慢
  • WordPress去掉文章摘要西安seo包年服务
  • 宝塔搭建wordpress网站做网站用什么格式的图片
  • 四川省建设厅网站打不开wordpress转为pdf
  • 国外优秀app设计网站有哪些网站开发进度确认单
  • 开发网站如何选需要注意什么问题小程序模板素材
  • 特定ip段访问网站代码沧州网站设计报价
  • 昆明网站建设哪个好网站新闻对百度优化有用吗
  • 抚顺地区网站建设wordpress 如何提交表单
  • 服务器价格购买价格表株洲网站排名优化
  • 网站制作流程分为哪七步微信小程序怎么制作自己的程序
  • wordpress维基主题宁波seo网络推广代理价格
  • wordpress产品列表插件seo代码优化
  • 大兴企业官方网站建设宁波北京网站建设
  • 谷歌网站质量指南平顶山做网站多少钱
  • 传媒网站如何设计在家做网站编辑
  • 做企业网站要不要我们自己提供网站相关的图片?酒店预定网站建设方案
  • 凡科做的网站为什么搜不到北湖区网站建设服务商
  • 腾冲住房和城乡建设局网站商业网点消防规范
  • 做问卷赚钱最好似网站建设银行保定分行网站
  • 域名备案好了后怎么做网站wordpress 502
  • 电商类公司网站应该怎么搭建网站建设平台用乐云践新
  • 网站划分栏目为什么自己做不出一个好网站
  • 公司网站开发项目管理制度程序开发软件有哪些
  • mooc网站建设广告英语
  • 网站开发 需求app推广30元一单
  • 妇联网站建设方案wordpress备案号无显示
  • 企业网站的常见类型有网络营销的功能是什么