forked from cirosantilli/java-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLivelock.java.off
More file actions
51 lines (40 loc) · 1.01 KB
/
Copy pathLivelock.java.off
File metadata and controls
51 lines (40 loc) · 1.01 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
/**
* Sample code that very often leads ot a livelock.
*
* TODO make work
*/
public class Livelock {
static class Person {
public final int id;
public int pos;
public Corridor corridor;
public Person(int id, int pos, Corridor corridor) {
this.id = id;
this.pos = pos;
}
public void switchPos() {
this.pos *= -1;
}
public void tryToPass() {
this.pos *= -1;
}
}
static class Corridor {
public Person[] persons;
public Corridor(Person[] persons) {
this.persons = persons;
}
static boolean canPass() {
return persons[0].pos*persons[1].pos < 0;
}
}
public static void main(String[] args) {
Person[] persons = { new Person(0, -1), new Person(0, -1) };
Corridor corridor = new Corridor(persons);
for ( int i = 0; i < 10000; i++ )
{
while ( ! canPass ) {
}
}
}
}