GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in thread 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 "doubt in thread" Watch "doubt in thread" New topic
Author

doubt in thread

V Gala
Ranch Hand

Joined: Aug 06, 2007
Posts: 113
Hi V Gala,

The question you posted here was an actual exam question - so wherever you got this question, it's ILLEGAL - it's a violation of copyright laws, so please don't use questions from that site anymore.

thanks,

Bert

[actual exam question deleted - bert]

[ September 03, 2007: Message edited by: V Gala ]

[ September 04, 2007: Message edited by: Bert Bates ]
[ September 04, 2007: Message edited by: Bert Bates ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14196
    
  20

Please use a meaningful subject line (instead of just "doubt").


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jianghu Li
Greenhorn

Joined: Sep 01, 2007
Posts: 13
C is correct.
V Gala
Ranch Hand

Joined: Aug 06, 2007
Posts: 113
Why A is False
Jianghu Li
Greenhorn

Joined: Sep 01, 2007
Posts: 13
Why a is correct?
The StringBuilder class was added in Java 5. It has exactly the same API as the StringBuffer class, except StringBuilder is not thread safe. In other words, its methods are not synchronized.
V Gala
Ranch Hand

Joined: Aug 06, 2007
Posts: 113
Hi
I think StringBuffer is synchronized means all its method are synchronized

there can be two different thread invoking tostring method can interfere with each other
thread1->sb.append("<");
thread2->sb.append("bbn,mn");

then the code will not be thread safe
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

For Java 5, which I assume that this mock test question is for, the answer is B. For Java 6, B and C can both be true.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Originally posted by V Gala:
Why A is False


A is false because the toString() method is thread safe -- and this is true regardless of whether a string buffer or builder is used. The buffer is a local variable that doesn't escape the method, there is no way that it can be accessed by multiple threads.

Henry
V Gala
Ranch Hand

Joined: Aug 06, 2007
Posts: 113
Thanks
I got it
Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
Can anyone please explain the code line by line.

Thanks in advance.
Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
Just an addendum to my previous post:

Also please explain the justification (i.e., why it is true or why it is false) for each of the option.

(Again)Thanks in advance.
 
GeeCON Prague 2014
 
subject: doubt in thread