• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using ReentrantLock to model a Semaphore

 
Sajith Hasanka
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.














 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sajith Hasanka
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21184
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Adam Smolnik
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Adam and Henry.

Got it cleared now

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic