-
Notifications
You must be signed in to change notification settings - Fork 49
/
Vote.java
35 lines (28 loc) · 975 Bytes
/
Vote.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
package elections;
import java.util.Random;
import java.util.Vector;
public class Vote extends Thread {
Random rand = new Random(); // generating a random number
int max = 750; // max wait time for thread
int min = 100; // min wait time for thread
int v, s;
Vector vec;
public Vote(int v, Vector vec) {
this.v = v;
this.vec = vec;
}
public void run() {
try {
// while voting print id
while (vec.size() < 240) { // ensure size of vote vector is below 240
System.out.println(this.getName() + " is Voting");
vec.add(v);
s = rand.nextInt((max - min) + 1) + min;
System.out.println(this.getName() + " is sleeping for " + s);
Thread.sleep(s); // create random delay between threads
}
} catch (InterruptedException e) {
System.out.println("Voting Exception: " + e);
}
}
}