Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringBuffer Instance Variable

 
Malatesh Karabisti
Ranch Hand
Posts: 153
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can any body explain whether method attachThisVal() is Thread safe ? if so please explain ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a method is thread safe is depends on how the method is access by two different thread.

if a single instance(object which has attachThisVal method) is shared by two thread, then the method is not thread safe . instance variable is shared by concurrent threads.
<edit>
however, here StringBuffer is used as instance variable, StringBuffer methods are synchronized, so append is synchronized, hence threads are syncronized here.
</edit>
hth
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the output of the method is expected to be THREAD_NAME appended with PASSED_VALUE

then it is not thread safe if synchornization is not done on the method calling code block.

The calls to append in the StringBuffer will happen in a synchronized manner.

 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a possible scenario, and would it be good for your program?

Think about it, keeping in mind that StringBuffer's methods are synchronized, but attachThisVal(String value) is NOT.


-Two threads access attachThisVal() at the same time
-first thread appends ThreadName
-second thread appends ThreadName
-second thread appends value
-first thread appends value
-your StringBuffer data's toString method reads: "name1name2value2value1"


 
Malatesh Karabisti
Ranch Hand
Posts: 153
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Dieter

I think the local variable will be created in stack for individual threads. Then how come

Thread1=Value2
Thread2=Value1

This kind of output we can expect.. can you comment on this ?
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think the output you said is possible.

Value 1 can never come before Name 1
Value 2 can never come before Name 2



 
Pablo Marmol
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The local variable is "value", but the StringBuffer is global to all threads calling that method. So while StringBuffer is thread-safe, the method "attachThisVal" isn't. For example, is posible an output like "Thread-1Thread-2Thread-3valuevaluevalue", if there is three threads and the value of parameter "value" is "value".

I hope this helps...
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any class is ThreadSafe if all its method are Synchronized.
using only StringBuffer will not make any class ThreadSafe
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we call StringBuffer as ThreadSafe because all its methods synchronized
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic