It's not a secret anymore!
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Is deadlock a big threat ? 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 "Is deadlock a big threat ?" Watch "Is deadlock a big threat ?" New topic

Is deadlock a big threat ?

Nicolas Zozol
Ranch Hand

Joined: Oct 13, 2009
Posts: 33
Each time I see an exemple of Deadlock in a book, I think :"How god, what the hell went inside his brain to design such a solution". Even with all warnings about deadlock on SCJD. in my SCJD, I don't see how you could create decently a deadlock, except maybe using notify() instead notifyAll().

Am I too confident ?

Robusta Web Library : Open Source for REST & Java
Matheus Mendes
Ranch Hand

Joined: May 15, 2007
Posts: 68
Maybe you are, ( or I'm just too newbie )

It will depends of what strategy you will use in your project. Maybe some distraction could leave you into one deadlock situation, all books they approach only a deadlock situation, with a poor context, because what matter is the deadlock and not the entire application. What I mean is that the book examples didn't cover all situations , they just lead you into the best practices. On my project I discover a dead lock issue, weeks before upload my project ( thanks do Perillo test suit ), and I was impatient to upload and finish it. I try to investigate and notice that I was using mutex locks to some I/O methods, to write and upload the records. Then I simply synchronized the methods and it works fine...... When it was teste with 20, 30 clients it works fine.... But in the test suit, with lots of threads, It locks =/.

The advice is, do not underestimate the locks and test every piece of code.

The Death of one is a tragedy, but the Death of a million is just a statistic. Joseph Stalin

Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
Just, as a matter of interest. Created a deadlock situation today, using notify instead of notifyAll while calling unlock() while using multiple threads. That's 1 example.
Roel De Nijs

Joined: Jul 19, 2004
Posts: 8393

Hi Nicolas,

I think trying to avoid the deadlock in this assignment is one of the big tasks. My initial code was also not deadlock-proof when I ran Roberto Perillo's test case. It's very easy to end up with code that possibly could deadlock (from my own experience). So I would be not too confident about it. If you are able to run Roberto's test with different counters (1, 10, 100, ..., 10000) and they all complete, I'm sure you are safe

Kind regards,

SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
Roberto Perillo

Joined: Dec 28, 2007
Posts: 2271

I'm glad to know that the code I created to test this part of the assigment is being helpful to everybody!

Now, Nicolas, I see this as a big opportunity to learn more about concurrency, because there are a lot of forms to implement it (take a look at the java.util.concurrent package and its subpackages), including the classical synchronization. Indeed, this is the hardest part of the assignment, so be careful with that.

I still didn't have the opportunity to read it, but I just found the Java Concurrency in Practice book, and as far as I could see, it is really really good. It can be a good resource for reseach!

Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Nicolas Zozol
Ranch Hand

Joined: Oct 13, 2009
Posts: 33
I've implemented the Lock stuff at the end of the program and launched the unit-test.

Guess what, I have a deadlock
Nicolas Kal
Ranch Hand

Joined: Sep 09, 2009
Posts: 69
At least now you can fix it It's good that you've found it before you submit it


SCJP 6.0 , SBCD (Preparing..),
MSc Enterprise Systems Development, BSc Computer Science
Nicolas Zozol
Ranch Hand

Joined: Oct 13, 2009
Posts: 33
I spent all the day on deadlocks

I resolved it quickly with ReentrantLock.

The downside of this is that I lock every records when I une the lock(int recNo) method :

In my comments, I will add that ReentrantReadWriteLock might be more efficient if it's needed. But it may affect the design of the interface.

Good point : it passes the Thread test with 2000 loops. It looks like I'm almost done with this program
I agree. Here's the link:
subject: Is deadlock a big threat ?
It's not a secret anymore!