• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there any alternative for synchronization?

 
Anand Jooshi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI friends,

Is there any alternative for synchronization?
If we are not allowed to use synchronization block or method, how we can make the code thread safe.

I faced this question while going through an interview!

Thanks,
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java.util.concurrent package has a whole range of classes that can be used to make multi-threaded code thread-safe without explicitly using the "synchronized" keyword, if that's what you're asking.

The ThreadLocal class may also help, as may the "volatile" keyword.

And, most fundamentally, multi-threadedness is only a problem if there is shared, mutable state. If you can avoid that, then the code is multithread-safe.
 
Henry Wong
author
Marshal
Pie
Posts: 21008
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I faced this question while going through an interview!


If only this question ever comes up on any interview that I ever go to....

Synchronization is for a type of locking called "pessimistic locking". It is based on the theory that if you don't protect the code, the threads will step on each other and corrupt the data.

This question is perfect to go into the theory of "optimistic locking" -- how you code with the assumption that the threads won't step on each other. This can lead to how you code something to be done in an atomic fashion, and how you can use the CAS methods of the atomic classes to do so. In other words, you use separate variables to do all the operations without exposing interim data -- then use the CAS to swap the data atomically. A CAS failure is considered a collision which will trigger a rollback (and/or retry).

This question is also perfect for a discussions about transactions -- and commit and rollbacks.

Henry
 
Anand Jooshi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys ...!

Both of you put me in the right direction
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic