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

doubt in thread

 
V Gala
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
 
Jianghu Li
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C is correct.
 
V Gala
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why A is False
 
Jianghu Li
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
I got it
 
Lalit Bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone please explain the code line by line.

Thanks in advance.
 
Lalit Bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic