Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

If synchronized were missing  RSS feed

 
Kalyan Anand
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java 1.4 if synchronized keyword was not provided how could the thread safety be achieved in java programs ?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


We would need to make sure no more that one thread is accessing individual object and the story would be same, worthless. No concurrency.
How could we save avoiding corrupting object by safe-less access of the object by multiple threads.
 
Shikhar Madhok
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure. But you would probably have to juggle with the wait), notify(), yield) methods to achieve synchronization.

Shikhar
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could possibly create your own mutex (mutual exclusion) lock by building your own Synchronizable base class and make sure all classes extend the base class. Still difficult, but could be possible.

In its most basic sense, a synchronized block can be simulated with:

Where hasLock() checks to see if the current thread has the mutex lock on the instance and obtainLock() checks that the mutex is empty (and then gets taken) or is already held. Note this is pseudocode and not recommended for use.

Without direct support from the VM you would never have truly atomic operations and would therefor never be 100% safe.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shikhar Madhok:
Not sure. But you would probably have to juggle with the wait), notify(), yield) methods to achieve synchronization.

Shikhar


But wait() and notify(), by rule can't be used without synchronized,
at its place.




If you need to call wait(), notify(), or notifyAll() from within a non-synchronized method, then you must first obtain a lock on the object's monitor. If you don't, an exception will be generated when an attempt is made to call the method in question.



 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without direct support from the VM you would never have truly atomic operations and would therefor never be 100% safe.


In fact, atomicity of lock testAndSet operation ,the underlying mechanism of synchronization, typically requires Hardware support.

For hardwares with no support for such an atomic testandset operation,we can probably employ Peterson's algorithm for achieving synchronization.

Check out Test-and-set


[ September 06, 2008: Message edited by: Ajay Saxena ]
[ September 06, 2008: Message edited by: Ajay Saxena ]
 
Kalyan Anand
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ajay Saxena:


In fact, atomicity of lock testAndSet operation ,the underlying mechanism of synchronization, typically requires Hardware support.

For hardwares with no support for such an atomic testandset operation,we can probably employ Peterson's algorithm for achieving synchronization.

Check out Test-and-set


[ September 06, 2008: Message edited by: Ajay Saxena ]

[ September 06, 2008: Message edited by: Ajay Saxena ]


I think the Java optimization architecture doesnt appear to permit this.

http://coding.derkeiler.com/Archive/Java/comp.lang.java.programmer/2006-11/msg03323.html
[ September 07, 2008: Message edited by: Santhosh Jali ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!