Example usage for java.util.concurrent SynchronousQueue SynchronousQueue

List of usage examples for java.util.concurrent SynchronousQueue SynchronousQueue

Introduction

In this page you can find the example usage for java.util.concurrent SynchronousQueue SynchronousQueue.

Prototype

public SynchronousQueue() 

Source Link

Document

Creates a SynchronousQueue with nonfair access policy.

Usage

From source file:com.sentaroh.android.TaskAutomation.TaskManager.java

static final public void buildTaskExecThreadPool(final EnvironmentParms envParms,
        final TaskManagerParms taskMgrParms, final CommonUtilities util) {
    if (taskMgrParms.taskExecutorThreadPool != null)
        removeTaskExecThreadPool(envParms, taskMgrParms, util);
    SynchronousQueue<Runnable> slq = new SynchronousQueue<Runnable>();
    RejectedExecutionHandler rh = new RejectedExecutionHandler() {
        @Override/*from   w  ww.ja v  a  2 s.  c om*/
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            util.addDebugMsg(1, "W", "Task executor reject handler entered.");
            startTaskOutsideThreadPool(taskMgrParms, envParms, util, (TaskExecutor) r);
        }
    };
    taskMgrParms.taskExecutorThreadPool = new ThreadPoolExecutor(envParms.settingTaskExecThreadPoolCount + 2,
            envParms.settingTaskExecThreadPoolCount + 2, 10, TimeUnit.SECONDS, slq, rh);
    for (int i = 0; i < envParms.settingTaskExecThreadPoolCount + 2; i++) {
        final int num = i + 1;
        Runnable rt = new Runnable() {
            @Override
            public void run() {
                Thread.currentThread().setPriority(THREAD_PRIORITY_TASK_EXEC);
                Thread.currentThread().setName("TaskExec-" + num);
            }
        };
        taskMgrParms.taskExecutorThreadPool.execute(rt);
    }
    taskMgrParms.taskExecutorThreadPool.prestartAllCoreThreads();
    util.addDebugMsg(1, "I", "Task executor thread pool was created.");
}

From source file:com.sentaroh.android.TaskAutomation.TaskManager.java

static final public void buildTaskCtrlThreadPool(final EnvironmentParms envParms,
        final TaskManagerParms taskMgrParms, final CommonUtilities util) {
    if (taskMgrParms.taskControlThreadPool != null)
        removeTaskCtrlThreadPool(envParms, taskMgrParms, util);
    SynchronousQueue<Runnable> slq = new SynchronousQueue<Runnable>();
    RejectedExecutionHandler rh = new RejectedExecutionHandler() {
        @Override//from www  .j a v a2s  .  c o  m
        public void rejectedExecution(final Runnable r, ThreadPoolExecutor executor) {
            util.addDebugMsg(1, "W", "Task control reject handler entered.");
            envParms.statsUseOutsideThreadPoolCountTaskCtrl++;
            Thread th = new Thread() {
                @Override
                public void run() {
                    r.run();
                }
            };
            th.start();
        }
    };
    taskMgrParms.taskControlThreadPool = new ThreadPoolExecutor(TASK_CTRL_THREAD_POOL_COUNT,
            TASK_CTRL_THREAD_POOL_COUNT, 10, TimeUnit.SECONDS, slq, rh);
    for (int i = 0; i < TASK_CTRL_THREAD_POOL_COUNT; i++) {
        final int num = i + 1;
        Runnable rt = new Runnable() {
            @Override
            public void run() {
                Thread.currentThread().setPriority(THREAD_PRIORITY_TASK_CTRL);
                Thread.currentThread().setName("TaskCtrl-" + num);
            }
        };
        taskMgrParms.taskControlThreadPool.execute(rt);
    }
    taskMgrParms.taskControlThreadPool.prestartAllCoreThreads();
    util.addDebugMsg(1, "I", "Task control thread pool was created.");
}

From source file:org.apache.solr.cloud.BasicDistributedZkTest.java

private void testStopAndStartCoresInOneInstance() throws Exception {
    SolrClient client = clients.get(0);/*from  w ww.  j ava 2s .co  m*/
    String url3 = getBaseUrl(client);
    try (final HttpSolrClient httpSolrClient = getHttpSolrClient(url3)) {
        httpSolrClient.setConnectionTimeout(15000);
        httpSolrClient.setSoTimeout(60000);
        ThreadPoolExecutor executor = null;
        try {
            executor = new ExecutorUtil.MDCAwareThreadPoolExecutor(0, Integer.MAX_VALUE, 5, TimeUnit.SECONDS,
                    new SynchronousQueue<Runnable>(), new DefaultSolrThreadFactory("testExecutor"));
            int cnt = 3;

            // create the cores
            createCores(httpSolrClient, executor, "multiunload2", 1, cnt);
        } finally {
            if (executor != null) {
                ExecutorUtil.shutdownAndAwaitTermination(executor);
            }
        }
    }

    ChaosMonkey.stop(cloudJettys.get(0).jetty);
    printLayout();

    Thread.sleep(5000);
    ChaosMonkey.start(cloudJettys.get(0).jetty);
    cloudClient.getZkStateReader().forceUpdateCollection("multiunload2");
    try {
        cloudClient.getZkStateReader().getLeaderRetry("multiunload2", "shard1", 30000);
    } catch (SolrException e) {
        printLayout();
        throw e;
    }

    printLayout();

}

From source file:com.microsoft.tfs.core.clients.versioncontrol.VersionControlClient.java

/**
 * <p>//  ww w . j a  v  a  2  s .  c o m
 * Creates a {@link VersionControlClient} that will use the given web
 * service proxy class and the given connection. The size of the download
 * worker thread pool and the thread idle timeouts can also be specified.
 * </p>
 * <p>
 * Generally you do not instantiate this class yourself. See
 * {@link TFSTeamProjectCollection#getClient(Class)}.
 * </p>
 *
 * @param connection
 *        the connection to use (must not be <code>null</code>)
 * @param webService
 *        the {@link _RepositorySoap} web service proxy to use (must not be
 *        <code>null</code>)
 * @param webServiceExtensions
 *        the {@link _RepositoryExtensionsSoap} proxy to use for TFS 2010
 *        features (may be null)
 * @param repository4
 *        the {@link _Repository4Soap} proxy to use for TFS 2012 features
 *        (may be null)
 * @param repository5
 *        the {@link _Repository5Soap} proxy to use for TFS 2012 QU1
 *        features (may be null)
 * @param maximumGetEngineWorkerThreads
 *        the maximum number of simultaneous worker threads started to
 *        process get operations. This controls the maximum number of
 *        threads that could be performing a file download in parallel,
 *        though these worker threads perform non-network work that may
 *        prevent the workers reaching maximum theoretical network
 *        parallelism. Must be > 0.
 * @param getEngineWorkerThreadIdleTimeoutSeconds
 *        the number of seconds of consecutive idle time after which a get
 *        engine worker thread stops running. Choose a number that provides
 *        for some thread re-use between calls that process get operations
 *        (so threads can be reused from previous operations). Choosing a
 *        very large number will result in idle threads hanging around long
 *        after their last get operation, possibly consuming resources when
 *        it is unlikely there will be any get-related work for them to
 *        perform in the near future. Must be >= 0.
 */
protected VersionControlClient(final TFSTeamProjectCollection connection, final _RepositorySoap webService,
        final _RepositoryExtensionsSoap webServiceExtensions, final _Repository4Soap repository4,
        final _Repository5Soap repository5, final int maximumGetEngineWorkerThreads,
        final int getEngineWorkerThreadIdleTimeoutSeconds) {
    /*
     * TODO Make these parameters available via some {@link
     * TFSTeamProjectCollection} configuration mechanism.
     */

    Check.notNull(connection, "connection"); //$NON-NLS-1$
    Check.isTrue(maximumGetEngineWorkerThreads > 0, "GetEngine worker threads must be > 0"); //$NON-NLS-1$
    Check.isTrue(getEngineWorkerThreadIdleTimeoutSeconds >= 0, "GetEngine worker timeout must be >= 0"); //$NON-NLS-1$
    Check.notNull(webService, "webService"); //$NON-NLS-1$

    this.connection = connection;
    this._webService = webService;
    this._webServiceExtensions = webServiceExtensions;
    this._repository4 = repository4;
    this._repository5 = repository5;

    /*
     * Look up the GUID of this VersionControlClient in the local workspace
     * cache. If it's not there, fetch it from the server.
     */
    final InternalServerInfo serverInfo = Workstation.getCurrent(getConnection().getPersistenceStoreProvider())
            .getCache().getServerInfoByURI(connection.getBaseURI());
    if (null == serverInfo) {
        serverGUID = connection.getInstanceID();
    } else {
        serverGUID = serverInfo.getServerGUID();
    }

    /**
     * The default get engine worker pool. The actual Executor we use should
     * be unbounded becausethe BoundedExecutor handles the submission
     * limiting.
     */
    threadPoolExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, getEngineWorkerThreadIdleTimeoutSeconds,
            TimeUnit.SECONDS, new SynchronousQueue<Runnable>());

    uploadDownloadWorkerExecutor = new BoundedExecutor(threadPoolExecutor, maximumGetEngineWorkerThreads);

    /*
     * Configure default property filters for the Unix execute bit.
     */
    if (Platform.isCurrentPlatform(Platform.GENERIC_UNIX)
            && getWebServiceLayer().getServiceLevel().getValue() >= WebServiceLevel.TFS_2012.getValue()) {
        setDefaultItemPropertyFilters(
                new String[] { PropertyConstants.EXECUTABLE_KEY, PropertyConstants.SYMBOLIC_KEY });
    }

    // Start listening for Workstation events
    Workstation.getCurrent(getConnection().getPersistenceStoreProvider()).addWorkstationEventListener(this);
}

From source file:org.apache.hadoop.hbase.mob.MobUtils.java

/**
 * Creates a thread pool.//  w  ww  .j  a v  a  2 s  .c o m
 * @param conf the Configuration
 * @return A thread pool.
 */
public static ExecutorService createMobCompactorThreadPool(Configuration conf) {
    int maxThreads = conf.getInt(MobConstants.MOB_COMPACTION_THREADS_MAX,
            MobConstants.DEFAULT_MOB_COMPACTION_THREADS_MAX);
    if (maxThreads == 0) {
        maxThreads = 1;
    }
    final SynchronousQueue<Runnable> queue = new SynchronousQueue<Runnable>();
    ThreadPoolExecutor pool = new ThreadPoolExecutor(1, maxThreads, 60, TimeUnit.SECONDS, queue,
            Threads.newDaemonThreadFactory("MobCompactor"), new RejectedExecutionHandler() {
                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    try {
                        // waiting for a thread to pick up instead of throwing exceptions.
                        queue.put(r);
                    } catch (InterruptedException e) {
                        throw new RejectedExecutionException(e);
                    }
                }
            });
    ((ThreadPoolExecutor) pool).allowCoreThreadTimeOut(true);
    return pool;
}

From source file:org.apache.hadoop.hbase.mob.compactions.TestPartitionedMobCompactor.java

private static ExecutorService createThreadPool() {
    int maxThreads = 10;
    long keepAliveTime = 60;
    final SynchronousQueue<Runnable> queue = new SynchronousQueue<Runnable>();
    ThreadPoolExecutor pool = new ThreadPoolExecutor(1, maxThreads, keepAliveTime, TimeUnit.SECONDS, queue,
            Threads.newDaemonThreadFactory("MobFileCompactionChore"), new RejectedExecutionHandler() {
                @Override//from   ww  w. j ava2 s. c om
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    try {
                        // waiting for a thread to pick up instead of throwing exceptions.
                        queue.put(r);
                    } catch (InterruptedException e) {
                        throw new RejectedExecutionException(e);
                    }
                }
            });
    ((ThreadPoolExecutor) pool).allowCoreThreadTimeOut(true);
    return pool;
}

From source file:org.apache.hadoop.hbase.mob.compactions.TestMobCompactor.java

private static ExecutorService createThreadPool(Configuration conf) {
    int maxThreads = 10;
    long keepAliveTime = 60;
    final SynchronousQueue<Runnable> queue = new SynchronousQueue<Runnable>();
    ThreadPoolExecutor pool = new ThreadPoolExecutor(1, maxThreads, keepAliveTime, TimeUnit.SECONDS, queue,
            Threads.newDaemonThreadFactory("MobFileCompactionChore"), new RejectedExecutionHandler() {
                @Override/*from   w  w  w. ja v a  2s.c o m*/
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    try {
                        // waiting for a thread to pick up instead of throwing exceptions.
                        queue.put(r);
                    } catch (InterruptedException e) {
                        throw new RejectedExecutionException(e);
                    }
                }
            });
    ((ThreadPoolExecutor) pool).allowCoreThreadTimeOut(true);
    return pool;
}

From source file:org.apache.hadoop.hbase.client.TestHCM.java

/**
 * Tests that a destroyed connection does not have a live zookeeper.
 * Below is timing based.  We put up a connection to a table and then close the connection while
 * having a background thread running that is forcing close of the connection to try and
 * provoke a close catastrophe; we are hoping for a car crash so we can see if we are leaking
 * zk connections./*from www  . ja va  2s .  c o  m*/
 * @throws Exception
 */
@Ignore("Flakey test: See HBASE-8996")
@Test
public void testDeleteForZKConnLeak() throws Exception {
    TEST_UTIL.createTable(TABLE_NAME4, FAM_NAM);
    final Configuration config = HBaseConfiguration.create(TEST_UTIL.getConfiguration());
    config.setInt("zookeeper.recovery.retry", 1);
    config.setInt("zookeeper.recovery.retry.intervalmill", 1000);
    config.setInt("hbase.rpc.timeout", 2000);
    config.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1);

    ThreadPoolExecutor pool = new ThreadPoolExecutor(1, 10, 5, TimeUnit.SECONDS,
            new SynchronousQueue<Runnable>(), Threads.newDaemonThreadFactory("test-hcm-delete"));

    pool.submit(new Runnable() {
        @Override
        public void run() {
            while (!Thread.interrupted()) {
                try {
                    HConnection conn = HConnectionManager.getConnection(config);
                    LOG.info("Connection " + conn);
                    HConnectionManager.deleteStaleConnection(conn);
                    LOG.info("Connection closed " + conn);
                    // TODO: This sleep time should be less than the time that it takes to open and close
                    // a table.  Ideally we would do a few runs first to measure.  For now this is
                    // timing based; hopefully we hit the bad condition.
                    Threads.sleep(10);
                } catch (Exception e) {
                }
            }
        }
    });

    // Use connection multiple times.
    for (int i = 0; i < 30; i++) {
        HConnection c1 = null;
        try {
            c1 = ConnectionManager.getConnectionInternal(config);
            LOG.info("HTable connection " + i + " " + c1);
            HTable table = new HTable(config, TABLE_NAME4, pool);
            table.close();
            LOG.info("HTable connection " + i + " closed " + c1);
        } catch (Exception e) {
            LOG.info("We actually want this to happen!!!!  So we can see if we are leaking zk", e);
        } finally {
            if (c1 != null) {
                if (c1.isClosed()) {
                    // cannot use getZooKeeper as method instantiates watcher if null
                    Field zkwField = c1.getClass().getDeclaredField("keepAliveZookeeper");
                    zkwField.setAccessible(true);
                    Object watcher = zkwField.get(c1);

                    if (watcher != null) {
                        if (((ZooKeeperWatcher) watcher).getRecoverableZooKeeper().getState().isAlive()) {
                            // non-synchronized access to watcher; sleep and check again in case zk connection
                            // hasn't been cleaned up yet.
                            Thread.sleep(1000);
                            if (((ZooKeeperWatcher) watcher).getRecoverableZooKeeper().getState().isAlive()) {
                                pool.shutdownNow();
                                fail("Live zookeeper in closed connection");
                            }
                        }
                    }
                }
                c1.close();
            }
        }
    }
    pool.shutdownNow();
}

From source file:org.opendedup.sdfs.filestore.cloud.BatchAwsS3ChunkStore.java

@Override
public void run() {
    while (!closed) {
        try {//from  w w w . j a  va  2  s .  c om
            Thread.sleep(60000);
            try {
                ObjectMetadata omd = s3Service.getObjectMetadata(name, binm);
                Map<String, String> md = omd.getUserMetadata();
                ObjectMetadata nmd = new ObjectMetadata();
                nmd.setUserMetadata(md);
                md.put("currentsize", Long.toString(HashBlobArchive.currentLength.get()));
                md.put("currentcompressedsize", Long.toString(HashBlobArchive.compressedLength.get()));
                md.put("currentsize", Long.toString(HashBlobArchive.currentLength.get()));
                md.put("currentcompressedsize", Long.toString(HashBlobArchive.compressedLength.get()));
                md.put("lastupdate", Long.toString(System.currentTimeMillis()));
                md.put("hostname", InetAddress.getLocalHost().getHostName());
                md.put("port", Integer.toString(Main.sdfsCliPort));
                byte[] sz = Long.toString(System.currentTimeMillis()).getBytes();
                String st = BaseEncoding.base64().encode(ServiceUtils.computeMD5Hash(sz));
                md.put("md5sum", st);
                nmd.setContentMD5(st);
                nmd.setContentLength(sz.length);
                nmd.setUserMetadata(md);
                s3Service.putObject(this.name, binm, new ByteArrayInputStream(sz), nmd);
            } catch (Exception e) {
                try {
                    ObjectMetadata omd = s3Service.getObjectMetadata(name, binm);
                    Map<String, String> md = omd.getUserMetadata();
                    ObjectMetadata nmd = new ObjectMetadata();
                    nmd.setUserMetadata(md);
                    md.put("currentsize", Long.toString(HashBlobArchive.currentLength.get()));
                    md.put("currentcompressedsize", Long.toString(HashBlobArchive.compressedLength.get()));
                    md.put("currentsize", Long.toString(HashBlobArchive.currentLength.get()));
                    md.put("currentcompressedsize", Long.toString(HashBlobArchive.compressedLength.get()));
                    md.put("lastupdate", Long.toString(System.currentTimeMillis()));
                    md.put("hostname", InetAddress.getLocalHost().getHostName());
                    md.put("port", Integer.toString(Main.sdfsCliPort));
                    byte[] sz = Long.toString(System.currentTimeMillis()).getBytes();
                    String st = BaseEncoding.base64().encode(ServiceUtils.computeMD5Hash(sz));
                    md.put("md5sum", st);
                    nmd.setContentMD5(st);
                    nmd.setContentLength(sz.length);
                    nmd.setUserMetadata(md);

                    this.updateObject(binm, nmd);
                } catch (Exception e1) {
                    SDFSLogger.getLog().error("unable to update metadata for " + binm, e);
                }
            }

            if (this.deletes.size() > 0) {
                SDFSLogger.getLog().info("running garbage collection");
                RejectedExecutionHandler executionHandler = new BlockPolicy();
                BlockingQueue<Runnable> worksQueue = new SynchronousQueue<Runnable>();
                ThreadPoolExecutor executor = new ThreadPoolExecutor(1, Main.dseIOThreads, 10, TimeUnit.SECONDS,
                        worksQueue, executionHandler);
                this.delLock.lock();
                HashMap<Long, Integer> odel = null;
                try {
                    odel = this.deletes;
                    this.deletes = new HashMap<Long, Integer>();
                    // SDFSLogger.getLog().info("delete hash table size of "
                    // + odel.size());
                } finally {
                    this.delLock.unlock();
                }
                Set<Long> iter = odel.keySet();
                for (Long k : iter) {
                    DeleteObject obj = new DeleteObject();
                    obj.k = k;
                    obj.odel = odel;
                    obj.st = this;
                    executor.execute(obj);
                }
                executor.shutdown();
                while (!executor.awaitTermination(10, TimeUnit.SECONDS)) {
                    SDFSLogger.getLog().debug("Awaiting deletion task completion of threads.");
                }
                SDFSLogger.getLog().info("done running garbage collection");
            }
        } catch (InterruptedException e) {
            break;
        } catch (Exception e) {
            SDFSLogger.getLog().error("error in delete thread", e);
        }
    }

}

From source file:org.mozilla.gecko.GeckoAppShell.java

public static String handleGeckoMessage(String message) {
    //        //from   ww w .j ava  2s. com
    //        {"gecko": {
    //                "type": "value",
    //                "event_specific": "value",
    //                ....
    try {
        JSONObject json = new JSONObject(message);
        final JSONObject geckoObject = json.getJSONObject("gecko");
        String type = geckoObject.getString("type");

        if (type.equals("Prompt:Show")) {
            if (sPromptQueue == null)
                sPromptQueue = new SynchronousQueue<String>();
            getHandler().post(new Runnable() {
                public void run() {
                    getPromptService().processMessage(geckoObject);
                }
            });

            String promptServiceResult = "";
            try {
                while (null == (promptServiceResult = sPromptQueue.poll(1, TimeUnit.MILLISECONDS))) {
                    processNextNativeEvent();
                }
            } catch (InterruptedException e) {
                Log.i(LOGTAG, "showing prompt ", e);
            }
            return promptServiceResult;
        }

        if (mEventListeners == null)
            return "";

        if (!mEventListeners.containsKey(type))
            return "";

        ArrayList<GeckoEventListener> listeners = mEventListeners.get(type);
        Iterator<GeckoEventListener> items = listeners.iterator();
        while (items.hasNext()) {
            items.next().handleMessage(type, geckoObject);
        }

    } catch (Exception e) {
        Log.i(LOGTAG, "handleGeckoMessage throws " + e);
    }

    return "";
}