Package java.util.concurrent.locks

Examples of java.util.concurrent.locks.Lock


   {
      PackagedSession existing = this.getAppMap(appName).putIfAbsent(session.getKey(), session);
     
      if (existing != null)
      {
         Lock lock = existing.getLock();
        
         try
         {
            lock.lockInterruptibly();
         }
         catch (InterruptedException ie)
         {
            Thread.currentThread().interrupt();
            this.log.info(ie);
            return;
         }
        
         try
         {
            if (existing.getOwner().equals(this.myNodeName))
            {
               // a modification has occured externally while we were the owner
               //
               this.ownedObjectExternallyModified(appName, session.getKey(), existing, session);
            }
           
            existing.update(session);
         }
         finally
         {
            lock.unlock();
         }
      }
   }
View Full Code Here


      if (session == null)
      {
         return null;
      }
     
      Lock lock = session.getLock();
     
      if (!lock.tryLock())
      {
         throw new java.rmi.RemoteException("Concurent calls on session object.");
      }
     
      try
      {
         if (!session.getOwner().equals(this.myNodeName))
         {
            Object[] args = { appName, keyId, this.myNodeName, new Long(session.getVersion()) };
            List<?> answers = null;
            try
            {
               answers = this.partition.callMethodOnCluster(this.sessionStateIdentifier, "_setOwnership", args, SET_OWNERSHIP_TYPES, true);
            }
            catch (Exception e)
            {
               this.log.error("operation failed", e);
            }
           
            if ((answers != null) && answers.contains(Boolean.FALSE))
            {
               throw new java.rmi.RemoteException("Concurent calls on session object.");
            }

            session.setOwner(this.myNodeName);
            return session;
         }

         return session;
      }
      finally
      {
         lock.unlock();
      }
   }
View Full Code Here

  
   public Boolean _setOwnership(String appName, Object keyId, String newOwner, Long remoteVersion)
   {
      PackagedSession session = this.getAppMap(appName).get(keyId);
     
      Lock lock = session.getLock();
     
      if (!lock.tryLock())
      {
         return Boolean.FALSE;
      }

      try
      {
         if (!session.getOwner().equals(this.myNodeName))
         {
            // this is not our business... we don't care
            // we do not update the owner of ps as another host may refuse the _setOwnership call
            // anyway, the update will be sent to us later if state is modified
            //
            return Boolean.TRUE;
         }
         else if (session.getVersion() > remoteVersion.longValue())
         {
            // we are concerned and our version is more recent than the one of the remote host!
            // it means that we have concurrent calls on the same state that has not yet been updated
            // this means we will need to raise a java.rmi.RemoteException
            //
            return Boolean.FALSE;
         }

         // the remote host has the same version as us (or more recent? possible?)
         // we need to update the ownership. We can do this because we know that no other
         // node can refuse the _setOwnership call
         session.setOwner(newOwner);

         this.ownedObjectExternallyModified(appName, keyId, session, session);
        
         return Boolean.TRUE;
      }
      finally
      {
         lock.unlock();
      }
   }
View Full Code Here

   */
  public void waitFor(long t)
  {
    if (_state == STATE_IDLE)
      return;
    final Lock lock = new ReentrantLock();
    final java.util.concurrent.locks.Condition isIdle = lock.newCondition();
    StateChangeListener listener = new StateChangeListener()
    {

      public void stateChange(int newState, int oldState)
      {
        lock.lock();
        isIdle.signal();
        lock.unlock();
      }

    };
    this.addStateChangeListener(STATE_IDLE, listener);
    if (_state != STATE_IDLE)
      try
      {
        lock.lock();
        isIdle.await(t, TimeUnit.MILLISECONDS);
      }
      catch (InterruptedException e)
      {
        e.printStackTrace();
        Thread.currentThread().interrupt();
      }
    lock.unlock();
    this.removeStateChangeListener(listener);
  }
View Full Code Here

    System.setOut(aPrintStream); // catches System.out messages
    System.setErr(aPrintStream); // catches error messages

    // signal condition if continue button hit
    final Lock lock = new ReentrantLock();
    final Condition cont = lock.newCondition();

    wsform._GO_BUTTON.addActionListener(new ActionListener()
    {

      public void actionPerformed(ActionEvent e)
      {
        lock.lock();
        cont.signal();
        lock.unlock();
      }

    });

    wsform._SHOW_CONF_BUTTON.addActionListener(new ActionListener()
    {

      public void actionPerformed(ActionEvent e)
      {
        showFile(configuration);
      }

    });

    wsform._SELECT_FOLDER_BUTTON.addActionListener(new ActionListener()
    {

      public void actionPerformed(ActionEvent e)
      {
        JFileChooser fc = new JFileChooser();
        fc.setCurrentDirectory(new File("."));
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fc.setAcceptAllFileFilterUsed(false);
        int retval = fc.showOpenDialog(wsform);

        if (retval == JFileChooser.APPROVE_OPTION)
        {
          // ... The user selected a file, get it, use it.
          File file = fc.getSelectedFile();

          // ... Update user interface.
          try
          {
            wsform._INSTALL_FOLDER.setText(file.getCanonicalPath());
          }
          catch (IOException e1)
          {
            e1.printStackTrace();
          }
        }
      }

    });

    // exit if cancel button hit
    wsform._CANCEL_BUTTON.addActionListener(new ActionListener()
    {

      public void actionPerformed(ActionEvent e)
      {
        System.exit(0);
      }

    });

    JFrame frame = new JFrame();
    frame.setTitle(TITLE);
    frame.setSize(530, 450);
    frame.setLocation(100, 100);
    frame.getContentPane().add(wsform);
    frame.setVisible(true);

    frame.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent evt)
      {
        System.exit(0);
      }
    });

    showStep("Click button to continue");

    // wait for continue button
    lock.lock();
    cont.await();
    lock.unlock();

    // get user data
    destination = wsform._INSTALL_FOLDER.getText();
    if (wsform._INSTALL_OPTION.isSelected())
    {
View Full Code Here

    }

    @Test
    public void testWithLocking() {
        ReentrantReadWriteLock readWriteLock = dhns.getReadWriteLock();
        Lock lock = readWriteLock.readLock();

        for (int i = 0; i < 100000; i++) {
            lock.lock();
            int degree = singleNode.getEdgesInTree().getCount() + singleNode.getEdgesOutTree().getCount();
            lock.unlock();
        }
    }
View Full Code Here

    }

    @Test
    public void testWithGlobalLocking() {
        ReentrantReadWriteLock readWriteLock = dhns.getReadWriteLock();
        Lock lock = readWriteLock.readLock();

        lock.lock();
        for (int i = 0; i < 100000; i++) {
            lock.lock();
            int degree = singleNode.getEdgesInTree().getCount() + singleNode.getEdgesOutTree().getCount();
            lock.unlock();
        }
        lock.unlock();
    }
View Full Code Here

    }
    else
      block = _store.loadBlock(blockId);

    try {
      Lock blockLock = block.getReadLock();
     
      blockLock.tryLock(_timeout, TimeUnit.MILLISECONDS);

      try {
        validateIndex(block);
       
        block.read();
       
        byte []buffer = block.getBuffer();

        boolean isLeaf = isLeaf(buffer, block);
     
        long value = lookupTuple(blockId, buffer,
                                 keyBuffer, keyOffset, keyLength,
                                 isLeaf);

        if (isLeaf || value == FAIL)
          return value;
        else
          return lookup(keyBuffer, keyOffset, keyLength, value);
      } finally {
        blockLock.unlock();
      }
    } finally {
      block.free();
    }
  }
View Full Code Here

                                  long value,
                                  boolean isOverride,
                                  Block block)
    throws IOException, SQLException, InterruptedException
  {
    Lock blockLock = block.getReadLock();
    blockLock.tryLock(_timeout, TimeUnit.MILLISECONDS);
     
    try {
      validateIndex(block);
       
      block.read();
     
      long blockId = block.getBlockId();
      byte []buffer = block.getBuffer();

      int length = getLength(buffer);

      if (length == _n) {
        // return false if the block needs to be split
        return false;
      }

      if (isLeaf(buffer, block)) {
        return false;
      }

      long childBlockId = lookupTuple(blockId, buffer,
                                      keyBuffer, keyOffset, keyLength,
                                      false);

      return insert(keyBuffer, keyOffset, keyLength,
                    value, isOverride, true,
                    childBlockId);
    } finally {
      blockLock.unlock();
    }
  }
View Full Code Here

                                   long value,
                                   boolean isOverride,
                                   Block block)
    throws IOException, SQLException, InterruptedException
  {
    Lock blockLock = block.getWriteLock();
    blockLock.tryLock(_timeout, TimeUnit.MILLISECONDS);
     
    try {
      block.read();
     
      validate(block);
     
      long blockId = block.getBlockId();
      byte []buffer = block.getBuffer();

      int length = getLength(buffer);

      if (length == _n) {
        // return false if the block needs to be split
        return false;
      }

      if (isLeaf(buffer, block)) {
        insertValue(keyBuffer, keyOffset, keyLength,
                    value, isOverride, block);

        validate(block);

        return true;
      }

      long childBlockId = lookupTuple(blockId, buffer,
                                      keyBuffer, keyOffset, keyLength,
                                      false);

      while (! insert(keyBuffer, keyOffset, keyLength,
                      value, isOverride, true,
                      childBlockId)) {
        split(block, childBlockId);

        childBlockId = lookupTuple(blockId, buffer,
                                   keyBuffer, keyOffset, keyLength,
                                   false);
      }
     
      validate(block);

      return true;
    } finally {
      blockLock.unlock();
    }
  }
View Full Code Here

TOP

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

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.