Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unlock hangs

 
Pieter Jacobs
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Maybe I must move away from the pc for a while :-)

I call my "unlock()" method from a "finally" block. The unlock() method, is a standard method with a synchronized block where I synchronize on my map where I keep the record numbers with cookie values. Now, I startup my test app (creating 5 threads) which locks a record, read the record and then in a finally block unlocks the record.
My problem is that when I specify a record to unlock that is not in the cookie-map, it then throws an exception but then the app hangs. I've tried a load of different things - from inserting wait() and notifyAll() in a few different places, but no-go.

Hope the above makes sense.

Thanks,
Pieter
 
Roel De Nijs
Sheriff
Posts: 10392
132
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pieter,

How can you unlock a record that is not in the cookie map? You lock the record, you read the record and you unlock the record. And i guess it is just for testing purposes, because you don't need to lock/unlock the record just for reading.

I guess your app hangs because not each thread is getting notified, because the exception is thrown. I would expect your unlock method looks like this:

So you are testing a situation that should never occur in real life when you developed your application well! Because you have to develop your application in such a way that you can't unlock a record that isn't locked.

If you have a scenario where 2 threads are already finished, 2 other threads lock record 1, then 1 thread will be waiting for the lock on record 1 to be released. If then thread5 unlocks record -10 (a non-existing record that is), then the waiting thread will never be notified and your application hangs.

Kind regards,
Roel
 
Donovan Chong
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,

I'm having a similar problem with it hanging but when I actually have a record in the map. I created a simulation where one thread obtains the lock on record 1 and another thread attempts to obtain a lock on the same record, the output is below:

thread 8 attempting to obtain lock on record 1
recordNo 1 locked? false
thread 10 attempting to obtain lock on record 1
recordNo 1 locked? true
currentThread ID=8 returning lock

The code in my unlock method looks something like this:



I'm definitely doing something wrong because my notifyAll() doesn't seem to have an effect. Am I synchronizing on the incorrect object?

Regards
Donovan
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, Donovan.

Champ, instead of calling Thread.currentThread().notifyAll(), try just notifyAll().
 
Donovan Chong
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

Ok so I changed this bit

but it's still the same, so now I'm wondering about the wait() part

It seems correct to me or am I missing something?

Regards
Donovan
 
Roel De Nijs
Sheriff
Posts: 10392
132
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Donovan,

When you mark all your public methods synchronized (the simplest approach, the one I followed) you are synchronizing on the this-object. When you want to use synchronized blocks, you can synchronize on the this-object or you can synchronize on another object. One important note is that the 2 threads should access the same object (so the same instance). That's one of the basics of synchronizing and thread interaction, you should be familiar with it, because you had to learn the basics for the scjp exam.
That's why marking all public methods of the Data class synchronized will work only if combined with the singleton design pattern.

Let's assume you have following code:

a snippet from the lock-method:

a snippet from the unlock-method:

This code is just to explain how locking is supposed to be implemented when using wait/notify/notifyAll-mechanism. So you must have 1 object that will be accessed by many threads.

Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10392
132
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Donovan,

If you call wait on objectA, you have to call notify or notifyAll on objectA too. You clearly struggle with the concepts and understanding of locking and thread interaction. So i would advise you to do some extra studying on this part: have another read of the K&B-book (if you still have it) or the SCJD-book (by Andrew Monkhouse), because this part is the biggest challenge of this assignment. If you don't have locking spot on, you risk a failure.

Now it's time for me to have a

Kind regards,
Roel
 
Pieter Jacobs
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

Thanks for the replies!

Roel, don't worry, in fact I do not lock and unlock when reading a record, I'm purely just testing the app to see what happens IF something goes wrong. It definitely should not hang though, but after all the replies now, I'll quickly see if I can see where my issue is.

Thanks,
Pieter
 
Donovan Chong
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies guy.

I probably should go over Threading again.

Regards
Donovan
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Champions, I don't know if you are aware of, but, in order to test your Data class and your locking mechanism, you might want to take a look at these tests prepared by me.
 
Pieter Jacobs
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roberto,

Thanks a lot !

Have fun!
Pieter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic