-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTest.java
116 lines (105 loc) · 2.68 KB
/
HashTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//Written by Brennan McTernan
//CS511 Assignment 4
import java.io.*;
import java.util.concurrent.*;
//Starting point of each thread
final class threadStart implements Runnable
{
private String inLine;
public threadStart(String line)
{
inLine = line;
}
@Override
public void run()
{
try
{
HashTest.execute(inLine);
}
catch(InterruptedException ie)
{
System.out.println(Thread.currentThread().getName() + "Has encountered an error");
}
}
}
//HashTest reads in commands from in.txt and adds, removes, or prints out strings to or from the bucket structure
public class HashTest
{
private static ExecutorService service;
private static StringHash stringHash; //initializes hash table - stringHash
public static void main(String[] args)
{
// Check if usage is correct
if(args.length < 4)
{
System.out.println("usage: HashTest num_buckets sleep_time read_write name_of_file");
System.exit(0);
}
//See usage above
int numBuckets = Integer.parseInt(args[0]);
int sleepTime = Integer.parseInt(args[1]);
boolean readWrite = Boolean.parseBoolean(args[2]);
String fileName = args[3];
String line;
String [] tokens;
//Create hash table, stringHash, and the threadpool with 10 threads
stringHash = new StringHash(numBuckets, sleepTime, readWrite);
service = Executors.newFixedThreadPool(10);
//Read data in from in.txt file
try
{
BufferedReader reader = new BufferedReader(new FileReader(fileName));
while ((line = reader.readLine()) != null)
{
service.submit(new threadStart(line)); //Submit command to service
}
reader.close();
service.shutdown();
}
catch (FileNotFoundException ex)
{
System.exit(1);
//TODO complain to user
}
catch (IOException ex)
{
System.exit(1);
//TODO notify user
}
//Wait for all threads to terminate
try
{
service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
System.out.println("Exiting Hash Test");
}
catch(InterruptedException is)
{
System.out.println("Await termination has fialed.");
}
}
static void execute(String line) throws InterruptedException
{
String[] tokens;
tokens = line.split(" ");
// process line
switch(tokens[0])
{
case "put": //Add String into hash table
stringHash.put(tokens[1]);
break;
case "isPresent": //Checks if string is present
stringHash.isPresent(tokens[1]);
break;
case "removeIfPresent": //Removes string if present
stringHash.removeIfPresent(tokens[1]);
break;
case "putIfAbsent": //Adds string if not present
stringHash.putIfAbsent(tokens[1]);
break;
case "display": //Prints hash table
stringHash.display();
break;
}
}
}