-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMover.java
More file actions
73 lines (58 loc) · 1.58 KB
/
Mover.java
File metadata and controls
73 lines (58 loc) · 1.58 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
/*
* Copyright 2018 ADVA Optical Networking SE. All rights reserved.
*
* Owner: tomaszd
*
* $Id: $
*/
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.*;
import static java.lang.Thread.sleep;
public class Mover implements Runnable{
private BlockingQueue<Move> queue;;
private static Mover instance;
private static Set<Move> currentMove;
public BlockingQueue<Move> getQueue() {
return queue;
}
private Mover(){
this.queue = new LinkedBlockingQueue<>();
this.currentMove = new HashSet<>();
}
public static Mover getInstance(){
if (instance == null){
instance = new Mover();
}
return instance;
}
public static void main(String[] args) {
System.out.println("In main function.");
Mover mover = Mover.getInstance();
ExecutorService executorService = Executors.newFixedThreadPool(2);
MovementProducer movementProducer = new SocketMovementProducer(mover.getQueue());
executorService.execute(movementProducer);
executorService.execute(mover);
executorService.shutdown();
}
@Override
public void run() {
Move move;
while (true) {
while ((move = queue.poll()) != null) {
updateCurrentMove(move);
System.out.format("Move %s received\n", move.getMove());
}
doMove();
}
}
private void updateCurrentMove(Move move){
currentMove.add(move);
}
private void doMove(){
if (currentMove.size() == 1){
System.out.println("Moving Robot to direction: " + currentMove.iterator().next().getMove());
}
currentMove.clear();
}
}