Designing this algorithm a better way?
I am working a much more complex version of this (with vehicle moving in both X and Y directions)

I made this example to get onions on better ways to accomplish this.

 1. I have a vehicle moving in the X direction at a speed (24.5872 mps)
 2. I am simulating this by incrementing the X value every 100 ms using an executor (To keep its X position more accurate and real time)
 3. After each second, I send a message to another process with the xMin and xMax values of the line I just covered
 4. The other process will respond with a JMS message (usually instantly) telling me to stop if there was an "Pothole" in the previous X area (Message callback msg to a linkedblockingqueue).

The problem I have is with the "usually instantly" part.  If I do not get a response quick enough, I think it will throw off the whole timing of my algorithm.  What is a better way to handle this situation?

Here is some basic code of what I am trying to do:


    public class Mover implements MessageHandler {
    
    	private static final long CAR_UPDATE_RATE_IN_MS = 100;
    	private static double currX = 0;
    	private static double CONSTANT_SPEED_IN_MPS = 24.5872; // 55 mph
    	private static double increment = CONSTANT_SPEED_IN_MPS / (1000 / CAR_UPDATE_RATE_IN_MS);
    	static LinkedBlockingQueue<BaseMessage> messageQueue = new LinkedBlockingQueue<BaseMessage>(); // ms
    
    	private static int incrementor = 0;
    
    	public static void main(String[] args) {
    		startMoverExecutor();
    	}
    
    	private static void startMoverExecutor() {
    
    		ScheduledExecutorService mover = Executors.newSingleThreadScheduledExecutor();
    		mover.scheduleAtFixedRate((new Runnable() {
    
    			@Override
    			public void run() {
    				currX = incrementor * increment;
    
    				if (incrementor % (1000 / CAR_UPDATE_RATE_IN_MS) == 0) {
    					System.out.println(currX);
    
    					sendMessage(currX - CONSTANT_SPEED_IN_MPS, currX);
    
    					// do something
    					try {
    						messageQueue.poll(1000, TimeUnit.MILLISECONDS);
    						
    					} catch (InterruptedException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    
    				}
    				incrementor++;
    			}
    
    		}), 0, CAR_UPDATE_RATE_IN_MS, TimeUnit.MILLISECONDS);
    
    	}
    
    	@Override
    	public void handleMessage(BaseMessage msg) {
    		messageQueue.add(msg);
    
    	}
    
    	protected static void sendMessage(double firstX, double secondX) {
    		// sendMessage here
    
    	}
    
    }
Designing this algorithm a better way?
===

I am working a much more complex version of this (with vehicle moving in both X and Y directions)

I made this example to get onions on better ways to accomplish this.

 1. I have a vehicle moving in the X direction at a speed (24.5872 mps)
 2. I am simulating this by incrementing the X value every 100 ms using an executor (To keep its X position more accurate and real time)
 3. After each second, I send a message to another process with the xMin and xMax values of the line I just covered
 4. The other process will respond with a JMS message (usually instantly) telling me to stop if there was an "Pothole" in the previous X area (Message callback msg to a linkedblockingqueue).

The problem I have is with the "usually instantly" part.  If I do not get a response quick enough, I think it will throw off the whole timing of my algorithm.  What is a better way to handle this situation?

Here is some basic code of what I am trying to do:


    public class Mover implements MessageHandler {
    
    	private static final long CAR_UPDATE_RATE_IN_MS = 100;
    	private static double currX = 0;
    	private static double CONSTANT_SPEED_IN_MPS = 24.5872; // 55 mph
    	private static double increment = CONSTANT_SPEED_IN_MPS / (1000 / CAR_UPDATE_RATE_IN_MS);
    	static LinkedBlockingQueue<BaseMessage> messageQueue = new LinkedBlockingQueue<BaseMessage>(); // ms
    
    	private static int incrementor = 0;
    
    	public static void main(String[] args) {
    		startMoverExecutor();
    	}
    
    	private static void startMoverExecutor() {
    
    		ScheduledExecutorService mover = Executors.newSingleThreadScheduledExecutor();
    		mover.scheduleAtFixedRate((new Runnable() {
    
    			@Override
    			public void run() {
    				currX = incrementor * increment;
    
    				if (incrementor % (1000 / CAR_UPDATE_RATE_IN_MS) == 0) {
    					System.out.println(currX);
    
    					sendMessage(currX - CONSTANT_SPEED_IN_MPS, currX);
    
    					// do something
    					try {
    						messageQueue.poll(1000, TimeUnit.MILLISECONDS);
    						
    					} catch (InterruptedException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    
    				}
    				incrementor++;
    			}
    
    		}), 0, CAR_UPDATE_RATE_IN_MS, TimeUnit.MILLISECONDS);
    
    	}
    
    	@Override
    	public void handleMessage(BaseMessage msg) {
    		messageQueue.add(msg);
    
    	}
    
    	protected static void sendMessage(double firstX, double secondX) {
    		// sendMessage here
    
    	}
    
    }