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

Threads Synchronization (simple)  RSS feed

 
Andree Charfen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Java partners!
I gotta a question and I hope you can help me. Here is some PSEUDO-CODE that I cant really tell if it is really synchronized protected.

now... since changeValues is not synchronized many threads can access it. That I got right. But... since all the functions and code in there are synchronized ... can X and Y really become different?
What I am trying to say.. can the JVM make a pause between functions to allow another thread to run and therefore access the changeValues function for a while?
Thanks for your help!
[ March 24, 2004: Message edited by: Andree Charfen ]
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the JVM can make a thread pause whenever it pleases. Thats the definition of preemptive multitasking.
You need to decide what you are trying to protect. Without having a specific thing you want to protect from, its impossible to say wether or not this will accomplish that goal.
 
Andree Charfen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so the values of x and y can be different?
 
Andree Charfen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can the JVM pause between functions... something like:

[ March 24, 2004: Message edited by: Andree Charfen ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andree Charfen:

/*CAN THE JVM PAUSE THE THREAD HERE AND UNLOCK THE OBJECT HERE?*******/

Yes, the JVM can run absolutely any other code that that point. The object needn't be "unlocked" to do so, because it's not locked to begin with. If, on the other hand, changeValue() were synchronized, then nothing else could call changeX(), changeY(), or viewXY() until changeValue() completed.
 
Andree Charfen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK! Got it!
Thanks!
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only time the JVM can not pause is during the execution of so called 'atomic' instructions.
And your monitors are unlocked and relocked each time you exit and enter those synchronized methods, so indeed the JVM could switch to an alternate thread while your monitor is unlocked. In fact it can switch while your monitor is locked as well. But in that case the thread it switches to will not be allowed to enter the method since it can't acquire the monitor.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!