List of usage examples for java.util.concurrent.locks ReentrantLock lock
public void lock()
From source file:org.exoplatform.social.core.storage.impl.ActivityStreamStorageImpl.java
@Override public void update(ProcessContext ctx) { final ReentrantLock lock = new ReentrantLock(); ThreadLocal<Set<String>> idLocal = new ThreadLocal<Set<String>>(); try {/*from w ww.ja v a 2 s.c om*/ StreamProcessContext streamCtx = ObjectHelper.cast(StreamProcessContext.class, ctx); ExoSocialActivity activity = streamCtx.getActivity(); ActivityEntity activityEntity = _findById(ActivityEntity.class, activity.getId()); lock.lock(); // block until condition holds Collection<ActivityRef> references = activityEntity.getActivityRefs(); Set<String> ids = new ConcurrentSkipListSet<String>(); for (ActivityRef ref : references) { ids.add(ref.getId()); } idLocal.set(ids); Set<String> idList = idLocal.get(); if (idList.size() > 0) { for (String id : idList) { ActivityRef old = _findById(ActivityRef.class, id); LOG.debug("ActivityRef will be deleted: " + old.toString()); ActivityRefListEntity refList = old.getDay().getMonth().getYear().getList(); // if (refList.isOnlyUpdate(old, activity.getUpdated().getTime())) { old.setName("" + activity.getUpdated().getTime()); old.setLastUpdated(activity.getUpdated().getTime()); } else { ActivityRef newRef = refList.getOrCreated(activity.getUpdated().getTime()); newRef.setLastUpdated(activity.getUpdated().getTime()); newRef.setActivityEntity(activityEntity); getSession().remove(old); } } } // mentioners addMentioner(streamCtx.getMentioners(), activityEntity); //turnOffLock to get increase perf //turnOnUpdateLock = false; } catch (NodeNotFoundException ex) { LOG.warn("Probably was updated activity reference by another session"); LOG.debug(ex.getMessage(), ex); //turnOnLock to avoid next exception } catch (ChromatticException ex) { Throwable throwable = ex.getCause(); if (throwable instanceof ItemExistsException || throwable instanceof InvalidItemStateException || throwable instanceof PathNotFoundException) { LOG.warn("Probably was updated activity reference by another session"); LOG.debug(ex.getMessage(), ex); //turnOnLock to avoid next exception } else { LOG.warn("Probably was updated activity reference by another session", ex); LOG.debug(ex.getMessage(), ex); } } finally { getSession().save(); lock.unlock(); } }
From source file:org.exoplatform.social.core.storage.impl.ActivityStreamStorageImpl.java
@Override public void updateCommenter(ProcessContext ctx) { final ReentrantLock lock = new ReentrantLock(); try {//w w w. j a va2 s .c o m StreamProcessContext streamCtx = ObjectHelper.cast(StreamProcessContext.class, ctx); ExoSocialActivity activity = streamCtx.getActivity(); Identity commenter = streamCtx.getIdentity(); IdentityEntity identityEntity = identityStorage._findIdentityEntity(commenter.getProviderId(), commenter.getRemoteId()); ActivityEntity activityEntity = _findById(ActivityEntity.class, activity.getId()); // lock.lock(); QueryResult<ActivityRef> got = getActivityRefs(identityEntity, activityEntity); ActivityRef activityRef = null; while (got.hasNext()) { activityRef = got.next(); activityRef.setName("" + activity.getUpdated().getTime()); activityRef.setLastUpdated(activity.getUpdated().getTime()); } long oldUpdated = streamCtx.getOldLastUpdated(); //activity's poster != comment's poster //don't have on My Activity stream boolean has = hasActivityRefs(identityEntity, activityEntity, ActivityRefType.MY_ACTIVITIES, oldUpdated); if (has == false) { manageRefList(new UpdateContext(commenter, null), activityEntity, ActivityRefType.MY_ACTIVITIES); } //post comment also put the activity on feed if have not any has = hasActivityRefs(identityEntity, activityEntity, ActivityRefType.FEED, oldUpdated); if (has == false) { manageRefList(new UpdateContext(commenter, null), activityEntity, ActivityRefType.FEED); } //create activityref for owner's activity createRefForPoster(activityEntity, oldUpdated); } catch (NodeNotFoundException ex) { LOG.warn("Probably was updated activity reference by another session"); LOG.debug(ex.getMessage(), ex); } catch (ChromatticException ex) { Throwable throwable = ex.getCause(); if (throwable instanceof ItemExistsException || throwable instanceof InvalidItemStateException || throwable instanceof PathNotFoundException) { LOG.warn("Probably was updated activity reference by another session"); LOG.debug(ex.getMessage(), ex); } else { LOG.warn("Probably was updated activity reference by another session", ex); LOG.debug(ex.getMessage(), ex); } } finally { lock.unlock(); } }
From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java
/** * Returns the largest number of threads that have ever simultaneously been * in the pool.//from w ww .j a v a 2 s . c o m * * @return the number of threads */ public int getLargestPoolSize() { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { return largestPoolSize; } finally { mainLock.unlock(); } }
From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java
/** * Returns the current number of threads in the pool. * /*from w w w. j a va 2 s. c om*/ * @return the number of threads */ public int getPoolSize() { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { // Remove rare and surprising possibility of // isTerminated() && getPoolSize() > 0 return runStateAtLeast(ctl.get(), TIDYING) ? 0 : workers.size(); } finally { mainLock.unlock(); } }
From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java
/** * Returns the approximate total number of tasks that have completed * execution. Because the states of tasks and threads may change dynamically * during computation, the returned value is only an approximation, but one * that does not ever decrease across successive calls. * //from ww w . j ava 2 s . c o m * @return the number of tasks */ public long getCompletedTaskCount() { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { long n = completedTaskCount; for (Worker w : workers) n += w.completedTasks; return n; } finally { mainLock.unlock(); } }
From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java
/** * Returns the approximate number of threads that are actively executing * tasks./* ww w.j av a2s.co m*/ * * @return the number of threads */ public int getActiveCount() { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { int n = 0; for (Worker w : workers) if (w.isLocked()) ++n; return n; } finally { mainLock.unlock(); } }
From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java
/** * Interrupts all threads, even if active. Ignores SecurityExceptions (in * which case some threads may remain uninterrupted). *//*w w w. ja v a 2 s .c o m*/ private void interruptWorkers() { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { for (Worker w : workers) { try { w.thread.interrupt(); } catch (SecurityException ignore) { } } } finally { mainLock.unlock(); } }
From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java
/** * Attempts to stop all actively executing tasks, halts the processing of * waiting tasks, and returns a list of the tasks that were awaiting * execution. These tasks are drained (removed) from the task queue upon * return from this method./*from w w w .j a va 2 s . com*/ * * <p> * There are no guarantees beyond best-effort attempts to stop processing * actively executing tasks. This implementation cancels tasks via * {@link Thread#interrupt}, so any task that fails to respond to * interrupts may never terminate. * * @throws SecurityException * {@inheritDoc} */ public List<Runnable> shutdownNow() { List<Runnable> tasks; final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { checkShutdownAccess(); advanceRunState(STOP); interruptWorkers(); tasks = drainQueue(); } finally { mainLock.unlock(); } tryTerminate(); return tasks; }
From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try {/*from www. j ava 2 s. c om*/ for (;;) { if (runStateAtLeast(ctl.get(), TERMINATED)) return true; if (nanos <= 0) return false; nanos = termination.awaitNanos(nanos); } } finally { mainLock.unlock(); } }
From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java
/** * Returns the approximate total number of tasks that have ever been * scheduled for execution. Because the states of tasks and threads may * change dynamically during computation, the returned value is only an * approximation.//from ww w. j av a2s . co m * * @return the number of tasks */ public long getTaskCount() { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { long n = completedTaskCount; for (Worker w : workers) { n += w.completedTasks; if (w.isLocked()) ++n; } return n + workQueue.size(); } finally { mainLock.unlock(); } }