Search in sources :

Example 76 with LinkedBlockingQueue

use of java.util.concurrent.LinkedBlockingQueue in project hadoop by apache.

the class ContainerLauncherImpl method serviceStart.

protected void serviceStart() throws Exception {
    ThreadFactory tf = new ThreadFactoryBuilder().setNameFormat("ContainerLauncher #%d").setDaemon(true).build();
    // Start with a default core-pool size of 10 and change it dynamically.
    launcherPool = new HadoopThreadPoolExecutor(initialPoolSize, Integer.MAX_VALUE, 1, TimeUnit.HOURS, new LinkedBlockingQueue<Runnable>(), tf);
    eventHandlingThread = new Thread() {

        @Override
        public void run() {
            ContainerLauncherEvent event = null;
            Set<String> allNodes = new HashSet<String>();
            while (!stopped.get() && !Thread.currentThread().isInterrupted()) {
                try {
                    event = eventQueue.take();
                } catch (InterruptedException e) {
                    if (!stopped.get()) {
                        LOG.error("Returning, interrupted : " + e);
                    }
                    return;
                }
                allNodes.add(event.getContainerMgrAddress());
                int poolSize = launcherPool.getCorePoolSize();
                // maximum limit yet.
                if (poolSize != limitOnPoolSize) {
                    // nodes where containers will run at *this* point of time. This is
                    // *not* the cluster size and doesn't need to be.
                    int numNodes = allNodes.size();
                    int idealPoolSize = Math.min(limitOnPoolSize, numNodes);
                    if (poolSize < idealPoolSize) {
                        // Bump up the pool size to idealPoolSize+initialPoolSize, the
                        // later is just a buffer so we are not always increasing the
                        // pool-size
                        int newPoolSize = Math.min(limitOnPoolSize, idealPoolSize + initialPoolSize);
                        LOG.info("Setting ContainerLauncher pool size to " + newPoolSize + " as number-of-nodes to talk to is " + numNodes);
                        launcherPool.setCorePoolSize(newPoolSize);
                    }
                }
                // the events from the queue are handled in parallel
                // using a thread pool
                launcherPool.execute(createEventProcessor(event));
            // TODO: Group launching of multiple containers to a single
            // NodeManager into a single connection
            }
        }
    };
    eventHandlingThread.setName("ContainerLauncher Event Handler");
    eventHandlingThread.start();
    super.serviceStart();
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) HashSet(java.util.HashSet) Set(java.util.Set) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) HadoopThreadPoolExecutor(org.apache.hadoop.util.concurrent.HadoopThreadPoolExecutor)

Example 77 with LinkedBlockingQueue

use of java.util.concurrent.LinkedBlockingQueue in project XobotOS by xamarin.

the class KeyChain method bind.

/**
     * @hide for reuse by CertInstaller and Settings.
     *
     * Caller should call unbindService on the result when finished.
     */
public static KeyChainConnection bind(Context context) throws InterruptedException {
    if (context == null) {
        throw new NullPointerException("context == null");
    }
    ensureNotOnMainThread(context);
    final BlockingQueue<IKeyChainService> q = new LinkedBlockingQueue<IKeyChainService>(1);
    ServiceConnection keyChainServiceConnection = new ServiceConnection() {

        volatile boolean mConnectedAtLeastOnce = false;

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            if (!mConnectedAtLeastOnce) {
                mConnectedAtLeastOnce = true;
                try {
                    q.put(IKeyChainService.Stub.asInterface(service));
                } catch (InterruptedException e) {
                // will never happen, since the queue starts with one available slot
                }
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
    boolean isBound = context.bindService(new Intent(IKeyChainService.class.getName()), keyChainServiceConnection, Context.BIND_AUTO_CREATE);
    if (!isBound) {
        throw new AssertionError("could not bind to KeyChainService");
    }
    return new KeyChainConnection(context, keyChainServiceConnection, q.take());
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) ComponentName(android.content.ComponentName) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Example 78 with LinkedBlockingQueue

use of java.util.concurrent.LinkedBlockingQueue in project android_frameworks_base by ResurrectionRemix.

the class KeyChain method bindAsUser.

/**
     * @hide
     */
@WorkerThread
public static KeyChainConnection bindAsUser(@NonNull Context context, UserHandle user) throws InterruptedException {
    if (context == null) {
        throw new NullPointerException("context == null");
    }
    ensureNotOnMainThread(context);
    final BlockingQueue<IKeyChainService> q = new LinkedBlockingQueue<IKeyChainService>(1);
    ServiceConnection keyChainServiceConnection = new ServiceConnection() {

        volatile boolean mConnectedAtLeastOnce = false;

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            if (!mConnectedAtLeastOnce) {
                mConnectedAtLeastOnce = true;
                try {
                    q.put(IKeyChainService.Stub.asInterface(service));
                } catch (InterruptedException e) {
                // will never happen, since the queue starts with one available slot
                }
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
    Intent intent = new Intent(IKeyChainService.class.getName());
    ComponentName comp = intent.resolveSystemService(context.getPackageManager(), 0);
    intent.setComponent(comp);
    if (comp == null || !context.bindServiceAsUser(intent, keyChainServiceConnection, Context.BIND_AUTO_CREATE, user)) {
        throw new AssertionError("could not bind to KeyChainService");
    }
    return new KeyChainConnection(context, keyChainServiceConnection, q.take());
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) ComponentName(android.content.ComponentName) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) WorkerThread(android.annotation.WorkerThread)

Example 79 with LinkedBlockingQueue

use of java.util.concurrent.LinkedBlockingQueue in project voltdb by VoltDB.

the class CoreUtils method getBoundedSingleThreadExecutor.

/**
     * Create a bounded single threaded executor that rejects requests if more than capacity
     * requests are outstanding.
     */
public static ListeningExecutorService getBoundedSingleThreadExecutor(String name, int capacity) {
    LinkedBlockingQueue<Runnable> lbq = new LinkedBlockingQueue<Runnable>(capacity);
    ThreadPoolExecutor tpe = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, lbq, CoreUtils.getThreadFactory(name));
    return MoreExecutors.listeningDecorator(tpe);
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Example 80 with LinkedBlockingQueue

use of java.util.concurrent.LinkedBlockingQueue in project voltdb by VoltDB.

the class ThreadLocalRandom method getInitialSeedUniquifier.

public static synchronized long getInitialSeedUniquifier() {
    // Use the value set via the setter.
    long initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier;
    if (initialSeedUniquifier == 0) {
        // Use the system property value.
        ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier = AccessController.doPrivileged(new PrivilegedAction<Long>() {

            @Override
            public Long run() {
                return Long.getLong("io.netty.initialSeedUniquifier", 0);
            }
        });
    }
    // Otherwise, generate one.
    if (initialSeedUniquifier == 0) {
        boolean secureRandom = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {

            @Override
            public Boolean run() {
                return Boolean.getBoolean("java.util.secureRandomSeed");
            }
        });
        if (secureRandom) {
            // Try to generate a real random number from /dev/random.
            // Get from a different thread to avoid blocking indefinitely on a machine without much entropy.
            final BlockingQueue<Long> queue = new LinkedBlockingQueue<Long>();
            Thread generatorThread = new Thread("initialSeedUniquifierGenerator") {

                @Override
                public void run() {
                    // Get the real random seed from /dev/random
                    SecureRandom random = new SecureRandom();
                    final byte[] seed = random.generateSeed(8);
                    long s = ((long) seed[0] & 0xff) << 56 | ((long) seed[1] & 0xff) << 48 | ((long) seed[2] & 0xff) << 40 | ((long) seed[3] & 0xff) << 32 | ((long) seed[4] & 0xff) << 24 | ((long) seed[5] & 0xff) << 16 | ((long) seed[6] & 0xff) << 8 | (long) seed[7] & 0xff;
                    queue.add(s);
                }
            };
            generatorThread.setDaemon(true);
            generatorThread.start();
            generatorThread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {

                @Override
                public void uncaughtException(Thread t, Throwable e) {
                    logger.debug("An exception has been raised by {}", t.getName(), e);
                }
            });
            // Get the random seed from the thread with timeout.
            final long timeoutSeconds = 3;
            final long deadLine = System.nanoTime() + TimeUnit.SECONDS.toNanos(timeoutSeconds);
            boolean interrupted = false;
            for (; ; ) {
                long waitTime = deadLine - System.nanoTime();
                if (waitTime <= 0) {
                    generatorThread.interrupt();
                    logger.warn("Failed to generate a seed from SecureRandom within {} seconds. " + "Not enough entrophy?", timeoutSeconds);
                    break;
                }
                try {
                    Long seed = queue.poll(waitTime, TimeUnit.NANOSECONDS);
                    if (seed != null) {
                        initialSeedUniquifier = seed;
                        break;
                    }
                } catch (InterruptedException e) {
                    interrupted = true;
                    logger.warn("Failed to generate a seed from SecureRandom due to an InterruptedException.");
                    break;
                }
            }
            // Just in case the initialSeedUniquifier is zero or some other constant
            // just a meaningless random number
            initialSeedUniquifier ^= 0x3255ecdc33bae119L;
            initialSeedUniquifier ^= Long.reverse(System.nanoTime());
            if (interrupted) {
                // Restore the interrupt status because we don't know how to/don't need to handle it here.
                Thread.currentThread().interrupt();
                // Interrupt the generator thread if it's still running,
                // in the hope that the SecureRandom provider raises an exception on interruption.
                generatorThread.interrupt();
            }
        } else {
            initialSeedUniquifier = mix64(System.currentTimeMillis()) ^ mix64(System.nanoTime());
        }
        ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier;
    }
    return initialSeedUniquifier;
}
Also used : SecureRandom(java.security.SecureRandom) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) AtomicLong(java.util.concurrent.atomic.AtomicLong) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Aggregations

LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)259 Test (org.junit.Test)91 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)64 IOException (java.io.IOException)26 ArrayList (java.util.ArrayList)23 Emitter (io.socket.emitter.Emitter)19 JSONObject (org.json.JSONObject)19 CountDownLatch (java.util.concurrent.CountDownLatch)18 ThreadFactory (java.util.concurrent.ThreadFactory)16 ExecutorService (java.util.concurrent.ExecutorService)14 BlockingQueue (java.util.concurrent.BlockingQueue)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 List (java.util.List)12 URI (java.net.URI)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 Intent (android.content.Intent)9 HashMap (java.util.HashMap)9 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)8 Map (java.util.Map)8 UUID (java.util.UUID)8