public synchronized void gridletCancel(int gridletId, int userId) {
    	double currentTime = GridSim.clock();
        boolean updateProfile = false;
        
        SSGridlet sgl = runningJobs.get(gridletId, userId);
        if (sgl != null) {
            // Test if job has finished 
            if(sgl.getStatus() == Gridlet.SUCCESS ||
            		sgl.getActualFinishTime() <= currentTime){
            	super.sendCancelGridlet(GridSimTags.GRIDLET_CANCEL, 
            			null, gridletId, userId);
            	return; 
            } else {
            	// remove from the queue before compressing the schedule
            	runningJobs.remove(sgl);
            	updateProfile = true;
            }
        }

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

        		if(sgl.getStartTime() > 0) { // the job is a pivot.
                	EasyBackFillingPartition pt = getPartition(sgl.getPartitionID()); 
                	pt.pivot = null;
                	updateProfile = true;
                }
            }
        }

        // Send a null job if the job could not be found
        if(sgl == null) {
        	logger.info(super.get_name() + " 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(this.get_id(), ActionType.ITEM_CANCELLED, true, sgl);
        //----------------------------------------------------------------------
       
        // remove/update entries of the job in the profile
        if(updateProfile) {
        	removeGridlet(sgl);
        	backfillGridlets();

        	//------------------- USED FOR DEBUGGING PURPOSES ONLY -----------------
        	visualizer.notifyListeners(this.get_id(), ActionType.SCHEDULE_CHANGED, true);
        	//----------------------------------------------------------------------
        }

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