This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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)
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.
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).
Joined: Nov 03, 2008
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 ]
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?
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).
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.
I did not notice this. Thanks for pointing it out.
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)