rendered paste body public class Runner
{
//data read in from text files.
public static String[] halls = ResidentInput.getHallNames();
public static int[] rooms = ResidentInput.getHallRooms();
public static String[] jerryHallList = ResidentInput.getEnrollRequestsJerry();
public static String[] tomHallList = ResidentInput.getEnrollRequestsTom();
public static void main(String args[])
{
Entry entry = new Entry();
Thread EnrollThreadTom = new Thread(new EntryPerson("Tom", entry));
Thread EnrollThreadJerry = new Thread(new EntryPerson("Jerry", entry));
EnrollThreadTom.start();
EnrollThreadJerry.start();
}
}
import java.lang.Runnable;
public class EntryPerson implements Runnable
{
//attributes
private String name;
private Entry entry;
//constructor
public EntryPerson(String name, Entry entry)
{
this.name = name;
this.entry = entry;
}
public synchronized void createEntry()
{
System.out.println(this.name);
}
//runnable
public void run()
{
for (int x=0; x<10; x++)
{
synchronized(entry)
{
//wait while other tread probably doens't work...
while(entry.beingUsed())
{
if (this.name.equalsIgnoreCase("Tom"))
try
{
System.out.println("waiting...");
entry.wait();
}
catch (InterruptedException e){}
}
}
entry.setBeingUsed(true);
synchronized(entry)
{
entry.notifyAll();
}
createEntry();
entry.setBeingUsed(false);
synchronized(entry)
{
entry.notifyAll();
}
}
}
}
public class Entry
{
boolean used = false;
public boolean beingUsed()
{
return used;
}
public void setBeingUsed(boolean used)
{
this.used = used;
}
}
import java.io.*;
import java.util.*;
public class ResidentInput {
public static String[] getHallNames() {
ArrayList<String> namesArray;
String[] names = null;
namesArray = loadFile("hallnames.txt");
if (namesArray != null) {
names = (String[])namesArray.toArray(new String[0]);
}
return names;
}
public static int[] getHallRooms() {
ArrayList roomsArray;
int[] rooms = null;
String intString;
roomsArray = loadFile("hallrooms.txt");
if ((roomsArray != null) && (roomsArray.size() > 0)) {
rooms = new int[roomsArray.size()];
for (int i = 0; i < roomsArray.size(); i++) {
intString = (String)roomsArray.get(i);
try {
rooms[i] = Integer.parseInt(intString);
}
catch (NumberFormatException e) {
rooms[i] = -1;
System.err.println("Error: bad value on line " + (i + 1) +
" of hallrooms.txt");
}
}
}
return rooms;
}
public static String[] getEnrollRequestsTom() {
ArrayList<String> namesArray;
String[] names = null;
namesArray = loadFile("requeststom.txt");
if (namesArray != null) {
names = (String[])namesArray.toArray(new String[0]);
}
return names;
}
public static String[] getEnrollRequestsJerry() {
ArrayList<String> namesArray;
String[] names = null;
namesArray = loadFile("requestsjerry.txt");
if (namesArray != null) {
names = (String[])namesArray.toArray(new String[0]);
}
return names;
}
protected static ArrayList<String> loadFile(String fileName) {
if ((fileName == null) || (fileName == ""))
throw new IllegalArgumentException();
String line;
ArrayList<String> lines = new ArrayList<String>();
try {
BufferedReader in = new BufferedReader(new FileReader(fileName));
if (!in.ready()) {
throw new IOException();
}
while ((line = in.readLine()) != null) {
if (!line.trim().equals("")) {
lines.add(line);
}
}
in.close();
}
catch (IOException e) {
System.out.println(e);
return null;
}
return lines;
}
}