@@ -0,0 +1,3394 @@
+package reach.project.coreViews;%0A%0Aimport java.util.concurrent.BlockingDeque;%0Aimport java.util.concurrent.ConcurrentHashMap;%0Aimport java.util.concurrent.ExecutionException;%0Aimport java.util.concurrent.Executors;%0Aimport java.util.concurrent.Future;%0Aimport java.util.concurrent.LinkedBlockingDeque;%0Aimport java.util.concurrent.ScheduledExecutorService;%0Aimport java.util.concurrent.TimeUnit;%0A%0A/**%0A * Created by dexter on 4/12/14.%0A */%0Apublic class SchedulerWithTaskOrdering %7B%0A%0A    private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1000);%0A    //a map of taskQueues. We queue all tasks of same ID.%0A    private static final ConcurrentHashMap%3CLong, BlockingDeque%3CMyTask%3E%3E mapOfTasks = new ConcurrentHashMap%3C%3E(1000);%0A%0A    public boolean submitWithChecks(Runnable task, long ID, long interval) %7B%0A%0A%0A        final BlockingDeque%3CMyTask%3E queue;%0A        if(mapOfTasks.containsKey(ID)) queue = mapOfTasks.get(ID);%0A        else queue = new LinkedBlockingDeque%3C%3E(1000);%0A%0A        //At this point we have a valid queue object%0A%0A        try %7B%0A            //insert the task into the queue%0A            queue.putLast(new MyTask(task, ID, interval));%0A        %7D catch (InterruptedException e) %7B%0A            e.printStackTrace();%0A            return false;%0A        %7D%0A%0A        //If the queue was already present it will get Updated my previous queue.putLast operation%0A        //If the queue was not present, we put it in the map and start a new queueEater thread.%0A        if(!mapOfTasks.containsKey(ID)) %7B%0A            mapOfTasks.put(ID, queue);%0A            scheduler.submit(new QueueEater(ID)); //start a new task execution queue%0A        %7D%0A        return true;%0A    %7D%0A%0A    private class QueueEater implements Runnable %7B%0A%0A        //This queueEater will consume the queue with this taskID%0A        private final Long ID;%0A%0A        private QueueEater(Long id) %7B%0A            ID = id;%0A        %7D%0A%0A        @Override%0A        public void run() %7B%0A%0A            //QueueEater will poll the mapOfTasks for the queue Object with its associated ID%0A            while (mapOfTasks.containsKey(ID)) %7B%0A%0A                final BlockingDeque%3CMyTask%3E tasks = mapOfTasks.get(ID);%0A                if(tasks.size() == 0) %7B%0A                    mapOfTasks.remove(ID);%0A                    return; //if task queue empty kill this thread;%0A                %7D%0A%0A                final MyTask myTask;%0A                try %7B%0A                    myTask = tasks.takeFirst();%0A                    //schedule the task with given interval%0A                    final Future future = scheduler.schedule(myTask.getTask(), myTask.getInterval(), TimeUnit.SECONDS);%0A                    future.get(); //wait till this task gets executed before scheduling new task%0A                %7D catch (InterruptedException %7C ExecutionException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A%0A        %7D%0A    %7D%0A%0A    private class MyTask %7B%0A%0A        private final Runnable task;%0A        private final long ID;%0A        private final long interval;%0A%0A        public long getInterval() %7B%0A            return interval;%0A        %7D%0A%0A        public long getID() %7B%0A            return ID;%0A        %7D%0A%0A        public Runnable getTask() %7B%0A            return task;%0A        %7D%0A%0A        private MyTask(Runnable task, long id, long interval) %7B%0A            this.task = task;%0A            ID = id;%0A            this.interval = interval;%0A        %7D%0A    %7D%0A%7D%0A

Create SchedulerWithTaskOrdering.java