List of usage examples for java.util.concurrent.locks Lock lock
lock
From source file:com.netprogs.minecraft.plugins.social.SocialPerson.java
/** * Returns all the messages of the given type for the given player name. * This version allows us to not create a copy allowing us to work with it internally. * @param fromPlayerName/*from w ww. ja va 2s . c om*/ * @param messageClass * @return */ @SuppressWarnings("unchecked") private <U extends IMessage> List<U> getMessagesFrom(String fromPersonName, Class<U> messageClass, boolean createCopy) { Lock lock = rwMessageQueueLock.readLock(); lock.lock(); try { String className = messageClass.getCanonicalName(); if (person.getMessageQueue().containsKey(className)) { if (person.getMessageQueue().get(className).containsKey(fromPersonName)) { // send back a copy of the items instead of the original list List<U> messages = (List<U>) person.getMessageQueue().get(className).get(fromPersonName); if (createCopy) { return Collections.unmodifiableList(messages); } else { return messages; } } } // return an empty list return Collections.emptyList(); } finally { lock.unlock(); } }
From source file:org.unitime.timetable.solver.exam.ExamSolver.java
public Collection<ExamAssignmentInfo> getPeriods(long examId, ExamProposedChange change) { Lock lock = currentSolution().getLock().readLock(); lock.lock(); try {// w ww. ja va 2 s . c om //lookup exam Exam exam = getExam(examId); if (exam == null) return null; //assign change Hashtable<Exam, ExamPlacement> undoAssign = new Hashtable(); HashSet<Exam> undoUnassing = new HashSet(); if (change != null) { for (ExamAssignment assignment : change.getConflicts()) { ExamPlacement placement = getPlacement(assignment); if (placement == null) continue; undoAssign.put((Exam) placement.variable(), placement); currentSolution().getAssignment().unassign(0, placement.variable()); } for (ExamAssignment assignment : change.getAssignments()) { ExamPlacement placement = getPlacement(assignment); if (placement == null) continue; for (Iterator i = placement.variable().getModel() .conflictValues(currentSolution().getAssignment(), placement).iterator(); i .hasNext();) { ExamPlacement conflict = (ExamPlacement) i.next(); if (conflict.variable().equals(placement.variable())) continue; Exam conflictingExam = (Exam) conflict.variable(); if (!undoAssign.containsKey(conflictingExam) && !undoUnassing.contains(conflictingExam)) undoAssign.put(conflictingExam, conflict); currentSolution().getAssignment().unassign(0, conflict.variable()); } if (currentSolution().getAssignment().getValue(placement.variable()) != null) undoAssign.put((Exam) placement.variable(), currentSolution().getAssignment().getValue(placement.variable())); else undoUnassing.add((Exam) placement.variable()); currentSolution().getAssignment().assign(0, placement); } } Vector<ExamAssignmentInfo> periods = new Vector<ExamAssignmentInfo>(); for (ExamPeriodPlacement period : exam.getPeriodPlacements()) { Set rooms = exam.findBestAvailableRooms(currentSolution().getAssignment(), period); if (rooms == null) rooms = new HashSet(); boolean conf = !exam.checkDistributionConstraints(currentSolution().getAssignment(), period); ExamAssignmentInfo assignment = new ExamAssignmentInfo(new ExamPlacement(exam, period, rooms), currentSolution().getAssignment()); if (conf) assignment.setPeriodPref("P"); periods.add(assignment); } //undo change for (Exam undoExam : undoUnassing) if (currentSolution().getAssignment().getValue(undoExam) != null) currentSolution().getAssignment().unassign(0, undoExam); for (Map.Entry<Exam, ExamPlacement> entry : undoAssign.entrySet()) currentSolution().getAssignment().unassign(0, entry.getKey()); for (Map.Entry<Exam, ExamPlacement> entry : undoAssign.entrySet()) currentSolution().getAssignment().assign(0, entry.getValue()); return periods; } finally { lock.unlock(); } }
From source file:net.myrrix.online.ServerRecommender.java
private void removePreference(long userID, long itemID, boolean bulk) { // Record datum try {/*from ww w .j ava 2 s . c o m*/ generationManager.remove(userID, itemID, bulk); } catch (IOException ioe) { log.warn("Could not append datum; continuing", ioe); } Generation generation; try { generation = getCurrentGeneration(); } catch (NotReadyException nre) { // Corner case -- no model ready so all we can do is record (above). Don't fail the request. return; } ReadWriteLock knownItemLock = generation.getKnownItemLock(); boolean removeUser = false; FastByIDMap<FastIDSet> knownItemIDs = generation.getKnownItemIDs(); if (knownItemIDs != null) { Lock knownItemReadLock = knownItemLock.readLock(); FastIDSet userKnownItemIDs; knownItemReadLock.lock(); try { userKnownItemIDs = knownItemIDs.get(userID); } finally { knownItemReadLock.unlock(); } if (userKnownItemIDs == null) { // Doesn't exist? So ignore this request return; } synchronized (userKnownItemIDs) { if (!userKnownItemIDs.remove(itemID)) { // Item unknown, so ignore this request return; } removeUser = userKnownItemIDs.isEmpty(); } } // We can proceed with the request FastByIDMap<float[]> X = generation.getX(); ReadWriteLock xLock = generation.getXLock(); if (removeUser) { Lock knownItemWriteLock = knownItemLock.writeLock(); knownItemWriteLock.lock(); try { knownItemIDs.remove(userID); } finally { knownItemWriteLock.unlock(); } Lock xWriteLock = xLock.writeLock(); xWriteLock.lock(); try { X.remove(userID); } finally { xWriteLock.unlock(); } } }
From source file:com.netprogs.minecraft.plugins.social.SocialPerson.java
public <U extends IMessage> void removeMessage(SocialPerson fromPerson, U message) { String fromPlayerName = fromPerson.getName(); Lock lock = rwMessageQueueLock.writeLock(); lock.lock(); try {//from www . j av a 2 s . co m String className = message.getClass().getCanonicalName(); // check to make sure there is a message of this type in the queue if (person.getMessageQueue().containsKey(className)) { // check to see if that list of types contains an entry for the player if (person.getMessageQueue().get(className).containsKey(fromPlayerName)) { // remove the message from the list person.getMessageQueue().get(className).get(fromPlayerName).remove(message); // if the list is empty now, remove it from the map, just to save object memory if (person.getMessageQueue().get(className).get(fromPlayerName).size() == 0) { person.getMessageQueue().get(className).remove(fromPlayerName); } // if the message type list is empty, remove it also, just to save object memory if (person.getMessageQueue().get(className).keySet().size() == 0) { person.getMessageQueue().remove(className); } } } } finally { lock.unlock(); } }
From source file:com.netprogs.minecraft.plugins.social.SocialPerson.java
@SuppressWarnings("unchecked") public <U extends IMessage> void addMessage(SocialPerson fromPerson, U message) { Lock lock = rwMessageQueueLock.writeLock(); lock.lock(); try {//from www. j a v a2 s.co m // check to see if this message type has a queue entry, if not, make one String className = message.getClass().getCanonicalName(); SocialNetworkPlugin.log("Adding message entry: [" + fromPerson.getName() + ", " + message + "]"); // If there isn't an entry for this message type, then create one Map<String, List<? extends IMessage>> playerMessageMap = person.getMessageQueue().get(className); if (playerMessageMap == null) { playerMessageMap = new HashMap<String, List<? extends IMessage>>(); person.getMessageQueue().put(className, playerMessageMap); } // If there isn't an entry for given fromPerson, then create their list now List<U> messageList = (List<U>) playerMessageMap.get(fromPerson.getName()); if (messageList == null) { SocialNetworkPlugin .log("Creating new message entry: [" + fromPerson.getName() + ", " + message + "]"); messageList = new ArrayList<U>(); playerMessageMap.put(fromPerson.getName(), messageList); } // add the message messageList.add(message); } finally { lock.unlock(); } }
From source file:org.unitime.timetable.solver.exam.ExamSolver.java
public Vector<ExamRoomInfo> getRooms(long examId, long periodId, ExamProposedChange change, int minRoomSize, int maxRoomSize, String filter, boolean allowConflicts) { Lock lock = currentSolution().getLock().readLock(); lock.lock(); try {/*from w ww.ja v a 2 s.c om*/ //lookup exam, period etc. Exam exam = getExam(examId); if (exam == null) return null; ExamPeriodPlacement period = null; for (ExamPeriodPlacement p : exam.getPeriodPlacements()) { if (periodId == p.getId()) { period = p; break; } } if (period == null) return null; Vector<ExamRoomInfo> rooms = new Vector<ExamRoomInfo>(); if (exam.getMaxRooms() == 0) return rooms; //assign change Hashtable<Exam, ExamPlacement> undoAssign = new Hashtable(); HashSet<Exam> undoUnassing = new HashSet(); if (change != null) { for (ExamAssignment assignment : change.getConflicts()) { ExamPlacement placement = getPlacement(assignment); if (placement == null) continue; undoAssign.put((Exam) placement.variable(), placement); currentSolution().getAssignment().unassign(0, placement.variable()); } for (ExamAssignment assignment : change.getAssignments()) { ExamPlacement placement = getPlacement(assignment); if (placement == null) continue; for (Iterator i = placement.variable().getModel() .conflictValues(currentSolution().getAssignment(), placement).iterator(); i .hasNext();) { ExamPlacement conflict = (ExamPlacement) i.next(); if (conflict.variable().equals(placement.variable())) continue; Exam conflictingExam = (Exam) conflict.variable(); if (!undoAssign.containsKey(conflictingExam) && !undoUnassing.contains(conflictingExam)) undoAssign.put(conflictingExam, conflict); currentSolution().getAssignment().unassign(0, conflict.variable()); } if (currentSolution().getAssignment().getValue(placement.variable()) != null) undoAssign.put((Exam) placement.variable(), currentSolution().getAssignment().getValue(placement.variable())); else undoUnassing.add((Exam) placement.variable()); currentSolution().getAssignment().assign(0, placement); } } ExamRoomSharing sharing = ((ExamModel) currentSolution().getModel()).getRoomSharing(); //compute rooms for (ExamRoomPlacement room : exam.getRoomPlacements()) { int cap = room.getSize(exam.hasAltSeating()); if (minRoomSize >= 0 && cap < minRoomSize) continue; if (maxRoomSize >= 0 && cap > maxRoomSize) continue; if (!ExamInfoModel.match(room.getName(), filter)) continue; if (!room.isAvailable(period.getPeriod())) continue; boolean conf = !exam.checkDistributionConstraints(currentSolution().getAssignment(), room); if (sharing == null) { for (ExamPlacement p : room.getRoom().getPlacements(currentSolution().getAssignment(), period.getPeriod())) if (!p.variable().equals(exam)) conf = true; } else { if (sharing.inConflict(exam, room.getRoom().getPlacements(currentSolution().getAssignment(), period.getPeriod()), room.getRoom())) conf = true; } if (!allowConflicts && conf) continue; rooms.add(new ExamRoomInfo(room.getRoom(), (conf ? 100 : 0) + room.getPenalty(period.getPeriod()))); } //undo change for (Exam undoExam : undoUnassing) if (currentSolution().getAssignment().getValue(undoExam) != null) currentSolution().getAssignment().unassign(0, undoExam); for (Map.Entry<Exam, ExamPlacement> entry : undoAssign.entrySet()) currentSolution().getAssignment().unassign(0, entry.getKey()); for (Map.Entry<Exam, ExamPlacement> entry : undoAssign.entrySet()) currentSolution().getAssignment().assign(0, entry.getValue()); return rooms; } finally { lock.unlock(); } }
From source file:net.myrrix.online.ServerRecommender.java
@Override public List<RecommendedItem> recommendToMany(long[] userIDs, int howMany, boolean considerKnownItems, IDRescorer rescorer) throws NoSuchUserException, NotReadyException { Preconditions.checkArgument(howMany > 0, "howMany must be positive"); Generation generation = getCurrentGeneration(); FastByIDMap<float[]> X = generation.getX(); Lock xLock = generation.getXLock().readLock(); List<float[]> userFeatures = Lists.newArrayListWithCapacity(userIDs.length); xLock.lock(); try {//from w ww.ja v a2 s . c o m for (long userID : userIDs) { float[] theUserFeatures = X.get(userID); if (theUserFeatures != null) { userFeatures.add(theUserFeatures); } } } finally { xLock.unlock(); } if (userFeatures.isEmpty()) { throw new NoSuchUserException(Arrays.toString(userIDs)); } FastByIDMap<FastIDSet> knownItemIDs = generation.getKnownItemIDs(); if (knownItemIDs == null && !considerKnownItems) { throw new UnsupportedOperationException("Can't ignore known items because no known items available"); } FastIDSet usersKnownItemIDs = null; if (!considerKnownItems) { Lock knownItemLock = generation.getKnownItemLock().readLock(); knownItemLock.lock(); try { for (long userID : userIDs) { FastIDSet theKnownItemIDs = knownItemIDs.get(userID); if (theKnownItemIDs == null) { continue; } if (usersKnownItemIDs == null) { usersKnownItemIDs = theKnownItemIDs; } else { LongPrimitiveIterator it = usersKnownItemIDs.iterator(); while (it.hasNext()) { if (!theKnownItemIDs.contains(it.nextLong())) { it.remove(); } } } if (usersKnownItemIDs.isEmpty()) { break; } } } finally { knownItemLock.unlock(); } } float[][] userFeaturesArray = userFeatures.toArray(new float[userFeatures.size()][]); Lock yLock = generation.getYLock().readLock(); yLock.lock(); try { return multithreadedTopN(userFeaturesArray, usersKnownItemIDs, generation.getUserTagIDs(), rescorer, howMany, generation.getCandidateFilter()); } finally { yLock.unlock(); } }
From source file:net.myrrix.online.ServerRecommender.java
private float[] buildAnonymousUserFeatures(long[] itemIDs, float[] values) throws NotReadyException, NoSuchItemException { Preconditions.checkArgument(values == null || values.length == itemIDs.length, "Number of values doesn't match number of items"); Generation generation = getCurrentGeneration(); FastByIDMap<float[]> Y = generation.getY(); Solver ytySolver = generation.getYTYSolver(); if (ytySolver == null) { throw new NotReadyException(); }//from w w w . j a v a2s . com float[] anonymousUserFeatures = null; Lock yLock = generation.getYLock().readLock(); boolean anyItemIDFound = false; for (int j = 0; j < itemIDs.length; j++) { long itemID = itemIDs[j]; float[] itemFeatures; yLock.lock(); try { itemFeatures = Y.get(itemID); } finally { yLock.unlock(); } if (itemFeatures == null) { continue; } anyItemIDFound = true; double[] userFoldIn = ytySolver.solveFToD(itemFeatures); if (anonymousUserFeatures == null) { anonymousUserFeatures = new float[userFoldIn.length]; } double signedFoldInWeight = foldInWeight(0.0, values == null ? 1.0f : values[j]); if (signedFoldInWeight != 0.0) { for (int i = 0; i < anonymousUserFeatures.length; i++) { anonymousUserFeatures[i] += (float) (signedFoldInWeight * userFoldIn[i]); } } } if (!anyItemIDFound) { throw new NoSuchItemException(Arrays.toString(itemIDs)); } return anonymousUserFeatures; }
From source file:org.unitime.timetable.solver.TimetableSolver.java
public SolverUnassignedClassesModel getUnassignedClassesModel(String prefix) { Lock lock = currentSolution().getLock().readLock(); lock.lock(); try {//from ww w. j a va2 s . c o m return new SolverUnassignedClassesModel(this, prefix); } finally { lock.unlock(); } }
From source file:org.unitime.timetable.solver.TimetableSolver.java
public AssignmentPreferenceInfo getInfo(Hint hint) { Lock lock = currentSolution().getLock().readLock(); lock.lock(); try {//w w w .ja v a 2 s. c o m return hint.getInfo(this); } finally { lock.unlock(); } }