-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBirthdayCounter1.java
157 lines (146 loc) · 4.27 KB
/
BirthdayCounter1.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Data Parallelism:
*
* Counts the Birthdays in each month using 3 different processes.
* This Birthday counter uses Data Parallelism to take a section of the text file
* and count the number of birthdays in each month.
* By: Cameron Gonzalez
*/
import java.io.*;
import java.util.*;
public class BirthdayCounter1
{
public static void main(String[] args) throws FileNotFoundException {
Counter counter = new Counter();
//create 3 different threads for counting
Thread t1 = new Thread(counter);
Thread t2 = new Thread(counter);
Thread t3 = new Thread(counter);
//Gets the time at the start of threads
long lStartTime = new Date().getTime();
t1.start();
t2.start();
t3.start();
try
{
if(t1.isAlive())
t1.join();
else if(t2.isAlive())
t2.join();
if(t3.isAlive())
t3.join();
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
//prints all recorded birthday tallies
counter.print();
//Record End Time
long lEndTime = new Date().getTime();
//Report Elapsed Time
long difference = lEndTime - lStartTime;
System.out.println("\nTime Elapsed: "+difference+" milliseconds.");
}
}
/**
* Counter is a task that counts the number of birthday each month using the Runnable interface.
*/
class Counter implements Runnable
{
//counters to hold the number of birthdays in each month
int jan;
int feb;
int mar;
int apr;
int may;
int jun;
int jul;
int aug;
int sep;
int oct;
int nov;
int dec;
File infile = new File("birthdays.txt");
Scanner scanner = new Scanner(infile);
Counter() throws FileNotFoundException
{
}
/**
* prints the number of birthdays in each month
*/
public void print()
{
System.out.printf("January: %d\nFebruary: %d\nMarch: %d\nApril: %d\nMay: %d\nJune: %d\nJuly: %d\nAugust: %d\n" +
"September: %d\nOctober: %d\nNovember: %d\nDecember: %d\n", jan, feb, mar, apr, may, jun, jul, aug, sep,
oct, nov, dec);
}
/**
* implements the Runnable interface and is executed at start of thread
* counts the number of birthdays
*/
public void run()
{
synchronized(this)
{
while(scanner.hasNext())
{
//counts the next 25 values
for (int i = 0; i < 25; i++)
{
String date = scanner.nextLine();
String[] tokens = date.split("/");
int month = Integer.parseInt(tokens[0]);
if(month == 1)
{
jan++;
}
else if(month == 2)
{
feb++;
}
else if(month == 3)
{
mar++;
}
else if(month == 4)
{
apr++;
}
else if(month == 5)
{
may++;
}
else if(month == 6)
{
jun++;
}
else if(month == 7)
{
jul++;
}
else if(month == 8)
{
aug++;
}
else if(month == 9)
{
sep++;
}
else if(month == 10)
{
oct++;
}
else if(month == 11)
{
nov++;
}
else if(month == 12)
{
dec++;
}
}
}
}
}
}