• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When a class is thread safe?

 
Alam Ameghino
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I forgot the word "synchronized" in the options [D] y [E].

I understand!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic