Example usage for java.util.concurrent Executors newSingleThreadExecutor

List of usage examples for java.util.concurrent Executors newSingleThreadExecutor

Introduction

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

Prototype

public static ExecutorService newSingleThreadExecutor() 

Source Link

Document

Creates an Executor that uses a single worker thread operating off an unbounded queue.

Usage

From source file:com.topekalabs.synchronization.LockTest.java

@Test
public void testLockAndUnlock() {
    final long timeout = 500;

    ExecutorService es = Executors.newSingleThreadExecutor();
    Future<Integer> future = es.submit(new Callable<Integer>() {
        @Override//from   w w  w  .j  a va2  s. c o  m
        public Integer call() throws Exception {
            Lock lock = lockClass.newInstance();
            lock.lock();
            lock.unlock();
            return 1;
        }
    });

    try {
        future.get(timeout, TimeUnit.MILLISECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException ex) {
        ErrorThrower.kill(ex);
    }

    es.shutdown();
}

From source file:thingynet.Application.java

@Bean
public ExecutorService monitorExecutorService() {
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    //        executorService.submit(workflowMonitor);
    return executorService;
}

From source file:org.kaaproject.kaa.client.event.registration.DefaultEndpointRegistrationManagerTest.java

@BeforeClass
public static void beforeSuite() {
    executorContext = Mockito.mock(ExecutorContext.class);
    executor = Executors.newSingleThreadExecutor();
    Mockito.when(executorContext.getApiExecutor()).thenReturn(executor);
    Mockito.when(executorContext.getCallbackExecutor()).thenReturn(executor);
}

From source file:com.hunch.ImageManager.java

private static void startExecutor() {
    // if we already have an executor, theres no need for another one.
    if (executor != null)
        return;/*  ww  w  .ja v  a 2  s .  co m*/

    if (Const.IMG_FETCH_THREADS > 1)
        executor = Executors.newFixedThreadPool(Const.IMG_FETCH_THREADS);
    else if (Const.IMG_FETCH_THREADS == 1)
        executor = Executors.newSingleThreadExecutor();
    else
        executor = Executors.newCachedThreadPool();
}

From source file:se.omegapoint.facepalm.infrastructure.FilePolicyRepository.java

@Override
public Optional<Policy> retrievePolicyWith(final String filename) {
    notBlank(filename);//from  w  w  w. j av  a 2s  .  c o  m

    final ExecutorService executorService = Executors.newSingleThreadExecutor();
    final Command command = commandBasedOnOperatingSystem(filename);
    final Future<String> future = executorService.submit(command);

    try {
        eventService.publish(new GenericEvent(format("About to execute command[%s]", command.command)));
        return Optional.of(new Policy(future.get(TIMEOUT, TimeUnit.SECONDS)));
    } catch (Exception e) {
        return Optional.empty();
    }
}

From source file:br.com.uol.runas.classloader.ClassLoaderGC.java

public ClassLoaderGC() {
    this.classLoaders = new ConcurrentLinkedQueue<>();
    this.executor = Executors.newSingleThreadExecutor();
}

From source file:com.netflix.curator.framework.recipes.barriers.TestDistributedBarrier.java

@Test
public void testServerCrash() throws Exception {
    final int TIMEOUT = 1000;

    final CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString())
            .connectionTimeoutMs(TIMEOUT).retryPolicy(new RetryOneTime(1)).build();
    try {//from   w ww. ja  va  2  s .  c  o  m
        client.start();

        final DistributedBarrier barrier = new DistributedBarrier(client, "/barrier");
        barrier.setBarrier();

        final ExecutorService service = Executors.newSingleThreadExecutor();
        Future<Object> future = service.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                Thread.sleep(TIMEOUT / 2);
                server.stop();
                return null;
            }
        });

        barrier.waitOnBarrier(TIMEOUT * 2, TimeUnit.SECONDS);
        future.get();
        Assert.fail();
    } catch (KeeperException.ConnectionLossException expected) {
        // expected
    } finally {
        client.close();
    }
}

From source file:no.ntnu.idi.socialhitchhiking.map.RouteProvider.java

/**
 * Returning a {@link MapRoute}, containing data that is retrieved from Google Maps.
 * //from ww  w.  ja v  a 2s . c om
 * @param fromLat The latitude where the route starts.
 * @param fromLon The longitude where the route starts.
 * @param toLat The latitude where the route ends.
 * @param toLon The latitude where the route ends.
 * @return Returns a {@link MapRoute} containing all the map data needed for showing a route in a map view.
 * @throws MalformedURLException 
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 * @throws XmlPullParserException 
 */
public static MapRoute getRoute(double fromLat, double fromLon, double toLat, double toLon,
        final boolean drawable) throws MalformedURLException, IOException, XmlPullParserException {
    final String url = RouteProvider.getUrl(fromLat, fromLon, toLat, toLon);
    ExecutorService executor = Executors.newSingleThreadExecutor();

    Callable<MapRoute> callable = new Callable<MapRoute>() {
        @Override
        public MapRoute call() throws ClientProtocolException, IOException, XmlPullParserException {
            InputStream is = RouteProvider.getConnectionInputStream(url);

            MapRoute temp = new MapRoute();
            temp = RouteProvider.getRoute(is, drawable);
            return temp;
        }
    };
    Future<MapRoute> future = executor.submit(callable);
    MapRoute ret;
    try {
        ret = future.get();
    } catch (InterruptedException e) {
        ret = null;
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        ret = null;
    }
    executor.shutdown();
    return ret;

}

From source file:org.openlmis.fulfillment.ExportSchemaFlywayCallback.java

@Override
public void afterMigrate(Connection connection) {
    XLOGGER.entry(connection);/*from  w  w  w.  j  av a2  s  . c o m*/

    XLOGGER.info("After migrations, exporting db schema");

    int exitCode = 0;
    try {
        schemaName = Optional.ofNullable(schemaName).orElse("fulfillment");

        Process proc = Runtime.getRuntime().exec("/app/export_schema.sh " + schemaName);

        StreamGobbler streamGobbler = new StreamGobbler(proc.getInputStream(), XLOGGER::info);
        Executors.newSingleThreadExecutor().submit(streamGobbler);

        exitCode = proc.waitFor();
    } catch (Exception ex) {
        XLOGGER.warn("Exporting db schema failed with message: " + ex);
    }

    XLOGGER.exit(exitCode);
}

From source file:com.armtimes.downloaders.ArticleInfoDownloaderService.java

@Override
public void onCreate() {
    super.onCreate();

    mDatabaseHelper = new NewsDatabaseHelper(getApplicationContext());
    // Open database that will be used for reading and writing.
    mDatabase = mDatabaseHelper.getWritableDatabase();
    mExecutor = Executors.newSingleThreadExecutor();

}