wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Where to call wait()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Where to call wait()?" Watch "Where to call wait()?" New topic
Author

Where to call wait()?

Anne Crace
Ranch Hand

Joined: Aug 29, 2005
Posts: 223
I am trying to get my locking to work, using synchronized, wait and notifyAll. I had been calling wait inside lock() but it hung my application. (Waiting forever). I have never written multi-threaded code before, and this is pretty confusing. From searching and reading lots of old posts, I am pretty sure I synchronize on my Map of locks, and only call notifyAll from unlock(). I am wondering if I even need to wait? I have the "consume no CPU cycles" in my instructions, so I'm pretty sure setting a timeout is a bad idea, and probably automatic failure. I also understand the notifyAll might consume a few (trivial few), but people generally have passed with full marks and used it. Thanks in advance.


SCJP, SCJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5300
    
  13

Hi Anne,

locking is the hardest part in my opinion

you can only wait on an object where you have the lock on.

locking would be something similar to:


and in your unlock method you will have something like


if you have synchronized all your methods, you will call wait and notifyAll on the this-object

good luck!


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Anne Crace
Ranch Hand

Joined: Aug 29, 2005
Posts: 223
Thanks, Roel,

I had a logic error in my code. I had everything you just told me, but this statement: I figured the lock wouldn't be in there yet, because I hadn't put it in there. I guess that is the whole point It works now, at least not throwing any exceptions and hanging. I guess I need to thread thrash it, now.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5300
    
  13

it took me 3-4 weeks to make my Data-implementation (i have fulltime job, so only weekends and evenings), but because i see it as the most important part, i took my time to ensure everything is working as expected (and javadoc'ing everything is also time consuming). I made also a unit test to verify if each method of the data class is doing what the javadoc says (including all possible exceptions that can be thrown). and the lock/unlock methods i tested using the test classes of roberto perillo.
Anne Crace
Ranch Hand

Joined: Aug 29, 2005
Posts: 223
I got a test harness off this forum. I will grab Roberto's as well. He got 80/80 in Locking. He also synchronized all of Data (I did that, too). I am lucky, I have down time at work right now, so I am working on it at work, and have made major progress this week. I should be getting another project tomorrow or Monday. I don't have time to work on it at home because I am working on my BS degree in Information Systems and my free time is spent working on that.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5300
    
  13

Anne Crace wrote: He also synchronized all of Data (I did that, too).


Me too

I'm not that lucky at work, i even have to travel 1.5 - 2h each day, so my time during the week is very limited, but it has a high priority, because i've the assignment already from Q2 2007 and all other projects have lower priority, i hope to finish at the beginning of april

which test harness are you using?

good luck with your assignment (and your bs degree)
regards,
Roel
Anne Crace
Ranch Hand

Joined: Aug 29, 2005
Posts: 223
I used this one: Take this Testing Tool. Be sure to note the second post. There is a small logic error that needs to be fixed in the original code. I found Roberto's in the process of looking for this one I have had mine out since Spring of 2006, so it is a priority for me, too!
 
GeeCON Prague 2014
 
subject: Where to call wait()?