aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes When a class is thread safe? 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 "When a class is thread safe?" Watch "When a class is thread safe?" New topic
Author

When a class is thread safe?

Alam Ameghino
Ranch Hand

Joined: Jun 01, 2011
Posts: 60
Hi friend! I go on with some exercises...

It is from Kathy and Bert's book, and the exercise is the next...

Which lines of code need to be changed to make the class thread safe?

And the options are:

[A] Line 2
[B] Line 3
[C] Line 4
[D] Line 5
[E] Line 6
[F] Line 7
[G] Line 8

The book say:
[B], [C], [D], [E], [F] and [G] are correct. The variables need to be private, and even methods that don't change a variable's value need to be synchronized if they acces the variable.


I am agree with the options [C], [F] and [G] about "y" var.
I am agree with the option [B] about "x" var. For me it is sufficient to put "private" to var "y"

I am disagree with the options [D] and [E] about "x" var.

(1) What do you think?
(2) What the book say "the variables need to be private"? An object with a static var can not be thread safe?


Thanks a lot!
Alam Ameghino
Ranch Hand

Joined: Jun 01, 2011
Posts: 60
Sorry, I forgot the word "synchronized" in the options [D] y [E].

I understand!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: When a class is thread safe?