• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

using ReentrantLock to model a Semaphore

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.














 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:



 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Adam and Henry.

Got it cleared now

 
reply
    Bookmark Topic Watch Topic
  • New Topic