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

asp连接数据库做登录网站完整下载网页设计工作室选址依据

asp连接数据库做登录网站完整下载,网页设计工作室选址依据,摄像网站建设,企业网站模板下载网站模板下载在这个简短的博客系列的最后BadTransferOperation中#xff0c;我一直在讨论分析死锁#xff0c;我将修复BadTransferOperation代码。 如果您看过本系列的其他博客 #xff0c;那么您将知道#xff0c;为了达到这一点#xff0c;我创建了死锁的演示代码#xff0c;展示了… 在这个简短的博客系列的最后BadTransferOperation中我一直在讨论分析死锁我将修复BadTransferOperation代码。 如果您看过本系列的其他博客 那么您将知道为了达到这一点我创建了死锁的演示代码展示了如何掌握线程转储然后分析了线程转储弄清楚发生僵局的位置和方式。 为了节省空间下面的讨论同时引用了本系列第1部分中的Account和DeadlockDemo类其中包含完整的代码清单。 教科书中有关死锁的描述通常是这样的“线程A将获得对象1的锁定并等待对象2的锁定而线程B将获得对象2的锁定同时等待对象1的锁定”。 我以前的博客中显示的堆积并在下面突出显示是一个真实的死锁其他线程锁和对象陷入了直接简单理论上的死锁情况。 Found one Java-level deadlock:Thread-21:waiting to lock monitor 7f97118bd560 (object 7f3366f58, a threads.deadlock.Account),which is held by Thread-20 Thread-20:waiting to lock monitor 7f97118bc108 (object 7f3366e98, a threads.deadlock.Account),which is held by Thread-4 Thread-4:waiting to lock monitor 7f9711834360 (object 7f3366e80, a threads.deadlock.Account),which is held by Thread-7 Thread-7:waiting to lock monitor 7f97118b9708 (object 7f3366eb0, a threads.deadlock.Account),which is held by Thread-11 Thread-11:waiting to lock monitor 7f97118bd560 (object 7f3366f58, a threads.deadlock.Account),which is held by Thread-20 如果将上面的文本和图像与以下代码相关联则可以看到Thread-20已锁定其fromAccount对象 fromAccount 正在等待锁定其toAccount对象e98 private void transfer(Account fromAccount, Account toAccount, int transferAmount) throws OverdrawnException {synchronized (fromAccount) {synchronized (toAccount) {fromAccount.withdraw(transferAmount);toAccount.deposit(transferAmount);}}} 不幸的是由于时序问题 Thread-20无法获得对对象e98的锁定因为它正在等待Thread-4释放对该对象的锁定。 Thread-4无法释放锁因为它正在等待Thread-7 Thread-7正在等待Thread-11而Thread-11正在等待Thread-20释放对对象f58的锁。 这个现实世界的僵局只是教科书描述的一个更复杂的版本。 这段代码的问题是从下面的代码片段中您可以看到我正在从Accounts数组中随机选择两个Account对象作为fromAccount和toAccount并将它们锁定。 由于fromAccount和toAccount可以引用accounts数组中的任何对象这意味着它们以随机顺序被锁定。 Account toAccount accounts.get(rnd.nextInt(NUM_ACCOUNTS));Account fromAccount accounts.get(rnd.nextInt(NUM_ACCOUNTS)); 因此 解决方法是对Account对象的锁定方式施加顺序并且只要顺序一致任何顺序都可以执行。 private void transfer(Account fromAccount, Account toAccount, int transferAmount) throws OverdrawnException {if (fromAccount.getNumber() toAccount.getNumber()) {synchronized (fromAccount) {synchronized (toAccount) {fromAccount.withdraw(transferAmount);toAccount.deposit(transferAmount);}}} else {synchronized (toAccount) {synchronized (fromAccount) {fromAccount.withdraw(transferAmount);toAccount.deposit(transferAmount);}}}} 上面的代码显示了此修复程序。 在此代码中我使用帐号来确保首先锁定具有最高帐号的Account对象以便永远不会出现以上的死锁情况。 以下代码是此修复程序的完整列表 public class AvoidsDeadlockDemo {private static final int NUM_ACCOUNTS 10;private static final int NUM_THREADS 20;private static final int NUM_ITERATIONS 100000;private static final int MAX_COLUMNS 60;static final Random rnd new Random();ListAccount accounts new ArrayListAccount();public static void main(String args[]) {AvoidsDeadlockDemo demo new AvoidsDeadlockDemo();demo.setUp();demo.run();}void setUp() {for (int i 0; i NUM_ACCOUNTS; i) {Account account new Account(i, rnd.nextInt(1000));accounts.add(account);}}void run() {for (int i 0; i NUM_THREADS; i) {new BadTransferOperation(i).start();}}class BadTransferOperation extends Thread {int threadNum;BadTransferOperation(int threadNum) {this.threadNum threadNum;}Overridepublic void run() {for (int i 0; i NUM_ITERATIONS; i) {Account toAccount accounts.get(rnd.nextInt(NUM_ACCOUNTS));Account fromAccount accounts.get(rnd.nextInt(NUM_ACCOUNTS));int amount rnd.nextInt(1000);if (!toAccount.equals(fromAccount)) {try {transfer(fromAccount, toAccount, amount);System.out.print(.);} catch (OverdrawnException e) {System.out.print(-);}printNewLine(i);}}System.out.println(Thread Complete: threadNum);}private void printNewLine(int columnNumber) {if (columnNumber % MAX_COLUMNS 0) {System.out.print(\n);}}/*** This is the crucial point here. The idea is that to avoid deadlock you need to ensure that threads cant try* to lock the same two accounts in the same order*/private void transfer(Account fromAccount, Account toAccount, int transferAmount) throws OverdrawnException {if (fromAccount.getNumber() toAccount.getNumber()) {synchronized (fromAccount) {synchronized (toAccount) {fromAccount.withdraw(transferAmount);toAccount.deposit(transferAmount);}}} else {synchronized (toAccount) {synchronized (fromAccount) {fromAccount.withdraw(transferAmount);toAccount.deposit(transferAmount);}}}}} } 在我的示例代码死锁的发生是因为时机问题嵌套的synchronized在我的关键字BadTransferOperation类。 在此代码中 synchronized关键字位于相邻的行上 但是最后一点是值得注意的是 synchronized关键字在代码中的什么位置都没关系它们不必相邻。 只要您使用同一线程锁定两个或更多不同的监视对象就会发生排序和死锁。 有关更多信息请参阅本系列中的其他博客 。 该系列以及其他博客的所有源代码都可以在Github上找到网址为git//github.com/roghughe/captaindebug.git 参考 调查死锁-第4部分来自Captain Debug博客博客的JCG合作伙伴 Roger Hughes 修复代码 。 翻译自: https://www.javacodegeeks.com/2012/11/investigating-deadlocks-part-4-fixing-the-code.html
http://wiki.neutronadmin.com/news/231773/

相关文章:

  • 常州网站建设外包公司哪家好文章网站模板哪个好
  • 制作自己专属头像seo收录查询工具
  • 免费素材哪个网站比较好天元建设集团有限公司第四建筑工程公司
  • c 做的网站怎么上传图片安徽信息工程学院信息门户平台
  • 网站设计优化方案音乐网站是否可以做浅度链接
  • 晋城网站建设价格微网站建设公司首选公司
  • 2018年静安区品牌网站建设月付购物网站建站
  • 自己的公司怎么做网站住房和成乡建设部网站
  • 做任务拿赏金的网站wordpress主题改中文字体
  • python如何做自己的网站WordPress对接易支付
  • 网站建设A系列套餐报价直播网站如何做
  • 印度购物网站排名游戏网站搭建需要多少钱
  • 黄岩城乡住房和建设局网站开什么网站暴利
  • 大麦网网站建设的功能定位网站建设价格最低多少钱
  • 广州 网站制作公司 网络服务连云港做电商网站的公司
  • 网站建设中 英文受欢迎的句容网站建设
  • 怒江北京网站建设什么事网页设计
  • 企业建站用什么主机手表网站十大品牌
  • 政务网站建设要求招远网站开发
  • 手机搭建网站工具seo竞争对手网站分析
  • 贵州省铁路建设办公室网站企业官网怎么做
  • ui在线设计网站目前引流最好的app
  • 手机网站制作要求标准女做受视频网站
  • 建立一个虚拟公司的网站网站开发求职简历
  • 网站 app建设开发合作协议wordpress 模板 推荐
  • 南宁专门建网站的公司公司网站标题优化
  • 福州专业网站建设服务商wordpress 插件 样式
  • 网站搭建空间泸州市建设规划局网站
  • 商丘手机网站制作上市公司网站推广方案
  • 阿里云云主机做网站网上开店如何找货源