Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronization  RSS feed

 
Vijay Kumar
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does happen actually when we implement synchronized in our code?

Thanks
 
Robert Hill
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In real simple terms it locks the method and creates a single key to open it, When a call is made to that method, if the key is not being used it is passed to the caller and it can call the method, otherwise it can't until current keyholder puts it back.

Yes, that is simplistic, but hopefully it can help you understand that basic premise and at least one potential problem.
 
Vijay Kumar
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Robert thankx for it....
but actaully what I want to know is that how does it happen at internal level. how does JVM behave when any thread ask for lock.how does code lock actually.

Thankx
 
Robert Hill
Ranch Hand
Posts: 94
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no deep magic to locking or blocking a thread. The JVM gives one RUNNABLE thread after another a slice of time according to some algorithm (that I'd rather not worry about or depend upon because it might change.) If a thread is BLOCKED, it's not runnable so it doesn't get any slices.

There is a bit of complicated magic when one thread gives up a monitor and the JVM decides which waiting thread can have it next. I've only gotten into this far enough to remember that I can't predict which thread it will be.
[ September 18, 2006: Message edited by: Stan James ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For writing Java applications, you do not need to know that level of detail. Indeed, it is usually bad practice to write a Java program that depends on the exact behaviour of a particular JVM. You should only make the assumptions that are documented in the Java specs.

If you really want to know what happens inside the JVM, to implement synchronisation, then answers will be specific to a particular JVM. For instance, one JVM might make heavy use of operating system thread-related facilities to implement it, while another JVM might mostly use its own code.

The native source code for some JVMs might be available. I could be totally wrong, but isn't there some process underway by which we will eventually get to find out what's inside Sun's JVMs?
[ September 19, 2006: Message edited by: Peter Chase ]
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
I could be totally wrong, but isn't there some process underway by which we will eventually get to find out what's inside Sun's JVMs?


The source code is available. The undergoing process is to allow modifications of the code.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!