• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple question about threads  RSS feed

 
Krishna Rao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the meaning of

synchronized(someobject){

}

Does it mean only thread is supposed to access someobject's fields and methods ?
 
ankush dixit
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it means that if a thread(which has access to someobject's lock) enters synchronized block others threads have to wait...
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Krishna Rao:
What is the meaning of

synchronized(someobject){

}

Does it mean only thread is supposed to access someobject's fields and methods ?


This means that one one thread at a time can run the statements within the block and the lock of someobject will be acquired before executing the statements and after the execution the lock is released.
 
Krishna Rao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies.

synchronized(a){
....
....
....
}

When the current execution thread has lock over 'a', can any other thread execute in a different path can execute a's method. For ex: a.print() ?

Please clarify
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends whether print method is syncronized on the same lock or nor .
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!