@Override
    public synchronized void gridletCancel(int gridletId, int userId) {
    	double currentTime = GridSim.clock();
    	SSGridlet sgl = null;			
        boolean updateProfile = false;
        
        sgl = runningJobs.get(gridletId, userId);
        if (sgl != null) {
            if(sgl.getStatus() == Gridlet.SUCCESS ||
            		sgl.getActualFinishTime() <= currentTime){
            	super.sendCancelGridlet(GridSimTags.GRIDLET_CANCEL, 
            			null, gridletId, userId);
            	return; // job has completed and cannot be cancelled
            } else {
            	runningJobs.remove(sgl);
            	updateProfile = true;
            }
        }

        if(sgl == null) {
        	sgl = waitingJobs.get(gridletId, userId);
        	if (sgl != null) {
                waitingJobs.remove(sgl);
                if(sgl == pivot) {
                	pivot = null;
                	updateProfile = true;
                }
            }
        }

        if(sgl == null) {
            logger.info("Cannot find Gridlet #" + gridletId + " for User #" + userId);
            super.sendCancelGridlet(GridSimTags.GRIDLET_CANCEL, 
            		null, gridletId, userId);
            return;
        }
        
        sgl.setStatus(Gridlet.CANCELED);

    	//----------------- USED FOR DEBUGGING PURPOSES ONLY -------------------
        visualizer.notifyListeners(super.get_id(), ActionType.ITEM_CANCELLED, true, sgl);
        //----------------------------------------------------------------------
       
        if(updateProfile) {
        	removeGridlet(sgl);
        	backfillGridlets();

        	//------------------- USED FOR DEBUGGING PURPOSES ONLY -----------------
       		// Inform the listeners about the new schedule
        	visualizer.notifyListeners(super.get_id(), ActionType.SCHEDULE_CHANGED, true);
        	//----------------------------------------------------------------------
        }

    	sgl.finalizeGridlet();
        super.sendCancelGridlet(GridSimTags.GRIDLET_CANCEL, 
        		sgl.getGridlet(), gridletId, userId);
    }