为什么StringBuffer是线程安全的?

tonyzjg 2010-06-18 11:43:03
如题
...全文
623 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
商科程序员 2010-06-18
  • 打赏
  • 举报
回复
线程安全就是你在两上线程里都用同一个stringbuffer实例,append进去的数据不会丢.
如果是stringbuilder,两个线程同时执行append时,有时会出现数据丢失情况.
czmchen 2010-06-18
  • 打赏
  • 举报
回复
synchronized 是线程安全的。
licip 2010-06-18
  • 打赏
  • 举报
回复
有这个:synchronized 这是线程安全的。
  • 打赏
  • 举报
回复
因为很多方法都是同步synchronized
dr_lou 2010-06-18
  • 打赏
  • 举报
回复
/*
* @(#)StringBuffer.java 1.101 05/11/17
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

package java.lang;


/**
* A thread-safe, mutable sequence of characters.
* A string buffer is like a {@link String}, but can be modified. At any
* point in time it contains some particular sequence of characters, but
* the length and content of the sequence can be changed through certain
* method calls.
* <p>
* String buffers are safe for use by multiple threads. The methods
* are synchronized where necessary so that all the operations on any
* particular instance behave as if they occur in some serial order
* that is consistent with the order of the method calls made by each of
* the individual threads involved.
* <p>
* The principal operations on a <code>StringBuffer</code> are the
* <code>append</code> and <code>insert</code> methods, which are
* overloaded so as to accept data of any type. Each effectively
* converts a given datum to a string and then appends or inserts the
* characters of that string to the string buffer. The
* <code>append</code> method always adds these characters at the end
* of the buffer; the <code>insert</code> method adds the characters at
* a specified point.
* <p>
* For example, if <code>z</code> refers to a string buffer object
* whose current contents are "<code>start</code>", then
* the method call <code>z.append("le")</code> would cause the string
* buffer to contain "<code>startle</code>", whereas
* <code>z.insert(4, "le")</code> would alter the string buffer to
* contain "<code>starlet</code>".
* <p>
* In general, if sb refers to an instance of a <code>StringBuffer</code>,
* then <code>sb.append(x)</code> has the same effect as
* <code>sb.insert(sb.length(), x)</code>.
* <p>
* Whenever an operation occurs involving a source sequence (such as
* appending or inserting from a source sequence) this class synchronizes
* only on the string buffer performing the operation, not on the source.
* <p>
* Every string buffer has a capacity. As long as the length of the
* character sequence contained in the string buffer does not exceed
* the capacity, it is not necessary to allocate a new internal
* buffer array. If the internal buffer overflows, it is
* automatically made larger.
*
* As of release JDK 5, this class has been supplemented with an equivalent
* class designed for use by a single thread, {@link StringBuilder}. The
* <tt>StringBuilder</tt> class should generally be used in preference to
* this one, as it supports all of the same operations but it is faster, as
* it performs no synchronization.
*
* @author Arthur van Hoff
* @version 1.101, 11/17/05
* @see java.lang.StringBuilder
* @see java.lang.String
* @since JDK1.0
*/
public final class StringBuffer
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{

/** use serialVersionUID from JDK 1.0.2 for interoperability */
static final long serialVersionUID = 3388685877147921107L;

/**
* Constructs a string buffer with no characters in it and an
* initial capacity of 16 characters.
*/
public StringBuffer() {
super(16);
}

/**
* Constructs a string buffer with no characters in it and
* the specified initial capacity.
*
* @param capacity the initial capacity.
* @exception NegativeArraySizeException if the <code>capacity</code>
* argument is less than <code>0</code>.
*/
public StringBuffer(int capacity) {
super(capacity);
}

/**
* Constructs a string buffer initialized to the contents of the
* specified string. The initial capacity of the string buffer is
* <code>16</code> plus the length of the string argument.
*
* @param str the initial contents of the buffer.
* @exception NullPointerException if <code>str</code> is <code>null</code>
*/
public StringBuffer(String str) {
super(str.length() + 16);
append(str);
}

/**
* Constructs a string buffer that contains the same characters
* as the specified <code>CharSequence</code>. The initial capacity of
* the string buffer is <code>16</code> plus the length of the
* <code>CharSequence</code> argument.
* <p>
* If the length of the specified <code>CharSequence</code> is
* less than or equal to zero, then an empty buffer of capacity
* <code>16</code> is returned.
*
* @param seq the sequence to copy.
* @exception NullPointerException if <code>seq</code> is <code>null</code>
* @since 1.5
*/
public StringBuffer(CharSequence seq) {
this(seq.length() + 16);
append(seq);
}

public synchronized int length() {
return count;
}

public synchronized int capacity() {
return value.length;
}


...


因为很多方法都是synchronized
aunty_flybird 2010-06-18
  • 打赏
  • 举报
回复
举个例子吧,vector和ArrayList的区别

就好比StringBuffer和StringBuilder的差别

想详细了解实现建议读读源码
ronniegxq 2010-06-18
  • 打赏
  • 举报
回复
因为很多方法都是同步synchronized

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧