黑马程序员技术交流社区

标题: 关于线程安全类的问题 [打印本页]

作者: 冯佩    时间: 2013-1-25 20:22
标题: 关于线程安全类的问题
本帖最后由 张向辉 于 2013-1-26 16:43 编辑

看教程我们知道StringBuffer是线程同步的,而StringBuilder是线程不同步的,除了看API,我们还有什么方法能知道某个类是否是线程同步的呢?还有,同步是因为有锁,但API中的同步类都没有加锁,是不是锁被封装到了内部呢?
作者: 黑马张英涛    时间: 2013-1-25 20:28
看源代码- -!以下摘自源代码:
  1.     public synchronized void getChars(int srcBegin, int srcEnd, char[] dst,
  2.                                       int dstBegin)
  3.     {
  4.         super.getChars(srcBegin, srcEnd, dst, dstBegin);
  5.     }

  6.     /**
  7.      * @throws IndexOutOfBoundsException {@inheritDoc}
  8.      * @see        #length()
  9.      */
  10.     public synchronized void setCharAt(int index, char ch) {
  11.         if ((index < 0) || (index >= count))
  12.             throw new StringIndexOutOfBoundsException(index);
  13.         value[index] = ch;
  14.     }

  15.     public synchronized StringBuffer append(Object obj) {
  16.         super.append(String.valueOf(obj));
  17.         return this;
  18.     }

  19.     public synchronized StringBuffer append(String str) {
  20.         super.append(str);
  21.         return this;
  22.     }
复制代码

作者: 黄锦成    时间: 2013-1-25 20:34
这个要看API对于类的介绍。这个要靠自己的积累了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2