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

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

Introduction

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

Prototype

void unlock();

Source Link

Document

Releases the lock.

Usage

From source file:com.funambol.pushlistener.service.taskexecutor.ScheduledTaskExecutor.java

/**
 * Called when a TaskWrapper ends its execution. See afterExecute.
 *//*from w ww. j a v a 2 s . c  o  m*/
protected void afterTaskExecution(Runnable r, Throwable t) {

    TaskWrapper taskWrapper = null;

    if (r instanceof ScheduledFuture) {
        ScheduledFuture scheduledFuture = (ScheduledFuture) r;

        Lock handlingTaskLock = null;

        synchronized (taskFutures) {
            taskWrapper = (TaskWrapper) taskFutures.getKey(scheduledFuture);
            if (taskWrapper != null) {
                handlingTaskLock = getHandlingTaskLock(taskWrapper);
                handlingTaskLock.lock();
            }
        }

        try {
            logTaskWrapperExecutionError(taskWrapper, scheduledFuture);
            if (taskWrapper == null) {
                return;
            }

            //
            // we don't need to remove the task from the (ScheduledFuture)queue
            // because it is already removed since is a one shot task (not scheduled)
            //
            synchronized (taskFutures) {
                taskFutures.remove(taskWrapper);
            }

            boolean needRedo = false;

            //synchronized (taskWrapper) {
            needRedo = taskWrapper.needRedo();
            //}
            if (needRedo) {
                long period = taskWrapper.getPeriod();
                if (log.isTraceEnabled()) {
                    log.trace("Task '" + taskWrapper + "' must be redone. " + "It will be executed in " + period
                            + " milliseconds");
                }
                ScheduledFuture f = schedule(taskWrapper, period, TimeUnit.MILLISECONDS);

                synchronized (taskFutures) {
                    taskFutures.put(taskWrapper, f);
                }

            }
        } finally {
            if (handlingTaskLock != null) {
                handlingTaskLock.unlock();
            }
            handlingOneShotExecutionTimeInformation(taskWrapper);
            LogContext.clear();
        }

    }
}

From source file:com.funambol.pushlistener.service.taskexecutor.ScheduledTaskExecutor.java

/**
 * Schedules a new task//from  w w  w. ja va2s .co m
 *
 * @param task the <code>ScheduledTaskWrapper</code> to schedule
 * @param delay the delay in the scheduling (it must be greater than 1 second
 *        otherwise the delay is set automatically to 1 sec)
 */
protected void scheduleTask(ScheduledTaskWrapper task, long delay) {
    if (task == null) {
        if (log.isTraceEnabled()) {
            log.trace("Trying to schedule a null task. Request rejected");
        }
        return;
    }
    //
    // We don't get any task lock, but the caller should do (see updateTask or
    // removeTask or scheduleTask(ScheduledTaskWrapper))
    //

    //
    // This is used below...see there for a description
    //
    Lock taskExecutionLock = null;
    try {

        if (!ScheduledTaskWrapper.State.CONFIGURED.equals(task.getState())
                && !ScheduledTaskWrapper.State.SCHEDULED.equals(task.getState())) {
            //
            // We log the error creating an empty exception in order to have
            // the stacktrace
            //
            log.error("Trying to schedule a not configured or scheduled task. Request rejected",
                    new Exception());

            return;
        }
        long period = task.getPeriod();
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        if (period == 0) {
            period = 1;
            timeUnit = TimeUnit.NANOSECONDS;
        }

        if (log.isTraceEnabled()) {
            log.trace("Scheduling task: " + task);
        }
        //
        // We use the execution lock to avoid the task execution before putting
        // it in the scheduledFutures map.
        // See ScheduledTaskWrapper.execute
        //
        taskExecutionLock = new ReentrantLock();
        taskExecutionLock.lock();
        task.setExecutionLock(taskExecutionLock);
        ScheduledFuture scheduledFuture = scheduleWithFixedDelay(task, delay, period, timeUnit);

        task.setState(ScheduledTaskWrapper.State.SCHEDULED);

        //
        // Since DualHashBidiMap is not synchronized we need to sync the write
        // access
        //
        synchronized (scheduledFutures) {
            scheduledFutures.put(task, scheduledFuture);
        }
    } finally {
        taskExecutionLock.unlock();
    }
}

From source file:com.esofthead.mycollab.module.project.service.ibatis.GanttAssignmentServiceImpl.java

private void massUpdateTaskGanttItems(final List<TaskGanttItem> taskGanttItems, Integer sAccountId) {
    if (CollectionUtils.isNotEmpty(taskGanttItems)) {
        Lock lock = DistributionLockUtil.getLock("gantt-task-service" + sAccountId);
        try {/*from  w  w  w  .  j  av  a 2  s . c  om*/
            final long now = new GregorianCalendar().getTimeInMillis();
            if (lock.tryLock(30, TimeUnit.SECONDS)) {
                try (Connection connection = dataSource.getConnection()) {
                    connection.setAutoCommit(false);
                    PreparedStatement batchTasksStatement = connection.prepareStatement(
                            "UPDATE `m_prj_task` SET " + "taskname = ?, `startdate` = ?, `enddate` = ?, "
                                    + "`lastUpdatedTime`=?, `percentagecomplete`=?, `assignUser`=?, `ganttindex`=?, "
                                    + "`milestoneId`=?, `parentTaskId`=? WHERE `id` = ?");
                    for (int i = 0; i < taskGanttItems.size(); i++) {
                        TaskGanttItem ganttItem = taskGanttItems.get(i);
                        if (ProjectTypeConstants.TASK.equals(ganttItem.getType())) {
                            batchTasksStatement.setString(1, ganttItem.getName());
                            batchTasksStatement.setDate(2, getDateWithNullValue(ganttItem.getStartDate()));
                            batchTasksStatement.setDate(3, getDateWithNullValue(ganttItem.getEndDate()));
                            batchTasksStatement.setDate(4, new Date(now));
                            batchTasksStatement.setDouble(5, ganttItem.getProgress());
                            batchTasksStatement.setString(6, ganttItem.getAssignUser());
                            batchTasksStatement.setInt(7, ganttItem.getGanttIndex());
                            batchTasksStatement.setObject(8, ganttItem.getMilestoneId());
                            batchTasksStatement.setObject(9, ganttItem.getParentTaskId());
                            batchTasksStatement.setInt(10, ganttItem.getId());
                            batchTasksStatement.addBatch();
                        }

                    }
                    batchTasksStatement.executeBatch();
                    connection.commit();
                }
            }
        } catch (Exception e) {
            throw new MyCollabException(e);
        } finally {
            DistributionLockUtil.removeLock("gantt-task-service" + sAccountId);
            lock.unlock();
        }
    }
}

From source file:gridool.db.partitioning.phihash.monetdb.MonetDBCsvLoadOperation.java

@Override
public Long execute() throws SQLException, GridException {
    if (expectedNumRecords == -1L) {
        throw new IllegalStateException();
    }//from   w  w w  .  j ava  2s. c  o  m
    // clear index buffer
    final ILocalDirectory index = registry.getDirectory();
    try {
        index.purgeAll(true);
        index.setBulkloading(false);
    } catch (IndexException dbe) {
        LOG.error(dbe);
    }
    InMemoryMappingIndex mmidx = registry.getMappingIndex();
    mmidx.saveIndex(true, false);

    LockManager lockMgr = registry.getLockManager();
    //String dbid = getConnectionUrl();
    //ReadWriteLock systblLock = lockMgr.obtainLock(dbid);
    // [Design notes] Better performance can be expected for a single bulkload process than multiple one.
    ReadWriteLock systblLock = lockMgr.obtainLock(DBAccessor.SYS_TABLE_SYMBOL);

    long actualInserted = 0L;
    final Lock exlock = systblLock.writeLock();
    exlock.lock();
    final Connection conn = getConnection();
    final String dburl = getConnectionUrl();
    try {
        // #1 create table
        prepareTable(conn, createTableDDL, tableName);
        // #2 invoke COPY INTO
        final StopWatch sw = new StopWatch();
        if (expectedNumRecords > 0L) {
            if (ENV_MONETDB_USE_NOV09_OPT) {
                JDBCUtils.update(conn, "SET optimizer='nov2009_pipe'");
                actualInserted = invokeCopyInto(conn, copyIntoQuery, tableName, csvFileName,
                        expectedNumRecords);
                JDBCUtils.update(conn, "SET optimizer='default_pipe'");
            } else {
                actualInserted = invokeCopyInto(conn, copyIntoQuery, tableName, csvFileName,
                        expectedNumRecords);
            }
            if (actualInserted != expectedNumRecords) {
                String errmsg = "Expected records (" + expectedNumRecords + ") != Actual records ("
                        + actualInserted + "): \n" + getCopyIntoQuery(copyIntoQuery, expectedNumRecords);
                LOG.error(errmsg);
                throw new GridException(errmsg);
            }
            LOG.info("Elapsed time for COPY " + actualInserted + " RECORDS INTO " + tableName + " [" + dburl
                    + "]: " + sw.toString());
        }
        // #3 create indices and constraints
        if (alterTableDDL != null) {
            sw.start();
            alterTable(conn, alterTableDDL);
            LOG.info("Elapsed time for creating indices and constraints on table '" + tableName + "': "
                    + sw.toString());
        }
    } finally {
        JDBCUtils.closeQuietly(conn);
        if (ENV_RESTART_AFTER_COPYINTO && actualInserted > ENV_COPYINTO_PIECES) {
            if (MonetDBUtils.restartLocalDbInstance(dburl)) {
                if (ENV_SLEEP_AFTER_RESTART > 0L) {
                    try {
                        Thread.sleep(ENV_SLEEP_AFTER_RESTART);
                    } catch (InterruptedException e) {
                        ;
                    }
                }
            } else {
                LOG.warn("failed to restart MonetDB instance: " + dburl);
            }
        }
        exlock.unlock();
    }
    return actualInserted;
}

From source file:org.opendedup.collections.ShardedProgressiveFileBasedCSMap.java

@Override
public InsertRecord put(ChunkData cm, boolean persist) throws IOException, HashtableFullException {
    // persist = false;
    if (this.isClosed())
        throw new HashtableFullException("Hashtable " + this.fileName + " is close");
    if (kSz.get() >= this.maxSz)
        throw new HashtableFullException("maximum sized reached");
    InsertRecord rec = null;/* w  w w .java 2  s.co  m*/
    // if (persist)
    // this.flushFullBuffer();
    Lock l = gcLock.readLock();
    l.lock();
    ShardedFileByteArrayLongMap bm = null;
    try {
        // long tm = System.currentTimeMillis();
        AbstractShard rm = this.getReadMap(cm.getHash(), false);
        if (rm == null) {
            // this.misses.incrementAndGet();
            // tm = System.currentTimeMillis() - tm;
            while (rec == null) {
                try {
                    if (persist && !cm.recoverd) {
                        try {
                            cm.persistData(true);
                        } catch (org.opendedup.collections.HashExistsException e) {
                            return new InsertRecord(false, e.getPos());
                        }
                    }
                    bm = this.getWriteMap();
                    rec = bm.put(cm.getHash(), cm.getcPos());
                    this.lbf.put(cm.getHash());
                } catch (HashtableFullException e) {
                    rec = null;
                } catch (Exception e) {
                    // this.keyLookup.invalidate(new
                    // ByteArrayWrapper(cm.getHash()));
                    throw e;
                }
            }
        } else {
            try {
                rec = new InsertRecord(false, rm.get(cm.getHash()));
            } catch (MapClosedException e) {
                this.keyLookup.invalidate(new ByteArrayWrapper(cm.getHash()));
                put(cm, persist);
            }
        }
        // this.msTr.addAndGet(tm);

    } finally {

        l.unlock();
    }
    /*
     * this.trs.incrementAndGet(); if(this.trs.get() == 10000) { long tpm =
     * 0; if(this.misses.get() > 0) tpm = this.msTr.get()/this.misses.get();
     * SDFSLogger.getLog().info("trs=" + this.trs.get() + " misses=" +
     * this.misses.get() + " mtm=" + this.msTr.get() + " tpm=" + tpm);
     * this.trs.set(0); this.misses.set(0); this.msTr.set(0); }
     */
    if (rec.getInserted())
        this.kSz.incrementAndGet();
    return rec;
}

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();/*from ww w  . j a  v  a  2 s . c  o  m*/
    try {

        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.alibaba.wasp.master.AssignmentManager.java

/**
 * Bulk assign entityGroups to <code>destination</code>.
 *
 * @param destination/*from   w w w . java2s.  c o  m*/
 * @param entityGroups
 *          EntityGroups to assign.
 * @return true if successful
 */
boolean assign(final ServerName destination, final List<EntityGroupInfo> entityGroups) {
    int entityGroupCount = entityGroups.size();
    if (entityGroupCount == 0) {
        return true;
    }
    LOG.debug("Bulk assigning " + entityGroupCount + " entityGroup(s) to " + destination.toString());

    Set<String> encodedNames = new HashSet<String>(entityGroupCount);
    for (EntityGroupInfo entityGroup : entityGroups) {
        encodedNames.add(entityGroup.getEncodedName());
    }

    List<EntityGroupInfo> failedToOpenEntityGroups = new ArrayList<EntityGroupInfo>();
    Map<String, Lock> locks = locker.acquireLocks(encodedNames);
    try {
        AtomicInteger counter = new AtomicInteger(0);
        Map<String, Integer> offlineNodesVersions = new ConcurrentHashMap<String, Integer>();
        OfflineCallback cb = new OfflineCallback(watcher, destination, counter, offlineNodesVersions);
        Map<String, EntityGroupPlan> plans = new HashMap<String, EntityGroupPlan>(entityGroups.size());
        List<EntityGroupState> states = new ArrayList<EntityGroupState>(entityGroups.size());
        for (EntityGroupInfo entityGroup : entityGroups) {
            String encodedEntityGroupName = entityGroup.getEncodedName();
            EntityGroupState state = forceEntityGroupStateToOffline(entityGroup, true);
            if (state != null && asyncSetOfflineInZooKeeper(state, cb, destination)) {
                EntityGroupPlan plan = new EntityGroupPlan(entityGroup, state.getServerName(), destination);
                plans.put(encodedEntityGroupName, plan);
                states.add(state);
            } else {
                LOG.warn("failed to force entityGroup state to offline or "
                        + "failed to set it offline in ZK, will reassign later: " + entityGroup);
                failedToOpenEntityGroups.add(entityGroup); // assign individually
                                                           // later
                Lock lock = locks.remove(encodedEntityGroupName);
                lock.unlock();
            }
        }

        // Wait until all unassigned nodes have been put up and watchers set.
        int total = states.size();
        for (int oldCounter = 0; !server.isStopped();) {
            int count = counter.get();
            if (oldCounter != count) {
                LOG.info(destination.toString() + " unassigned znodes=" + count + " of total=" + total);
                oldCounter = count;
            }
            if (count >= total)
                break;
            Threads.sleep(5);
        }

        if (server.isStopped()) {
            return false;
        }

        // Add entityGroup plans, so we can updateTimers when one entityGroup is
        // opened so
        // that unnecessary timeout on EGIT is reduced.
        this.addPlans(plans);

        List<EntityGroupInfo> entityGroupOpenInfos = new ArrayList<EntityGroupInfo>(states.size());
        for (EntityGroupState state : states) {
            EntityGroupInfo entityGroup = state.getEntityGroup();
            String encodedEntityGroupName = entityGroup.getEncodedName();
            Integer nodeVersion = offlineNodesVersions.get(encodedEntityGroupName);
            if (nodeVersion == null || nodeVersion.intValue() == -1) {
                LOG.warn("failed to offline in zookeeper: " + entityGroup);
                failedToOpenEntityGroups.add(entityGroup); // assign individually
                                                           // later
                Lock lock = locks.remove(encodedEntityGroupName);
                lock.unlock();
            } else {
                entityGroupStates.updateEntityGroupState(entityGroup, EntityGroupState.State.PENDING_OPEN,
                        destination);
                entityGroupOpenInfos.add(entityGroup);
            }
        }

        // Move on to open entityGroups.
        try {
            // Send OPEN RPC. If it fails on a IOE or RemoteException, the
            // TimeoutMonitor will pick up the pieces.
            long maxWaitTime = System.currentTimeMillis() + this.server.getConfiguration()
                    .getLong("wasp.entityGroupserver.rpc.startup.waittime", 60000);
            for (int i = 1; i <= maximumAttempts && !server.isStopped(); i++) {
                try {
                    List<EntityGroupOpeningState> entityGroupOpeningStateList = serverManager
                            .sendEntityGroupsOpen(destination, entityGroupOpenInfos);
                    if (entityGroupOpeningStateList == null) {
                        // Failed getting RPC connection to this server
                        return false;
                    }
                    for (int k = 0, n = entityGroupOpeningStateList.size(); k < n; k++) {
                        EntityGroupOpeningState openingState = entityGroupOpeningStateList.get(k);
                        if (openingState != EntityGroupOpeningState.OPENED) {
                            EntityGroupInfo entityGroup = entityGroupOpenInfos.get(k);
                            if (openingState == EntityGroupOpeningState.ALREADY_OPENED) {
                                processAlreadyOpenedEntityGroup(entityGroup, destination);
                            } else if (openingState == EntityGroupOpeningState.FAILED_OPENING) {
                                // Failed opening this entityGroup, reassign it later
                                failedToOpenEntityGroups.add(entityGroup);
                            } else {
                                LOG.warn("THIS SHOULD NOT HAPPEN: unknown opening state " + openingState
                                        + " in assigning entityGroup " + entityGroup);
                            }
                        }
                    }
                    break;
                } catch (IOException e) {
                    if (e instanceof RemoteException) {
                        e = ((RemoteException) e).unwrapRemoteException();
                    }
                    if (e instanceof FServerStoppedException) {
                        LOG.warn("The fserver was shut down, ", e);
                        // No need to retry, the entityGroup server is a goner.
                        return false;
                    } else if (e instanceof ServerNotRunningYetException) {
                        long now = System.currentTimeMillis();
                        if (now < maxWaitTime) {
                            LOG.debug("Server is not yet up; waiting up to " + (maxWaitTime - now) + "ms", e);
                            Thread.sleep(100);
                            i--; // reset the try count
                            continue;
                        }
                    } else if (e instanceof java.net.SocketTimeoutException
                            && this.serverManager.isServerOnline(destination)) {
                        // In case socket is timed out and the entityGroup server is still
                        // online,
                        // the openEntityGroup RPC could have been accepted by the server
                        // and
                        // just the response didn't go through. So we will retry to
                        // open the entityGroup on the same server.
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Bulk assigner openEntityGroup() to " + destination
                                    + " has timed out, but the entityGroups might"
                                    + " already be opened on it.", e);
                        }
                        continue;
                    }
                    throw e;
                }
            }
        } catch (IOException e) {
            // Can be a socket timeout, EOF, NoRouteToHost, etc
            LOG.info("Unable to communicate with the fserver in order" + " to assign entityGroups", e);
            return false;
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    } finally {
        for (Lock lock : locks.values()) {
            lock.unlock();
        }
    }

    if (!failedToOpenEntityGroups.isEmpty()) {
        for (EntityGroupInfo entityGroup : failedToOpenEntityGroups) {
            invokeAssign(entityGroup);
        }
    }
    LOG.debug("Bulk assigning done for " + destination.toString());
    return true;
}

From source file:gridool.db.sql.ParallelSQLMapTask.java

@Override
protected ParallelSQLMapTaskResult execute() throws GridException {
    assert (registry != null);

    final File tmpFile;
    try {//w w  w.ja  v a 2s .c  om
        tmpFile = File.createTempFile("PSQLMap" + taskNumber + '_', '_' + NetUtils.getLocalHostAddress());
    } catch (IOException e) {
        throw new GridException(e);
    }

    final QueryString[] queries = SQLTranslator.divideQuery(query, true);
    final boolean singleStatement = (queries.length == 1);
    final String selectQuery = singleStatement ? query : SQLTranslator.selectFirstSelectQuery(queries);

    GridNode localNode = config.getLocalNode();
    GridNode senderNode = getSenderNode();
    assert (senderNode != null);
    final boolean useCreateTableAS = senderNode.equals(localNode);

    final int fetchedRows;
    final LockManager lockMgr = registry.getLockManager();
    final ReadWriteLock rwlock = lockMgr.obtainLock(DBAccessor.SYS_TABLE_SYMBOL);
    final Lock lock = rwlock.writeLock(); // REVIEWME tips: exclusive lock for system table in MonetDB
    long startQueryTime = System.currentTimeMillis();
    final Connection dbConn = getDbConnection(taskMasterNode, registry);
    try {
        lock.lock();
        if (singleStatement) {
            // #1 invoke COPY INTO file
            if (useCreateTableAS) {
                dbConn.setAutoCommit(true);
                fetchedRows = executeCreateTableAs(dbConn, selectQuery, taskTableName);
            } else {
                dbConn.setAutoCommit(false);
                fetchedRows = executeCopyIntoFile(dbConn, selectQuery, taskTableName, tmpFile);
                dbConn.commit();
            }
        } else {
            dbConn.setAutoCommit(false);
            // #1-1 DDL before map SELECT queries (e.g., create view)
            issueDDLBeforeSelect(dbConn, queries);
            // #1-2 invoke COPY INTO file
            if (useCreateTableAS) {
                fetchedRows = executeCreateTableAs(dbConn, selectQuery, taskTableName);
            } else {
                fetchedRows = executeCopyIntoFile(dbConn, selectQuery, taskTableName, tmpFile);
            }
            // #1-3 DDL after map SELECT queries (e.g., drop view)
            issueDDLAfterSelect(dbConn, queries);
            dbConn.commit();
        }
        assert (fetchedRows != -1);
    } catch (SQLException sqle) {
        String errmsg = "Failed to execute a query: \n" + query;
        LOG.error(errmsg, sqle);
        if (singleStatement) {
            try {
                dbConn.rollback();
            } catch (SQLException rbe) {
                LOG.warn("Rollback failed", rbe);
            }
        }
        new FileDeletionThread(tmpFile, LOG).start();
        throw new GridException(errmsg, sqle);
    } catch (Throwable e) {
        String errmsg = "Failed to execute a query: \n" + query;
        LOG.fatal(errmsg, e);
        if (singleStatement) {
            try {
                dbConn.rollback();
            } catch (SQLException rbe) {
                LOG.warn("Rollback failed", rbe);
            }
        }
        new FileDeletionThread(tmpFile, LOG).start();
        throw new GridException(errmsg, e);
    } finally {
        lock.unlock();
        JDBCUtils.closeQuietly(dbConn);
    }
    long queryExecTime = System.currentTimeMillis() - startQueryTime;

    String sentFileName = null;
    long sendResultTime = -1L; // would be null for a local task
    if (fetchedRows > 0) {
        // #2 send file
        long startResultTime = System.currentTimeMillis();
        try {
            TransferUtils.sendfile(tmpFile, dstAddr, dstPort, false, true);
            sendResultTime = System.currentTimeMillis() - startResultTime;
            sentFileName = tmpFile.getName();
        } catch (IOException e) {
            throw new GridException("failed to sending a file", e);
        } finally {
            new FileDeletionThread(tmpFile, LOG).start();
        }
    }
    return new ParallelSQLMapTaskResult(taskMasterNode, sentFileName, taskNumber, fetchedRows, queryExecTime,
            sendResultTime);
}

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();/*from  w  w  w  . jav  a2  s  .c  o m*/
    try {

        // 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:com.alibaba.wasp.master.AssignmentManager.java

/**
 * Use care with forceNewPlan. It could cause double assignment.
 *//*from w  ww. j av  a2  s.  com*/
public void assign(EntityGroupInfo entityGroup, boolean setOfflineInZK, boolean forceNewPlan) {
    if (!setOfflineInZK && isDisabledorDisablingEntityGroupInEGIT(entityGroup)) {
        return;
    }
    if (this.serverManager.isClusterShutdown()) {
        LOG.info("Cluster shutdown is set; skipping assign of " + entityGroup.getEntityGroupNameAsString());
        return;
    }
    String encodedName = entityGroup.getEncodedName();
    Lock lock = locker.acquireLock(encodedName);
    try {
        EntityGroupState state = forceEntityGroupStateToOffline(entityGroup, forceNewPlan);
        if (state != null) {
            assign(state, setOfflineInZK, forceNewPlan);
        }
    } finally {
        lock.unlock();
    }
}