mirror of
https://github.com/jlengrand/design-patterns.git
synced 2026-03-10 08:11:17 +00:00
Queue actually works now, as well as Thread.
This commit is contained in:
@@ -36,7 +36,7 @@ public class CommandGenerator {
|
||||
private class CommandTimerTask extends TimerTask{
|
||||
|
||||
private int generatorId;
|
||||
private Queue queue;
|
||||
private volatile Queue queue;
|
||||
|
||||
public CommandTimerTask(int id, Queue queue){
|
||||
this.queue = queue;
|
||||
|
||||
@@ -3,34 +3,35 @@ package nl.lengrand.patterns.command;
|
||||
import nl.lengrand.patterns.command.commands.Command;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class CommandProcessor implements Runnable {
|
||||
|
||||
private Queue<Command> queue;
|
||||
private boolean running = false;
|
||||
private volatile Queue<Command> queue;
|
||||
private final AtomicBoolean running = new AtomicBoolean(true);
|
||||
|
||||
public CommandProcessor(Queue queue){
|
||||
this.queue = queue;
|
||||
}
|
||||
|
||||
public void start(){
|
||||
System.out.println("Starting processor");
|
||||
this.running = true;
|
||||
this.run();
|
||||
}
|
||||
|
||||
public void stop(){
|
||||
this.running = false;
|
||||
System.out.println("Stopping CommandProcessor");
|
||||
this.running.set(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while(!queue.isEmpty()){
|
||||
while(this.running.get()){
|
||||
// Command Processors process items in the queue as fast as possible
|
||||
var item = queue.poll();
|
||||
if(item != null) item.execute();
|
||||
System.out.println(queue.size() + " items left in queue");
|
||||
if(item != null) {
|
||||
item.execute();
|
||||
}
|
||||
else{
|
||||
System.out.println("Empty queue. Stopping Processor");
|
||||
this.stop();
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import nl.lengrand.patterns.command.commands.Command;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.SynchronousQueue;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
@@ -20,22 +21,24 @@ public class ProcessingQueueExample {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Queue<Command> queue = new LinkedList<>();
|
||||
int numberGenerators = 5;
|
||||
|
||||
|
||||
List<CommandGenerator> generators = IntStream.range(0, 5)
|
||||
.mapToObj(i -> new CommandGenerator(i + 1, queue, getRandomInterval()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
generators.forEach(g -> g.generate());
|
||||
Thread.sleep(2000);
|
||||
Thread.sleep(1000);
|
||||
|
||||
CommandProcessor processor = new CommandProcessor(queue);
|
||||
processor.start();
|
||||
Thread processorThread = new Thread(processor);
|
||||
System.out.println("Starting");
|
||||
processorThread.start();
|
||||
|
||||
Thread.sleep(200);
|
||||
generators.forEach(g -> g.stop());
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Returns a random integer between 1 and 1000 (representing between 1 and 1000 ms interval)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user