• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

3.类一级的静态变量能否锁定

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 30次 | 进入软件测试论坛讨论

领测软件测试网

Lock smith

Can you lock class-level static variables?


Summary
-->-->



Can I lock class-level (static) variables? A static member attribute can be modified by more than one calling program at a time; can I somehow make sure it@#s locked? I could think of using something like:


synchronized(this.class)

inside the method that modifies the variable. Also, I think I will need to make the static variable private. But does it make sense to make it private? Then I would give one public static function that allows modification to this variable. Also, in this method I should use:


synchronized(this.class) {
   // modify the static attribute here.
      } // End of synchronized block.

Will this ensure that no two modifying threads can modify it at the same time?

You ask an interesting question. The quick answer is yes, you can lock a class variable and I@#ll show you how (though you are close)!
First, any member variable that you wish to make thread safe must be declared private. Consider the following code:


public class MyPoint
{
  public int x;
  public int y;

  public int getX()
  {
    return x;
  }
  public int getY()
  {
    return y;
  }
  public synchronized setX(int x)
  {
    this.x = x;
  }
  public synchronized getY(int y)
  {
    this.y = y;
  }
}

If this were an ideal world, everyone would set x and y by calling setX() and setY(), respectively. However, we all know that someone will come along and set x or y directly, since we defined the members as public. And since we defined x and y as public, it is reasonable for someone to come along and access the variables directly. If we didn@#t want someone to access the members directly, we shouldn@#t have declared them as public in the first place! The only way to guarantee that threads will access x and y safely is to declare them as private and provide synchronized access.
Nothing changes when you declare a member static. If you don@#t want some thread to come along and bypass your careful synchronization, don@#t declare the member public or protected.
Now, the question remains: how do we lock the member? Sometimes I think that part of the confusion around synchronization stems from a misunderstanding of what actually happens when you call synchronized(some_object). Calling synchronized on some_object does not prevent access to some_object. Instead, you must think of synchronized as a request to open a lock around some piece of code for your thread. some_object is the lock that you wish to have access to. Only one thread may hold a specific lock at any one time.
Making a static member thread safe is fairly simple once you understand what really happens when you call synchronized. Here is a template to follow (there are many more ways to do it, though):


class Safe {
  ... other class definitions ...
  private static <type> var;

  public final synchronized static setVar(<type> val)
  {
    var = val;
  }
}

As a second option, you can also synchronize within the method. Which approach you take will depend upon your code. However, in this example, either way is equivalent, since a synchronized static method grabs the lock on the class. You would use the second option if you were doing a lot of processing inside of the method. That way you only synchronize when you have to -- thus improving performance.
You would need to use synchronization within the method if you didn@#t want to declare setVar() static. A nonstatic method declared as synchronized locks on the instance, not the class. So, if you had more than one instance, the member would no longer be thread safe. 

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网