aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Threads and synchronization!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Threads and synchronization!!" Watch "Threads and synchronization!!" New topic
Author

Threads and synchronization!!

Hung Tang
Ranch Hand

Joined: Feb 14, 2002
Posts: 148
can anyone explain how threads and synchronization works? What I understand is when you have a block of synchronized code, you must acquire the lock of the object to get into the synchronized code. But how do you know if a thread can acquire it or not? If it can't, does it hang around outside of the synchornized code until it gets the lock? Once it acquires the lock, where does it resume, into the synchronized block of code? For example.
synchronized (Data) {
<<write code here>>
}
I assume which ever thread that comes into this block of code first gets the lock, and other thread gets blocked because they're not able to acquire the lock, am I right? The thread that enter this synchronize code can call the wait() method. Would this result in the thread giving up the lock? What happens to this thread after? In what situation would ever have to invoke the notifyAll() method?
Bhuvan mehra
Ranch Hand

Joined: Mar 29, 2002
Posts: 69
hi Hung Tang,
i am also new to the forum but i have read some discussions of the forum on the lock()/unLock().
====================
I assume which ever thread that comes into this block of code first gets the lock, and other thread gets blocked because they're not able to acquire the lock, am I right?
=============================
yes, you are correct.
=============================
The thread that enter this synchronize code can call the wait() method.
=============================
yes, to invoke wait(), thread needs lock of object.
=============================
Would this result in the thread giving up the lock?
=============================
yes again, invoking wait() means giving up the lock.
=============================
What happens to this thread after? In what situation would ever have to invoke the notifyAll() method?
=============================
first ques. is in which situation you will call wait(), when a record (resource) will not be available.
when the record will be available in that situation you will call notifyAll().
to implement the above, you can maintain a collection API, say HashSet, when thread will execute syn. code first step will be to check if the record no. is in the HashSet, if yes, then call wait().
if it is not in the HashSet, put it in the HashSet (for other thread which want the same record will check HashSet and call wait()), continue to excecute. after that call notifyAll().
i have not implement the procedure, so wait for ans. from someone who have implement it.
best of luck.
regards,
Bhuvan.
[ March 31, 2002: Message edited by: Bhuvan ]
 
jQuery in Action, 2nd edition
 
subject: Threads and synchronization!!