This week's giveaway is in the Threads forum.
We're giving away four copies of Java Concurrency Live Lessons and have Doug Schmidt on-line!
See this thread for details.
Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads...  RSS feed

 
sumi mathew
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi can any one answer to this...


How can two threads access a synchronized block simultaneuously i.e. one thread should give the chance for other thread to execute synchronized block even before this thread finishes the execution of the same synchronized block?
 
Henry Wong
author
Sheriff
Posts: 22866
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sumi mathew:

How can two threads access a synchronized block simultaneuously i.e. one thread should give the chance for other thread to execute synchronized block even before this thread finishes the execution of the same synchronized block?


Simple answer... you can't. While a thread is holding a lock, another thread that needs the lock can't execute. That is the whole purpose of synchroniation.

More complex answer.... you can give up the lock within the synchronization block via the wait() method. But if you are going to do that, you will have to work out a wait/notify handshake between the two threads.

Henry
[ May 09, 2008: Message edited by: Henry Wong ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!