Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Semaphore

 
raj raval
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a thread program using Semaphores.
But I don't know about semaphores
and I have to develop this program

1. A group of M fraternity brothers & sorority sisters is having a party & drinking from a keg that can hold 100 servings of beverage . one unlucky pledge is responsible for replacing the keg each time it empties. When a partier wants to drink, he or she fills a cup from keg, unless it is empty. If the keg is empty , the partier wakes up the pledge and then waits until the pledge has returned with a new keg. The behavior of the partiers and pledge is specified by the following �threads�:


Partier:
loop
tellPledgeIfKegIsEmpty
getServingFromKeg
drink
waitSomeAmountOfTime
end loop

Pledge:
loop
waitForKegToEmpty
getnewKeg
end loop
1. Develop a Java monitor or Ada protected record which implements the actions of a semaphore.
2. Write a solution to the above program using that semaphore implementation for synchronization.Your Java or Ada solution should have a single command-line argument representing the number of partiers.
3. Write a solution to the above program without using the semaphore implementation.This program can use a separate class or protected record for synchronization but it should be one specially developed for the program(rather than a generic semaphore).It should not be exactly equivalent to the solution of 2). Again, use a single command-line argument representing the number of partiers.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you and This Guy are working on the same homework assignment. Perhaps post a response in that thread and see how far he got. Maybe you two can work together to understand the concepts.

Otherwise, ask a specific question that you are having trouble with and we will try to help. We won't do your homework for you but we can help you get out around a mental block if you tell us what you tried, and how it doesn't work. (code is nice)
 
Henry Wong
author
Marshal
Pie
Posts: 20907
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a side note, Java has a Semaphore class, which implements a counting semaphore. This is *not* what is described here. What is described here is simply an assignment, and it is unclear how a "semaphore" is to be used to implement it.

Henry
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could think of two ways:
1) The keg has 100 drinks, if you think of each drink as a permission, then a semaphore would be a good approximation of the keg (which the partiers take permissions from, and when they are gone the pledge releases all permissions).
2) A binary semaphore works like a mutually exclusive lock that one thread locks and another thread can unlock. This would be a nice mechanism for the pledge to wait on (get permission to do work from semaphore) until the partier tells him the keg needs refilling (release permission to do work).
 
raj raval
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now when i compile then it's giving me following error
java 2 : class semaphore is public and should be declared in a file named semaphore public class ...
[ November 06, 2008: Message edited by: raj raval ]
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you go back and edit your post so it has code tags around the code. You can edit the post by pressing the edit icon ( ) then generate the code tags by pressing the CODE button at the bottom of the edit window.

Also, you said that it gives you an error, can you tell us what the error is and more details about when it occurs?
 
Henry Wong
author
Marshal
Pie
Posts: 20907
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmm... I added the code tags to your post, and it still seems messy. Let me reformat it -- doing it as a separate post.

Here is your code... reformatted:


[ November 06, 2008: Message edited by: Henry Wong ]
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by raj raval:
Now when i compile then it's giving me following error
java 2 : class semaphore is public and should be declared in a file named semaphore public class ...

[ November 06, 2008: Message edited by: raj raval ]


It looks like the name you gave the class does not match the name of the .java. If the class name is Semaphore then the file should be Semaphore.java. Also, case matters, so semaphore.java won't work.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This also looks to be essentially the same code used in the other thread. So be sure to read my comments in that thread.

An additional mistake you made in the code provided above is that you use wait(int) inside the run() method, while your class has a static Wait(int) method that it looks like you want to call. Remember that case matters in Java, so wait() and Wait() aren't the same method, and there is a special wait() method already present in all java Objects so you should avoid using it as a method name (just to prevent confusion).
 
Henry Wong
author
Marshal
Pie
Posts: 20907
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Luke:
This also looks to be essentially the same code used in the other thread. So be sure to read my comments in that thread.



Steve,

I did not notice this. Thanks for pointing it out.


Raj,

Shame on you. It looks like you simply took the code from the other topic, and tried to pass it off as yours here. (I will give you the benefit of the doubt though, and assume that it wasn't intentional)

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic