Hi, have my exam tomorrow, wish me luck, i am really nervous, just been studyin for 2 weeks, but have some java experience from be4. Anyway i have my fingers crossed
My question is:
I have some problems understanding the way wait() method works on objects. I understand the processing of code, but have some problem with the general understanding.
Plz tell me if my view on this code is right or wrong:
There are two threads here ThreadA and ThreadB. Thread A starts of running first, it calls and makes a ThreadB object, and executes the ThreadB on line 4.
ThreadA continues to process, and on line 6 it becomes the owner of object b (with other words it gets the lock on this object). ThreadA now waits until it gets notified, and gets runnable again and a candidate to run. When ThreadB gets to line 24, it notifies ThreadA.
MY QUESTIONS NOW ARE:
-it is said that a call to wait() on an object, makes a thread leave the running state. As ThreadA did in the example. BUT I ALSO READ SOMEWHERE
that when the wait()-method is invoked on an object, the thread executing that code gives up its lock ? this i dont understand, how can one thread get to be a owner of a lock and at the same time release all its locks ??? i dont know what im saying but im confused
-by the way shouldnt "int total" instance variable in ThreadB (line 17) be private ? cause some other class in this same package could get access to it and corrupt it ?
[ Jess added
UBB [code] tags to preserve whitespace, check 'em out! ]
[ August 20, 2003: Message edited by: Jessica Sant ]