0

If your text can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.

If your text can changes, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous.

What does it mean by multiple threads? Can anyone explain me over this? I mean is it something two methods or two programs trying to access another method at same time.

2
  • 2
    en.wikipedia.org/wiki/Thread_%28computer_science%29 Commented Sep 24, 2010 at 10:09
  • 2
    It means that when 2 or more threads access the same object at the 'the same time'. For example 1 thread might trying to get the value of the string and the other one updating it. It's the known 'synchronization' issue. The stringBuffer could be any object accessed by 2 or more threads. Commented Sep 24, 2010 at 10:13

4 Answers 4

6

Threads are paths of execution that can be executed concurrently. You can have multiple threads in your Java program, which can call the same method of the same object at the same time. If the method e.g. prints something on screen, you might see the messages coming from different threads jumbled up - unless you explicitly ensure that only one message can be printed out at a time, and all other requests to print shall wait until the actual message is fully printed.

Or, if you have a field in that object, all threads see it. And if one of them modifies the field... that's when the interesting part begins :-) Other threads may only see the updated value at a later time, or not at all, unless you specifically ensure that it is safe to use by multiple threads. This can result in subtle, hard to reproduce bugs. This is why writing concurrent programs correctly is a difficult task.

On machines with a single processor core, only a single thread can run at any time, thus different threads are executed one after another, but the OS switches between them frequently (many times per second), thus giving the user the illusion of seeing multiple threads running in parallel. OTOH multicore machines can really run several threads at the same time - as many as processor cores they have.

Every Java program has at least one thread. You may manually create additional threads within your program and pass them tasks to execute.

A detailed explanation of threads and processes - and further, concurrency in Java - can be found in the Java Tutorials.

Sign up to request clarification or add additional context in comments.

2 Comments

@John, the answer to this is already there - I emphasized it with bold now :-)
@John: Every independent execution path is a thread. So, by definition, executing a Java program that consists of nothing but a main method is also a thread.
1

Threads are like to little process.

Consider the case a string is shared between two threads which are running concurrently.

Both of them operating on it. So it will be the case where the String is under manipulation by both of the thread so it won't remain in consistent state.

So.

  • StringBuffer is designed to be thread-safe and all public methods in StringBuffer are synchronized. StringBuilder does not handle thread-safety issue and none of its methods is synchronized.

  • StringBuilder has better performance than StringBuffer under most circumstances.

  • Use the new StringBuilder wherever possible.

For more on concurrency refer this

4 Comments

A String is immutable, it can't be "under manipulation". String itself is threadsafe.
every java program has at least a main thread that is started by the main class. but there may be many others
By default in Simple java application there is a Main Thread,, You can startss many threads using Thread Class ,For more on Thread please refer :download.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html
From your main thread you can start new threads. and from them new again.
1

I think you can use StringBuilder in both cases, but be very aware in multithreaded programs. Synchronization at StringBuffer method level is not useful when you must do more operations on such string (think of it like on database transactions) like delete 3 chars at beginning, then delete 3 chars at end, and compare it with something. Even when such delete operations are synchronized (thus atomic) you can have:

  1. first thread can get such string, and delete 3 chars at beginning
  2. second thread get such string and delete 3 chars at beginning
  3. string is not in consistent state (6 chars deleted from beginning)

You should synchronize access to such variables on your method level, not relying on StringBuffer method synchronization. Using StringBuffer you will have two levels of synchronizations while with StringBuilder you will have only your own synchronization.

Comments

0

Mu;ltiple threads is like running parts of the same program at the same time sharing the same data.

A typical example is that when the program needs to do a long calculation, it can create a separate thread to do the calculation in the background and keep reacting to user input on the main thread.

The problem with multiple threads is that since they are running at the same time, and you do not really now what they are doing, since they can make their own decisions, it becomes dngerous to rely that certain actions on the shared data are always done in a certain order.

There a re various techniques of deqling with thqt, one is the synchronize key to qllow synchronous access. This means that one thread blocks access to an Object while it is busy so when the other threads want to get access, they have to wait.

So that's what meant with that StringBuffer is synchronous, it will block access to toher threads when one thread is updating it.

Using multiple threads is considered an advanced topic and not all problems have been solved in a satisfactory manner. Relying on 'synchronous' objects to deal with concurrency will not get you very far, because typically you will do updates to multiple objects in a coordinated manner and these must also be synchronized.

My advice : stay away from there until you've read a good book and experimented on exercises. Till then share no data between threads (other than the simplest of signaling flags).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.