-
Notifications
You must be signed in to change notification settings - Fork 49
/
MultiThreadVote.java
70 lines (59 loc) · 1.95 KB
/
MultiThreadVote.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
import elections.Vote;
import elections.count;
import java.util.Vector;
public class MultiThreadVote {
public static void main(String[] args) {
Vector votevec = new Vector(240); // creating a vote array for 240 votes
Vote a = new Vote(1, votevec);
a.start();
Vote b = new Vote(2, votevec);
b.start();
Vote c = new Vote(3, votevec);
c.start();
try {
a.join();
b.join();
c.join();
System.out.println("Voting has ended!");
} catch (Exception e) {
System.out.println(e);
}
count ac = new count(1, votevec);
count bc = new count(2, votevec);
count cc = new count(3, votevec);
ac.start();
bc.start();
cc.start();
try {
ac.join();
bc.join();
cc.join();
System.out.println("Counting has ended!");
} catch (Exception e) {
System.out.println(e);
}
int av = ac.count;
int bv = bc.count;
int cv = cc.count;
System.out.println("elections.Vote Vector:" + "\n" + votevec);
System.out.println(av + " votes for A");
System.out.println(bv + " votes for B");
System.out.println(cv + " votes for C");
if (av >= bv && av >= cv) {
if (av == bv || av == cv)
System.out.println("Tie in elections!");
else
System.out.println("A has won the elections!");
} else if (bv >= av && bv >= cv) {
if (av == bv || bv == cv)
System.out.println("Tie in elections!");
else
System.out.println("B has won the elections!");
} else if (cv >= av && cv >= bv) {
if (cv == bv || cv == av)
System.out.println("Tie in elections!");
else
System.out.println("C has won the elections!");
}
}
}