File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer  Instance Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer  Instance Variable" Watch "StringBuffer  Instance Variable" New topic
Author

StringBuffer Instance Variable

Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153



Can any body explain whether method attachThisVal() is Thread safe ? if so please explain ?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Aug 06, 2010
Posts: 173
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.


Regards,
Avishkar Nikale
Dieter Quickfend
Ranch Hand

Joined: Aug 06, 2010
Posts: 359
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"



Oracle Certified Professional: Java SE 6 Programmer
Oracle Certified Expert: Java EE 6 Web Component Developer
Oracle Certified Expert: Java EE 6 Enterprise JavaBeans Developer
Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

@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

Joined: Aug 06, 2010
Posts: 173
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

Joined: Aug 23, 2010
Posts: 13
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

Joined: Mar 17, 2010
Posts: 1051

Any class is ThreadSafe if all its method are Synchronized.
using only StringBuffer will not make any class ThreadSafe


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

we call StringBuffer as ThreadSafe because all its methods synchronized
 
 
subject: StringBuffer Instance Variable
 
Similar Threads
how many objects are created?
How to replace string in a file
String Problem
Object creation??
StringBuffer to File - Need Example