aspose file tools*
The moose likes Threads and Synchronization and the fly likes using ReentrantLock to model a Semaphore 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 » Java » Threads and Synchronization
Bookmark "using ReentrantLock to model a Semaphore" Watch "using ReentrantLock to model a Semaphore" New topic
Author

using ReentrantLock to model a Semaphore

Sajith Hasanka
Greenhorn

Joined: Aug 20, 2009
Posts: 17
Hi all,

Im using ReentrantLock, to implement a Semaphore, for an assignment, i have implemented that for a certain level, but have some issues.
Hope anybody of you can help me on this.

My intention is to have 2 threads, 1 thread prints value 0, and the other prints 1, and i need to have those values print in order..
first prints 0, and then 1

But im getting the following exception

Thread number is : 0
waiting...
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:485)
at org.concurrent.saj.Semaphore.p(Semaphore.java:29)
at org.concurrent.saj.MyThread.run(MyThread.java:29)
at java.lang.Thread.run(Unknown Source)



What i doubt was, the place where i invoke wait( ) is not a synchronized code block. Isn't it enough to invoke lock.lock( ) to make that code synchronized, ??
or else do i need to follow some other approach with this ReentrantLock ??

your qiuck response is highly appreciated.

Thanks in advance
saj

code snippets are listed below.















- SCJP 6.0
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
ReentrantLock and wait/notify are completely different mechanisms, don't mix them.
Wait/notify works only in synchronized blocks.
ReentrantLock is like simple 'virtual' synchronized block without wait/notify:
lock() method begins your 'virtual' synchronized block,
and unlock() ends this block. The lock guarantees that this 'virtual block' could be executed
by only one thread that has acquired the lock, the other threads will have to wait on lock().

Declare initialValue variable as volatile and try the following code, it should give you desired results:



Adam Smolnik
Ranch Hand

Joined: Apr 15, 2009
Posts: 63
Hey.
Your MySemaphore.wait() expects the lock on MySemaphore instance not on other object. Take a look at the better and proper usage of Lock, Condtion and await instead of wait.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/locks/Condition.html.

If you still have a problem with this issue I can help to write it.

Adam


SCJP, SCWCD, SCBCD, SCDJWS
Sajith Hasanka
Greenhorn

Joined: Aug 20, 2009
Posts: 17
Ireneusz,

thanks a lot for you explanation, earlier i tried that using Thread.sleep(), and it executed in the way i expected.
But the problem is, here i need to implement a "Sempahore" (using locks). according to the definition of the Semaphore,
in its down method, if the counter variable value is 0, the thread which invoked down method should block, until some other
thread signals it to wakeup. With sleep method how it can be implemented? i mean, we need to provide the time of sleep
as an argument, so without knowing the time, for how long it should sleep, how can we use that method? Also, in the up method of
the semaphore, it should signals (notifies) any waiting threads on it. How can i achieve that?

What do you think about the approach proposed by Adam, to use await and signal?

Adam, thanks for your reply, and im trying that approach!

Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Sajith Hasanka wrote:
What do you think about the approach proposed by Adam, to use await and signal?

Adam, thanks for your reply, and im trying that approach!


That is how it is designed. The wait/notify mechanism implements condition variable support, in relation to Java synchronization. The Condition class implements condition variable support in relation to the Lock class.

So... if you are going to use the ReentrantLock class as your synchronization mechanism, don't use wait and notify. Use a Condition object returned from your Lock Object.... Don't mix the two mechanisms.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Adam Smolnik
Ranch Hand

Joined: Apr 15, 2009
Posts: 63
Hey.

Of course - therefore I have used the "...proper usage of Lock, Condtion and await instead of wait".

More details can be found in the article below(written by favourite author Brian Goetz):
http://www.ibm.com/developerworks/java/library/j-jtp10264/index.html

Adam
Sajith Hasanka
Greenhorn

Joined: Aug 20, 2009
Posts: 17
Thanks Adam and Henry.

Got it cleared now

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: using ReentrantLock to model a Semaphore