-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameScale.java
More file actions
61 lines (46 loc) · 1.54 KB
/
GameScale.java
File metadata and controls
61 lines (46 loc) · 1.54 KB
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
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class GameScale {
public static final int NUM_PLAYERS = 12;
public static void main(String[] args) {
Lock lock = new ReentrantLock();
int length = NUM_PLAYERS;
Player[] players = new Player[length];
for (int i=0; i < length; i++) {
Player player = new Player("player"+i, lock, null, null);
players[i] = player;
}
for (int i=0; i < length - 1; i++) {
players[i].setNextPlayer(players[i+1]);
}
players[length - 1].setNextPlayer(players[0]);
System.out.println("Game starting...!");
players[0].setPlay(true);
//Threads creation
Thread[] threads = new Thread[length];
for (int i=0; i < length; i++) {
Thread thread = new Thread(players[i]);
threads[i] = thread;
thread.start();
}
//Let the players play!
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//Tell the players to stop
for (Thread thread : threads) {
thread.interrupt();
}
//Don't progress main thread until all players have finished
try {
for (Thread thread : threads) {
thread.join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Game finished!");
}
}