public synchronized void gridletCancel(int gridletId, int userId) {
    	SSGridlet sgl = null;			
        
        // The jobs whose execution time is larger than this time have to be 
    	// shifted forwards. There's no point in changing jobs that are
    	// scheduled to start before this time.
        double refTime = GridSim.clock();

        sgl = runningJobs.get(gridletId, userId);
        if (sgl != null) {
            if(sgl.getStatus() == Gridlet.SUCCESS ||
            		sgl.getActualFinishTime() <= refTime) {
            	super.sendCancelGridlet(GridSimTags.GRIDLET_CANCEL, 
            			null, gridletId, userId);
            	return; // cannot cancel the job
            } else {
            	runningJobs.remove(sgl);
            }
        }

        if(sgl == null) {
        	sgl = waitingJobs.get(gridletId, userId);
        	if (sgl != null) {
                waitingJobs.remove(sgl);
                refTime = sgl.getStartTime();
            }
        }

        // Job has not been found
        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(!sgl.hasReserved()) {
            removeGridlet(sgl);
    		compressSchedule(refTime, sgl.getStatus() == Gridlet.INEXEC);
    	}
        
    	//------------------- USED FOR DEBUGGING PURPOSES ONLY -----------------
    	visualizer.notifyListeners(super.get_id(), ActionType.SCHEDULE_CHANGED, true);
    	//----------------------------------------------------------------------
	
        // finalise and send the Gridlet back to user
    	sgl.finalizeGridlet();
        super.sendCancelGridlet(GridSimTags.GRIDLET_CANCEL, sgl.getGridlet(), gridletId, userId);
    }