Created
June 6, 2012 04:15
-
-
Save bharatkrishna/2879872 to your computer and use it in GitHub Desktop.
Classic Producer-Consumer problem rehashed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* The Classic Producer-Consumer problem rehashed | |
* | |
* “There is a room with many people in it. People can enter and exit the room at any time. | |
* Write a program to find the number of people in the room at any given time.” | |
*/ | |
class Room { | |
private int count; //head count | |
private boolean isFull; | |
private int capacity; | |
public Room(int capacity) | |
{ | |
isFull = false; | |
count = 0; | |
this.capacity = capacity; | |
} | |
public synchronized int enterRoom() | |
{ | |
while(isFull==true) // if room is full, then wait | |
{ | |
try | |
{ | |
wait(); | |
}catch(InterruptedException ex){} | |
} //enter the room and increment the count | |
count++; | |
if(count>=capacity) //if the room is full, put the house full board :) | |
{ | |
isFull=true; | |
notifyAll(); | |
} | |
return count; | |
} | |
public synchronized int exitRoom() | |
{ | |
while(isFull==false) // no one can exit an empty room! | |
{ | |
try | |
{ | |
wait(); | |
}catch(InterruptedException ex){} | |
} | |
count--; | |
if(count==0) | |
{ | |
isFull = false; | |
notifyAll(); | |
} | |
return count; | |
} | |
} | |
class Enter extends Thread // Analogous to Producer | |
{ | |
private Room room; | |
private int iterations; | |
public Enter(String name, int iterations, Room room) | |
{ | |
super(name); | |
this.room = room; | |
this.iterations = iterations; | |
} | |
public void run() | |
{ | |
for(int i=0;i<iterations;i++) | |
{ | |
int headCount = room.enterRoom(); | |
System.out.println(this.getName()+"\t\tHead count: " + headCount); | |
try | |
{ | |
Thread.sleep((long)(Math.random()*1000)); // people enter & exit the room in random intervals | |
} catch (InterruptedException e) {} | |
} | |
} | |
} | |
class Exit extends Thread // Analogous to Consumer | |
{ | |
private Room room; | |
private int iterations; | |
public Exit(String name, int iterations, Room room) | |
{ | |
super(name); | |
this.room = room; | |
this.iterations = iterations; | |
} | |
public void run() | |
{ | |
for(int i=0;i<iterations;i++) | |
{ | |
int headCount = room.exitRoom(); | |
System.out.println(this.getName()+"\t\tHead count: " + headCount); | |
try | |
{ | |
Thread.sleep((long)(Math.random()*1000)); | |
} catch (InterruptedException e) {} | |
} | |
} | |
} | |
public class RoomProblem | |
{ | |
public static void main(String args[]) | |
{ | |
int size = (int)(Math.random()*20)+1; // a random room size. Adding 1 to prevent the room size being 0 | |
System.out.println("Room capacity: "+size); | |
System.out.println(); | |
Room room = new Room(size); | |
Enter ent1 = new Enter("Entering Door 1",size,room); // The size parameter passed here | |
Enter ent2 = new Enter("Entering Door 2",size,room); // is just the number of times people can | |
Exit ext1 = new Exit("Exiting Door 1",size,room); // enter/exit. For convenience I've set it to the | |
Exit ext2 = new Exit("Exiting Door 2",size,room); // capacity of the room. As per the original problem this | |
ent1.start(); // is infinity. | |
ext2.start(); | |
ent2.start(); | |
ext1.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample Output:
Room capacity: 15
Entering Door 1 Head count: 1
Entering Door 2 Head count: 2
Entering Door 1 Head count: 3
Entering Door 1 Head count: 4
Entering Door 2 Head count: 5
Entering Door 2 Head count: 6
Entering Door 2 Head count: 7
Entering Door 1 Head count: 8
Entering Door 1 Head count: 9
Entering Door 1 Head count: 10
Entering Door 2 Head count: 11
Entering Door 1 Head count: 12
Entering Door 2 Head count: 13
Entering Door 1 Head count: 14
Entering Door 2 Head count: 15
Exiting Door 2 Head count: 13
Exiting Door 1 Head count: 14
Exiting Door 2 Head count: 12
Exiting Door 1 Head count: 11
Exiting Door 2 Head count: 10
Exiting Door 1 Head count: 9
Exiting Door 2 Head count: 8
Exiting Door 2 Head count: 7
Exiting Door 1 Head count: 6
Exiting Door 2 Head count: 5
Exiting Door 1 Head count: 4
Exiting Door 2 Head count: 3
Exiting Door 2 Head count: 2
Exiting Door 1 Head count: 1
Entering Door 2 Head count: 1
Exiting Door 1 Head count: 0
Entering Door 1 Head count: 2
Entering Door 2 Head count: 3
Entering Door 1 Head count: 4
Entering Door 2 Head count: 5
Entering Door 1 Head count: 6
Entering Door 2 Head count: 7
Entering Door 1 Head count: 8
Entering Door 2 Head count: 9
Entering Door 2 Head count: 10
Entering Door 1 Head count: 11
Entering Door 2 Head count: 12
Entering Door 2 Head count: 13
Entering Door 1 Head count: 14
Entering Door 1 Head count: 15
Exiting Door 2 Head count: 13
Exiting Door 1 Head count: 14
Exiting Door 2 Head count: 12
Exiting Door 1 Head count: 11
Exiting Door 2 Head count: 10
Exiting Door 1 Head count: 9
Exiting Door 2 Head count: 8
Exiting Door 2 Head count: 7
Exiting Door 1 Head count: 6
Exiting Door 2 Head count: 5
Exiting Door 1 Head count: 4
Exiting Door 1 Head count: 3
Exiting Door 2 Head count: 2
Exiting Door 1 Head count: 1
Exiting Door 1 Head count: 0