-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfinite.java
More file actions
99 lines (80 loc) · 2.46 KB
/
Infinite.java
File metadata and controls
99 lines (80 loc) · 2.46 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
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
package javaapplication40;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Infinite
{
int CurrentMove;
Infinite()
{
CurrentMove = 1;
}
public static void main (String[] args)
{
System.out.println("Give the number of players: ");
Scanner keyboard = new Scanner(System.in);
Integer N = keyboard.nextInt();
System.out.println("Give the number of loops: ");
Integer Loops = keyboard.nextInt();
Infinite BG = new Infinite ();
ArrayList<Thread> players = new ArrayList<>();
int count=1;
for(int i=0; i<N; i++){
Thread Player = new MyThread(BG, N, Loops, count);
players.add(Player);
count++;
}
players.forEach((p) -> {
p.start();
});
players.forEach((p) -> {
try {
p.join();
} catch (InterruptedException ex) {
Logger.getLogger(Infinite.class.getName()).log(Level.SEVERE, null, ex);
}
});
System.out.println ("Thats All");
}
}
class MyThread extends Thread
{
Infinite Board;
Integer N;
Integer player;
Integer turn;
Integer Loops;
public MyThread (Infinite B, int N, int Loops , int player)
{
this.turn = player;
Board = B; this.N=N; this.player=player; this.Loops=Loops;
}
@Override
public void run ()
{
boolean EndIt = false;
while (!EndIt)
{
synchronized (Board)
{
while (Board.CurrentMove != turn)
{
try
{
Board.wait ();
}
catch (InterruptedException ex)
{
}
}
System.out.println ("Do Move : " + Board.CurrentMove + " Player: " + player);
Board.CurrentMove++;
turn=turn+N;
Board.notifyAll ();
}
//System.out.println(turn );
if ((Board.CurrentMove + N) > (Loops+1)) {EndIt = true;}
}
}
}