• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Semaphore  RSS feed

 
Krystian Kowalski
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I can't figure out how should i solve this. I am trying to implements simple Semaphore but there is a problem- at the end the thread decreaser is waiting forever and i was thinking about some additional counters that counts how long that thread has been waiting but i feel it's not a good way to resolve this problem, can you help me ?
Here is my code:



output:
take: Thread-0
release before while: Thread-1
release before while: Thread-1
release from wait: Thread-1
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
take: Thread-0
release before while: Thread-1
release from wait: Thread-1

So... possible scenario :
- thread 0 notify thread 1 and set boolean to true
- for loop of thread 0 finishes
- thread 1 is doing something then set flag to false
- he goes into wait ... and waits forever- no notify

Sleep is resolving this problem, because thread 0 is slower than thread 1, but is there any other way? I want to implement simple semaphore with wait and notify, no counting
Please help : )
 
Henry Wong
author
Sheriff
Posts: 23282
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In your example, the wait() and notify() mechanism is supposed to work with the counters -- so the synchronization locks should also protect the counters. Without it, it is possible to release semaphores that you don't own (or release more than you own) or grab a semaphore that isn't available. With your implementation, you are just using the wait/notify mechanism to communicate between threads, and completely unaware of why the notifications are happening.

Unfortunately, it isn't a one or two line change to fix this. So, I don't know what hints to give ...

Henry
 
Krystian Kowalski
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh :< Is it possible to implement a Semaphore with notify and wait only ? Without counter inside ?
 
Henry Wong
author
Sheriff
Posts: 23282
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Krystian Kowalski wrote:oh :< Is it possible to implement a Semaphore with notify and wait only ? Without counter inside ?


I don't see how it would be possible ... but ... if you can do it, I would love to see it...

Henry
 
Krystian Kowalski
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so, i am pretty new to concurrency (not using it in my daily tasks in job : < ) and i am learning it at home. Done some tutorials and now I have to do a homework on my studys :
I have those 2 classes to increment and decrement and as my homework is to implement two semaphores: Binary Semaphore and counting Semaphore.
I know how counting Semaphore works (java.util.concurrent.Semaphore ).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!