栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

同步块-锁定多个对象

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

同步块-锁定多个对象

实际上,同步是针对代码的,而不是对象或数据的。在同步块中用作参数的对象引用表示锁定。

因此,如果您有如下代码:

class Player {  // Same instance shared for all players... Don't show how we get it now.  // Use one dimensional board to simplify, doesn't matter here.  private List<Player>[] fields = Board.getBoard();  // Current position  private int x;  public synchronized int getX() {    return x;  }  public void setX(int x) {    synchronized(this) { // Same as synchronized method      fields[x].remove(this);      this.x = x;      field[y].add(this);    }  }}

然后,尽管处于同步块中,但由于锁不相同(它在不同的实例上),所以对字段的访问不受保护。因此,您的董事会的球员名单可能会变得不一致,并导致运行时异常。

相反,如果您编写下面的代码,它将起作用,因为我们只有一个共享锁可供所有玩家使用:

class Player {  // Same instance shared for all players... Don't show how we get it now.  // Use one dimensional board to simplify, doesn't matter here.  private List<Player>[] fields;  // Current position  private int x;  private static Object sharedLock = new Object(); // Any object's instance can be used as a lock.  public int getX() {    synchronized(sharedLock) {      return x;    }  }  public void setX(int x) {    synchronized(sharedLock) {      // Because of using a single shared lock,      // several players can't access fields at the same time      // and so can't create inconsistencies on fields.      fields[x].remove(this);       this.x = x;      field[y].add(this);    }  }}

请确保仅使用一个锁来访问所有播放器,否则板的状态将不一致。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/516510.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号