Package java.util.concurrent.locks

Examples of java.util.concurrent.locks.ReentrantLock


            fail("null condition not detected");
        } catch (IllegalArgumentException iax) {
            // expected
        }

        Lock      lck = new ReentrantLock();
        Condition cnd = lck.newCondition();

        WaitingThread wt = new WaitingThread(cnd, null);
        assertEquals("wrong condition", cnd, wt.getCondition());
        assertNull  ("pool from nowhere", wt.getPool());
        assertNull  ("thread from nowhere", wt.getThread());
View Full Code Here


    }


    public void testAwaitWakeup() throws InterruptedException {

        Lock      lck = new ReentrantLock();
        Condition cnd = lck.newCondition();
        WaitingThread wt = new WaitingThread(cnd, null);

        AwaitThread ath = new AwaitThread(wt, lck, null);
        ath.start();
        Thread.sleep(100); // give extra thread time to block

        assertNull("thread caught exception", ath.getException());
        assertTrue("thread not waiting", ath.isWaiting());
        assertEquals("wrong thread", ath, wt.getThread());

        Thread.sleep(500); // just for fun, let it wait for some time
        // this may fail due to a spurious wakeup
        assertTrue("thread not waiting, spurious wakeup?", ath.isWaiting());

        try {
            lck.lock();
            wt.wakeup();
        } finally {
            lck.unlock();
        }
        ath.join(10000);

        assertFalse("thread still waiting", ath.isWaiting());
        assertNull("thread caught exception", ath.getException());
View Full Code Here

    }


    public void testInterrupt() throws InterruptedException {

        Lock      lck = new ReentrantLock();
        Condition cnd = lck.newCondition();
        WaitingThread wt = new WaitingThread(cnd, null);

        AwaitThread ath = new AwaitThread(wt, lck, null);
        ath.start();
        Thread.sleep(100); // give extra thread time to block
View Full Code Here

    }


    public void testIllegal() throws InterruptedException {

        Lock      lck = new ReentrantLock();
        Condition cnd = lck.newCondition();
        WaitingThread wt = new WaitingThread(cnd, null);

        try {
            lck.lock();
            wt.wakeup();
            fail("missing waiter not detected");
        } catch (IllegalStateException isx) {
            // expected
        } finally {
            lck.unlock();
        }

        AwaitThread ath1 = new AwaitThread(wt, lck, null);
        ath1.start();
        Thread.sleep(100); // give extra thread time to block
View Full Code Here

    protected AbstractConnPool() {
        issuedConnections = new HashSet<BasicPoolEntryRef>();
        idleConnHandler = new IdleConnectionHandler();

        boolean fair = false; //@@@ check parameters to decide
        poolLock = new ReentrantLock(fair);
    }
View Full Code Here

     * Returns and optionally creates a lock to load a tag file.
     */
    private Lock getTagFileLoadingLock(final String tagFilePath) {
        Lock lock = tagFileLoadingLocks.get(tagFilePath);
        if (lock == null) {
            lock = new ReentrantLock();
            final Lock existingLock = tagFileLoadingLocks.putIfAbsent(tagFilePath, lock);
            if (existingLock != null) {
                lock = existingLock;
            }
        }
View Full Code Here

  public RangeBasedReaderWriterLock() {
    readerRanges = new PriorityQueue<LockToken>(100);
    writerRange = new Range(-1, 0);
    writerIn = false;
    mutex = new ReentrantLock();
    writesPossible = mutex.newCondition();
    readsPossible = mutex.newCondition();

  }
View Full Code Here

            executorService = new ThreadPoolExecutor(0, MAX_WORKER_THREADS,
                    5L, TimeUnit.SECONDS,
                    new LinkedBlockingQueue<Runnable>(),
                    threadFactory) {

                private final ReentrantLock pauseLock = new ReentrantLock();
                private final Condition unpaused = pauseLock.newCondition();
                private boolean isPaused = false;
                private final ReentrantLock executeLock = new ReentrantLock();

                @Override
                public void execute(Runnable command) {
                    /*
                    * ThreadPoolExecutor first tries to run task
                    * in a corePool. If all threads are busy it
                    * tries to add task to the waiting queue. If it
                    * fails it run task in maximumPool.
                    *
                    * We want corePool to be 0 and
                    * maximumPool to be MAX_WORKER_THREADS
                    * We need to change the order of the execution.
                    * First try corePool then try maximumPool
                    * pool and only then store to the waiting
                    * queue. We can not do that because we would
                    * need access to the private methods.
                    *
                    * Instead we enlarge corePool to
                    * MAX_WORKER_THREADS before the execution and
                    * shrink it back to 0 after.
                    * It does pretty much what we need.
                    *
                    * While we changing the corePoolSize we need
                    * to stop running worker threads from accepting new
                    * tasks.
                    */

                    //we need atomicity for the execute method.
                    executeLock.lock();
                    try {

                        pauseLock.lock();
                        try {
                            isPaused = true;
                        }
                        finally {
                            pauseLock.unlock();
                        }

                        setCorePoolSize(MAX_WORKER_THREADS);
                        super.execute(command);
                        setCorePoolSize(0);

                        pauseLock.lock();
                        try {
                            isPaused = false;
                            unpaused.signalAll();
                        }
                        finally {
                            pauseLock.unlock();
                        }
                    }
                    finally {
                        executeLock.unlock();
                    }
                }

                @Override
                protected void afterExecute(Runnable r, Throwable t) {
View Full Code Here

       
        ComponentMetadata metadata = container.getComponentMetadata();

        m_dependencyManagers = loadDependencyManagers( metadata );

        m_stateLock = new ReentrantLock( true );

        // dump component details
        if ( isLogEnabled( LogService.LOG_DEBUG ) )
        {
            log(
View Full Code Here

      if (!isHotDeployEnabled(init))
      {
         return;
      }
     
      ReentrantLock lock = new ReentrantLock();
      if (lock.tryLock(500, TimeUnit.MILLISECONDS))
      {
         try
         {
            hotDeploymentStrategy = createHotDeployment(Thread.currentThread().getContextClassLoader(), isHotDeployEnabled(init));
           
            boolean changed = new TimestampCheckForwardingDeploymentStrategy()
            {
               @Override
               protected DeploymentStrategy delegate()
               {
                  return hotDeploymentStrategy;
               }
              
            }.changedSince(init.getTimestamp());
           
            if (hotDeploymentStrategy.available() && changed)
            {
               ServletLifecycle.beginReinitialization(request);
               Contexts.getEventContext().set(HotDeploymentStrategy.NAME, hotDeploymentStrategy);
               hotDeploymentStrategy.scan();
              
               if (hotDeploymentStrategy.getTimestamp() > init.getTimestamp())
               {
                  log.info("redeploying components");
                  Seam.clearComponentNameCache();
                  for ( String name: init.getHotDeployableComponents() )
                  {
                     Component component = Component.forName(name);
                     if (component!=null)
                     {
                        ScopeType scope = component.getScope();
                        if ( scope!=ScopeType.STATELESS && scope.isContextActive() )
                        {
                           scope.getContext().remove(name);
                        }
                        init.removeObserverMethods(component);
                     }
                     Contexts.getApplicationContext().remove(name + COMPONENT_SUFFIX);
                  }
              
                  init.getHotDeployableComponents().clear();
                  installHotDeployableComponents();
                  installComponents(init);
                  log.info("done redeploying components");
               }
               // update the timestamp outside of the second timestamp check to be sure we don't cause an unnecessary scan
               // the second scan checks annotations (the slow part) which might happen to exclude the most recent file
               init.setTimestamp(System.currentTimeMillis());
               ServletLifecycle.endReinitialization();
            }
              
            final WarRootDeploymentStrategy warRootDeploymentStrategy = new WarRootDeploymentStrategy(Thread.currentThread().getContextClassLoader(), warRoot, new File[] { warClassesDirectory, warLibDirectory, hotDeployDirectory });
            changed = new TimestampCheckForwardingDeploymentStrategy()
            {
               @Override
               protected DeploymentStrategy delegate()
               {
                  return warRootDeploymentStrategy;
               }
              
            }.changedSince(init.getWarTimestamp());
            if (changed)
            {
               warRootDeploymentStrategy.scan();
               if (warRootDeploymentStrategy.getTimestamp() > init.getWarTimestamp())
               {
                  log.info("redeploying page descriptors...");
                  Pages pages = (Pages) ServletLifecycle.getServletContext().getAttribute(Seam.getComponentName(Pages.class));
                  if (pages != null) {
                     // application context is needed for creating expressions
                     Lifecycle.setupApplication();
                     pages.initialize(warRootDeploymentStrategy.getDotPageDotXmlFileNames());
                     Lifecycle.cleanupApplication();
                  }
                  ServletLifecycle.getServletContext().removeAttribute(Seam.getComponentName(Exceptions.class));
                  init.setWarTimestamp(warRootDeploymentStrategy.getTimestamp());
                  log.info("done redeploying page descriptors");
               }
            }
         }
         finally
         {
            lock.unlock();
         }
      }
   }
View Full Code Here

TOP

Related Classes of java.util.concurrent.locks.ReentrantLock

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.