Example usage for java.util.concurrent.locks Lock lock

List of usage examples for java.util.concurrent.locks Lock lock

Introduction

In this page you can find the example usage for java.util.concurrent.locks Lock lock.

Prototype

lock

Source Link

Usage

From source file:org.unitime.timetable.solver.exam.ExamSolver.java

public Collection<ExamAssignmentInfo> getAssignedExamsOfInstructor(Long instructorId) {
    Lock lock = currentSolution().getLock().readLock();
    lock.lock();
    try {//  w  ww  . jav a2s .  c  o m
        ExamInstructor instructor = null;
        for (ExamInstructor i : ((ExamModel) currentSolution().getModel()).getInstructors()) {
            if (i.getId() == instructorId) {
                instructor = i;
                break;
            }
        }
        if (instructor == null)
            return null;
        Vector<ExamAssignmentInfo> ret = new Vector<ExamAssignmentInfo>();
        for (ExamPeriod period : ((ExamModel) currentSolution().getModel()).getPeriods()) {
            Set exams = instructor.getExams(currentSolution().getAssignment(), period);
            if (exams != null)
                for (Iterator i = exams.iterator(); i.hasNext();) {
                    Exam exam = (Exam) i.next();
                    ret.add(new ExamAssignmentInfo(currentSolution().getAssignment().getValue(exam),
                            currentSolution().getAssignment()));
                }
        }
        return ret;
    } finally {
        lock.unlock();
    }
}

From source file:org.unitime.timetable.solver.exam.ExamSolver.java

public Collection<ExamAssignmentInfo[]> getChangesToBest(Long subjectAreaId) {
    String sa = (subjectAreaId != null && subjectAreaId >= 0
            ? new SubjectAreaDAO().get(subjectAreaId).getSubjectAreaAbbreviation() + " "
            : null);/*  w w  w .  ja  v  a 2s .co m*/
    Vector<ExamAssignmentInfo[]> changes = new Vector<ExamAssignmentInfo[]>();
    Lock lock = currentSolution().getLock().readLock();
    lock.lock();
    try {
        for (Exam exam : currentSolution().getModel().variables()) {
            if (sa != null) {
                boolean hasSubjectArea = false;
                for (Iterator<ExamOwner> f = exam.getOwners().iterator(); !hasSubjectArea && f.hasNext();) {
                    ExamOwner ecs = f.next();
                    hasSubjectArea = ecs.getName().startsWith(sa);
                }
                if (!hasSubjectArea)
                    continue;
            }
            if (!ToolBox.equals(exam.getBestAssignment(), currentSolution().getAssignment().getValue(exam))) {
                changes.add(new ExamAssignmentInfo[] {
                        new ExamAssignmentInfo(exam, exam.getBestAssignment(),
                                currentSolution().getAssignment()),
                        new ExamAssignmentInfo(exam, currentSolution().getAssignment().getValue(exam),
                                currentSolution().getAssignment()) });
            }
        }
    } finally {
        lock.unlock();
    }
    return changes;
}

From source file:org.unitime.timetable.solver.exam.ExamSolver.java

public Collection<ExamAssignmentInfo[]> getChangesToInitial(Long subjectAreaId) {
    String sa = (subjectAreaId != null && subjectAreaId >= 0
            ? new SubjectAreaDAO().get(subjectAreaId).getSubjectAreaAbbreviation() + " "
            : null);/*w  w w  .  j  ava2  s.  c om*/
    Vector<ExamAssignmentInfo[]> changes = new Vector<ExamAssignmentInfo[]>();
    Lock lock = currentSolution().getLock().readLock();
    lock.lock();
    try {
        for (Exam exam : currentSolution().getModel().variables()) {
            if (sa != null) {
                boolean hasSubjectArea = false;
                for (Iterator<ExamOwner> f = exam.getOwners().iterator(); !hasSubjectArea && f.hasNext();) {
                    ExamOwner ecs = f.next();
                    hasSubjectArea = ecs.getName().startsWith(sa);
                }
                if (!hasSubjectArea)
                    continue;
            }
            if (!ToolBox.equals(exam.getInitialAssignment(),
                    currentSolution().getAssignment().getValue(exam))) {
                changes.add(new ExamAssignmentInfo[] {
                        new ExamAssignmentInfo(exam, exam.getInitialAssignment(),
                                currentSolution().getAssignment()),
                        new ExamAssignmentInfo(exam, currentSolution().getAssignment().getValue(exam),
                                currentSolution().getAssignment()) });
            }
        }
    } finally {
        lock.unlock();
    }
    return changes;
}

From source file:net.myrrix.online.ServerRecommender.java

@Override
public List<RecommendedItem> mostPopularItems(int howMany, IDRescorer rescorer) throws NotReadyException {

    Preconditions.checkArgument(howMany > 0, "howMany must be positive");

    Generation generation = getCurrentGeneration();
    FastByIDMap<FastIDSet> knownItemIDs = generation.getKnownItemIDs();
    if (knownItemIDs == null) {
        throw new UnsupportedOperationException();
    }//from  w w w  .  java  2 s . c om

    FastIDSet itemTagIDs = generation.getItemTagIDs();
    FastByIDFloatMap itemCounts = new FastByIDFloatMap();
    Lock knownItemReadLock = generation.getKnownItemLock().readLock();
    knownItemReadLock.lock();
    try {
        // Don't count data from users that are really item tags
        Lock xReadLock = generation.getXLock().readLock();
        xReadLock.lock();
        try {

            for (FastByIDMap.MapEntry<FastIDSet> entry : generation.getKnownItemIDs().entrySet()) {
                long userID = entry.getKey();
                if (!itemTagIDs.contains(userID)) {
                    FastIDSet itemIDs = entry.getValue();
                    synchronized (itemIDs) {
                        LongPrimitiveIterator it = itemIDs.iterator();
                        while (it.hasNext()) {
                            long itemID = it.nextLong();
                            itemCounts.increment(itemID, 1.0f);
                        }
                    }
                }
            }

        } finally {
            xReadLock.unlock();
        }
    } finally {
        knownItemReadLock.unlock();
    }

    // Filter out 'items' that were really user tags
    FastIDSet userTagIDs = generation.getUserTagIDs();
    Lock yReadLock = generation.getYLock().readLock();
    yReadLock.lock();
    try {
        LongPrimitiveIterator it = itemCounts.keySetIterator();
        while (it.hasNext()) {
            if (userTagIDs.contains(it.nextLong())) {
                it.remove();
            }
        }
    } finally {
        yReadLock.unlock();
    }

    return TopN.selectTopN(new MostPopularItemsIterator(itemCounts.entrySet().iterator(), rescorer), howMany);
}

From source file:org.unitime.timetable.solver.exam.ExamSolver.java

public ExamPlacement getPlacement(ExamAssignment assignment) {
    Lock lock = currentSolution().getLock().readLock();
    lock.lock();
    try {/*w  w w  . jav a  2  s  .c  o m*/
        Exam exam = getExam(assignment.getExamId());
        if (exam == null)
            return null;
        ExamPeriodPlacement period = null;
        for (ExamPeriodPlacement p : exam.getPeriodPlacements()) {
            if (p.getId().equals(assignment.getPeriodId())) {
                period = p;
                break;
            }
        }
        if (period == null)
            return null;
        HashSet rooms = new HashSet();
        for (ExamRoomInfo roomInfo : assignment.getRooms()) {
            Long roomId = roomInfo.getLocationId();
            ExamRoomPlacement room = null;
            for (ExamRoomPlacement r : exam.getRoomPlacements()) {
                if (r.getId() == roomId) {
                    room = r;
                    break;
                }
            }
            if (room != null)
                rooms.add(room);
        }
        return new ExamPlacement(exam, period, rooms);
    } finally {
        lock.unlock();
    }
}

From source file:org.unitime.timetable.solver.exam.ExamSolver.java

public ExamAssignmentInfo getAssignment(Long examId, Long periodId, Collection<Long> roomIds) {
    Lock lock = currentSolution().getLock().readLock();
    lock.lock();
    try {/* www  .  j  av a2  s .  c  o  m*/
        Exam exam = getExam(examId);
        if (exam == null)
            return null;
        ExamPeriodPlacement period = null;
        for (ExamPeriodPlacement p : exam.getPeriodPlacements()) {
            if (p.getId().equals(periodId)) {
                period = p;
                break;
            }
        }
        if (period == null)
            return null;
        HashSet rooms = new HashSet();
        for (Long roomId : roomIds) {
            ExamRoomPlacement room = null;
            for (ExamRoomPlacement r : exam.getRoomPlacements()) {
                if (r.getId() == roomId) {
                    room = r;
                    break;
                }
            }
            if (room != null)
                rooms.add(room);
        }
        return new ExamAssignmentInfo(exam, new ExamPlacement(exam, period, rooms),
                currentSolution().getAssignment());
    } finally {
        lock.unlock();
    }
}

From source file:org.unitime.timetable.solver.exam.ExamSolver.java

protected void onFinish() {
    super.onFinish();
    try {// w w w .j  a  v a  2  s  . c o  m
        iWorking = true;
        if (currentSolution().getBestInfo() != null)
            currentSolution().restoreBest();
        if (currentSolution().getBestInfo() != null
                && getProperties().getPropertyBoolean("General.Save", false)) {
            ExamDatabaseSaver saver = new ExamDatabaseSaver(this);
            Lock lock = currentSolution().getLock().readLock();
            lock.lock();
            try {
                saver.save();
            } finally {
                lock.unlock();
            }
        }
        if (getProperties().getPropertyBoolean("General.Unload", false)) {
            dispose();
        } else {
            Progress.getInstance(currentSolution().getModel()).setStatus("Awaiting commands ...");
        }
    } finally {
        iWorking = false;
    }
}

From source file:org.unitime.timetable.solver.exam.ExamSolver.java

public ExamSuggestionsInfo getSuggestions(long examId, ExamProposedChange change, String filter, int depth,
        int limit, long timeOut) {
    Lock lock = currentSolution().getLock().writeLock();
    lock.lock();
    try {// ww w  . j  a  va  2s.  c  om
        Exam exam = getExam(examId);
        if (exam == null)
            return null;
        ExamSuggestions s = new ExamSuggestions(this);
        s.setDepth(depth);
        s.setFilter(filter);
        s.setLimit(limit);
        s.setTimeOut(timeOut);
        TreeSet<ExamProposedChange> suggestions = s.computeSuggestions(exam,
                (change == null ? null : change.getAssignments()));
        String message = null;
        if (s.wasTimeoutReached()) {
            message = "(" + (timeOut / 1000l) + "s timeout reached, " + s.getNrCombinationsConsidered()
                    + " possibilities up to " + depth + " changes were considered, ";
        } else {
            message = "(all " + s.getNrCombinationsConsidered() + " possibilities up to " + depth
                    + " changes were considered, ";
        }
        if (suggestions.isEmpty()) {
            message += "no suggestion found)";
        } else if (s.getNrSolutions() > suggestions.size()) {
            message += "top " + suggestions.size() + " of " + s.getNrSolutions() + " suggestions displayed)";
        } else {
            message += suggestions.size() + " suggestions displayed)";
        }
        return new ExamSuggestionsInfo(suggestions, message, s.wasTimeoutReached());
    } finally {
        lock.unlock();
    }
}

From source file:com.netprogs.minecraft.plugins.social.SocialPerson.java

public String getName() {
    Lock lock = rwNameLock.readLock();
    lock.lock();
    try {//from  w  w  w  .  ja v  a  2 s.c  o m
        return person.getName();
    } finally {
        lock.unlock();
    }
}

From source file:com.netprogs.minecraft.plugins.social.SocialPerson.java

public ICommandType getWaitCommand() {
    Lock lock = rwWaitLock.readLock();
    lock.lock();
    try {/* www.j a v a  2s  .  com*/
        return person.getWaitCommand();
    } finally {
        lock.unlock();
    }
}