File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Is there any alternative for synchronization?  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Is there any alternative for synchronization?  " Watch "Is there any alternative for synchronization?  " New topic
Author

Is there any alternative for synchronization?

Anand Jooshi
Greenhorn

Joined: Jun 15, 2009
Posts: 8
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
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Anand Jooshi
Greenhorn

Joined: Jun 15, 2009
Posts: 8
Thanks guys ...!

Both of you put me in the right direction
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there any alternative for synchronization?
 
Similar Threads
Why we should prefer synchronization?
single raf vs. raf per client
synchronization in an application server
advantage of synchronization
file parsing and synchronization