A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 冯佩 中级黑马   /  2013-1-25 20:22  /  925 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张向辉 于 2013-1-26 16:43 编辑

看教程我们知道StringBuffer是线程同步的,而StringBuilder是线程不同步的,除了看API,我们还有什么方法能知道某个类是否是线程同步的呢?还有,同步是因为有锁,但API中的同步类都没有加锁,是不是锁被封装到了内部呢?

2 个回复

倒序浏览
看源代码- -!以下摘自源代码:
  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.     }
复制代码
回复 使用道具 举报
这个要看API对于类的介绍。这个要靠自己的积累了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马