Java programming using semaphores and shared variables
Hey everyone, I have a task to perform at school and really don't know how to get started. I don't want the whole solution, but rather tips and how to get started with this. Sorry but I'm new to programming. Here's the task:
A bulletin board (40p) is used to post apartment rental notices. Each rental notice includes 3 tear-away tabs with the phone number on it. A group of n students scans the board looking for apartments. Each student randomly selects 3 different apartments, and tries to remove one tab from each of 3 corresponding notices. If a student succeeds in taking 3 tabs that s/he has selected, s/he leaves, otherwise s/he does not take any, pauses a random amount time, and then tries again with a new selection of 3 different apartments. The simulation ends when all students have left. Assume the board has space for roundup(n/3) notices. Develop a parallel program that simulates actions of the students using only semaphores for synchronization. Represent the students as concurrent processes. Be sure to declare and initialize shared variables that you use for process interaction and synchronization. Try to maximize concurrency. Shortly explain how your solution works and how it avoids deadlock.
update to include info provided as an answer
Here's my code so far! Am I in the right direction!?
private static Semaphore[] apartments;
public void setApartments()
{
apartments = new Semaphore[3];
for(int i = 0; i < 3; i++)
apartments[i] = new Semaphore(3);
}
@Override
public void run()
{
setApartments();
Random random = new Random();
while(counter < 3)
{
try
{
acquired = apartments[random.nextInt(3)].tryAcquire();
if(acquired)
System.out.println("Student" + id + " succeded.");
else
System.out.println("Student" + id + " failed.");
counter++;
}
catch(Exception e) {e.printStackTrace();}
}
}
I have one problem that i can't figure out. I have 4 threads running开发者_C百科 at the same time, talking one permit at a time from: static Semaphore tabsA = new Semaphore(3);
So i thread will run forever cause, it doesn't get any. But if I use tabsA.release(); when the thread didn't meet the requirements, all four threads can take the permits even when there are 3 permits in Semaphore(3). I could even run 10 threds, and it would work. How come?
As a rule, I don't like answering homework questions directly, but you might want to focus your research on the Random
class with its nextInt(int)
method, the List<T>
interface and its implementations, the Runnable
interface (and Thread
class), and the Semaphore
class with its tryAcquire()
and release()
methods).
I would suggest writing this serially (i.e. one student at a time) just to get your basic objects/methods/algorithms working properly. This should be relatively simple for you. Then come back when you start refactoring for parallelism and ask pertinent questions.
One approach might be to represent each rental notice as having a Semaphore for the number of available tabs.
Semaphores are basically a way of declaring that a limited number of resources exist. When a process attempts to acquire a Semaphore it is basically asking for a permit to use the resource that the Semaphore is symbolically guarding. When a process which has acquired a permit is done with the resource, it should release the permit so that another process can acquire it. If a process wants to wait (potentially forever) for a permit to be available in Java it calls Semaphore.acquire()
. If a process just wants to try to get a permit if it is immediately available or do something else otherwise, in Java it call Semaphore.tryAcquire()
.
精彩评论