java.util.concurrent

Interface ForkJoinPool.ManagedBlocker

  • Enclosing class:
    ForkJoinPool

    public static interface ForkJoinPool.ManagedBlocker
    Interface for extending managed parallelism for tasks running in ForkJoinPools.

    A ManagedBlocker provides two methods. Method isReleasable must return true if blocking is not necessary. Method block blocks the current thread if necessary (perhaps internally invoking isReleasable before actually blocking). These actions are performed by any thread invoking ForkJoinPool.managedBlock(java.util.concurrent.ForkJoinPool.ManagedBlocker). The unusual methods in this API accommodate synchronizers that may, but don't usually, block for long periods. Similarly, they allow more efficient internal handling of cases in which additional workers may be, but usually are not, needed to ensure sufficient parallelism. Toward this end, implementations of method isReleasable must be amenable to repeated invocation.

    For example, here is a ManagedBlocker based on a ReentrantLock:

     class ManagedLocker implements ManagedBlocker {
       final ReentrantLock lock;
       boolean hasLock = false;
       ManagedLocker(ReentrantLock lock) { this.lock = lock; }
       public boolean block() {
         if (!hasLock)
           lock.lock();
         return true;
       }
       public boolean isReleasable() {
         return hasLock || (hasLock = lock.tryLock());
       }
     }

    Here is a class that possibly blocks waiting for an item on a given queue:

     class QueueTaker<E> implements ManagedBlocker {
       final BlockingQueue<E> queue;
       volatile E item = null;
       QueueTaker(BlockingQueue<E> q) { this.queue = q; }
       public boolean block() throws InterruptedException {
         if (item == null)
           item = queue.take();
         return true;
       }
       public boolean isReleasable() {
         return item != null || (item = queue.poll()) != null;
       }
       public E getItem() { // call after pool.managedBlock completes
         return item;
       }
     }
    • Method Summary

      Methods 
      Modifier and Type Method and Description
      boolean block()
      Possibly blocks the current thread, for example waiting for a lock or condition.
      boolean isReleasable()
      Returns true if blocking is unnecessary.

        

    • Method Detail

      • block

        boolean block()
                      throws InterruptedException
        Possibly blocks the current thread, for example waiting for a lock or condition.
        Returns:
        true if no additional blocking is necessary (i.e., if isReleasable would return true)
        Throws:
        InterruptedException - if interrupted while waiting (the method is not required to do so, but is allowed to)
      • isReleasable

        boolean isReleasable()
        Returns true if blocking is unnecessary.

Traduction non disponible

Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.

Document créé le 11/06/2005, dernière modification le 04/03/2020
Source du document imprimé : https://www.gaudry.be/java-api-rf-java/util/concurrent/ForkJoinPool.ManagedBlocker.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.

Références

  1. Consulter le document html Langue du document :fr Manuel PHP : https://docs.oracle.com

Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.

Table des matières Haut