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

重庆高铁建设网站php网站登录系统怎么做

重庆高铁建设网站,php网站登录系统怎么做,经典网站模板下载,网站用的横幅广告怎么做死锁#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.hkea.cn/news/14301539/

相关文章:

  • 网站动画效果怎么做的网站开发温州
  • 怎么在网站上面做悬浮广告天津网站建设网站推广
  • 申请网站空间就是申请域名注册一家设计公司流程
  • 包头正规旅游网站开发哪家好建设银行兴安支行网站
  • 网站建设ssc源码修复网站设计与管理
  • 论坛网站建设推广优化app开发方式
  • 专业营销网站公司wordpress批量建分类
  • 西安网站建设价格企业logo设计规范
  • 网站开发工程师有证书考试吗手机企业网站如何建设
  • 国外的设计网站做商城网站还要服务器
  • 网站程序员一份完整的商业计划书
  • 怎样找回网站备案密码错误站长工具友链检测
  • 325建筑兼职网东莞seo广告宣传
  • 怎样建设的网站好优化好排名商城网站设计实训总结
  • 网站建设的困难做网站服务商
  • 闵行营销型网站建设公司网站建设与管理好找工作吗
  • 建设一个微商的网站网上营销是做什么的
  • 射阳县住房和城乡建设局网站wordpress左侧菜单怎么添加
  • 百度爱采购官方网站wordpress主题报错
  • 网站建设的经费预算报告wordpress占用服务器内存
  • 电子商务网站 功能网站建设一对一培训
  • 华大 网站建设网络营销服务概念
  • 栾川网站开发外贸网站运营推广
  • 乐清 网站建设企业为什么做网站素材
  • 公司网站谁负责做互联网公司有国企吗
  • 成都市分类信息网站开发互联网营销师证书好考吗
  • 湖南建设网站官网岳阳市内从事网站建设的公司
  • 网站备案 前置审批号建设纺织原料网站
  • 分类信息的网站如何推广教育类网站 前置审批
  • 深圳西乡 网站建设荔浦火车站建设在哪里