Example usage for java.util.concurrent ExecutorService execute

List of usage examples for java.util.concurrent ExecutorService execute

Introduction

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

Prototype

void execute(Runnable command);

Source Link

Document

Executes the given command at some time in the future.

Usage

From source file:com.uwsoft.editor.proxy.ResolutionManager.java

public void createNewResolution(ResolutionEntryVO resolutionEntryVO) {
    ProjectManager projectManager = facade.retrieveProxy(ProjectManager.NAME);
    projectManager.getCurrentProjectInfoVO().resolutions.add(resolutionEntryVO);
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.execute(() -> {
        // create new folder structure
        String projPath = projectManager.getCurrentProjectPath();
        String sourcePath = projPath + "/" + "assets/orig/images";
        String targetPath = projPath + "/" + "assets/" + resolutionEntryVO.name + "/images";
        createIfNotExist(sourcePath);/*from   w w w.  ja v a  2  s  .c  o  m*/
        createIfNotExist(projPath + "/" + "assets/" + resolutionEntryVO.name + "/pack");
        copyTexturesFromTo(sourcePath, targetPath);
        int resizeWarnings = resizeTextures(targetPath, resolutionEntryVO);
        rePackProjectImages(resolutionEntryVO);
        createResizedAnimations(resolutionEntryVO);
        changePercentBy(5);
        if (resizeWarnings > 0) {
            DialogUtils.showOKDialog(Sandbox.getInstance().getUIStage(), "Warning", resizeWarnings
                    + " images were not resized for smaller resolutions due to already small size ( < 3px )");
        }
        Overlap2DFacade.getInstance().sendNotification(RESOLUTION_LIST_CHANGED);
    });
    executor.execute(() -> {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        projectManager.saveCurrentProject();
        //            handler.progressComplete();
    });
    executor.shutdown();
}

From source file:org.commonjava.util.partyline.JoinableFileManagerTest.java

@Test
public void concurrentWriteAndRead_CleanupFileEntryOnLastClose() throws Exception {
    String src = "This is a test";

    File f = temp.newFile();/*from  w  ww .  java 2  s  .c o m*/
    FileUtils.write(f, src);

    int count = 2;
    CountDownLatch writing = new CountDownLatch(count);
    CountDownLatch reading = new CountDownLatch(count);
    CountDownLatch end = new CountDownLatch(count);

    Logger logger = LoggerFactory.getLogger(getClass());
    ExecutorService executor = Executors.newCachedThreadPool();
    executor.execute(() -> {
        logger.info("Starting write: {}", f);
        try (OutputStream out = mgr.openOutputStream(f)) {
            logger.info("Signaling write starting: {}", f);
            writing.countDown();

            IOUtils.write(src, out);

            logger.info("Waiting for read to start...");
            reading.await(1, TimeUnit.SECONDS);
        } catch (IOException e) {
            e.printStackTrace();
            fail("Failed to write: " + f);
        } catch (InterruptedException e) {
            e.printStackTrace();
            fail("Write Interrupted!");
        } finally {
            end.countDown();
        }
    });

    executor.execute(() -> {
        logger.info("Signaling thread: {} has started", Thread.currentThread().getName());
        writing.countDown();
        try {
            logger.info("Waiting for other thread(s) to written...");
            writing.await(1, TimeUnit.SECONDS);

            assertThat("Threads did not written correctly!", writing.getCount(), equalTo(0L));

            logger.info("Opening: {}", f);

            try (InputStream in = mgr.openInputStream(f)) {
                logger.info("Signaling that reading has begun: {}", f);
                reading.countDown();
                assertThat(IOUtils.toString(in), equalTo(src));
            } catch (IOException e) {
                e.printStackTrace();
                fail("Cannot open: " + f);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            fail("Interrupted");
        } finally {
            logger.info("Signaling thread: {} has ended", Thread.currentThread().getName());
            end.countDown();
        }
    });

    logger.info("Waiting for end of threads");
    end.await(5, TimeUnit.SECONDS);

    assertThat("Threads did not end correctly!", end.getCount(), equalTo(0L));

    AtomicInteger counter = new AtomicInteger(0);
    mgr.getFileTree().forAll(entry -> true, entry -> counter.incrementAndGet());

    assertThat("FileEntry instance was not removed after closing!", counter.get(), equalTo(0));
}

From source file:koper.client.DefaultConsumerLauncher.java

private void startMessageReceiver(int size) {
    ExecutorService receiverPool = Executors.newFixedThreadPool(size,
            new NamedThreadFactory(MessageReceiverThread.class.getSimpleName()));

    try {/*  ww w  . j av a 2  s .c o  m*/
        for (int i = 0; i < size; i++) {
            MessageReceiver messageReceiver = this.messageReceiverClass.newInstance();
            Runnable consumerThread = new MessageReceiverThread(properties, messageReceiver, messageCenter,
                    this.listenerRegistry, this.partitions);
            receiverPool.execute(consumerThread);
        }
    } catch (Exception e) {
        log.error("startMessageReceiver :{}", e);
        throw new RuntimeException(e);
    }
}

From source file:org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClientTest.java

@Test
public void testConcurrentUpdate() throws Exception {
    TestServlet.clear();//from   w  w w .jav a2  s.  co m

    String serverUrl = jetty.getBaseUrl().toString() + "/cuss/foo";

    int cussThreadCount = 2;
    int cussQueueSize = 100;

    // for tracking callbacks from CUSS
    final AtomicInteger successCounter = new AtomicInteger(0);
    final AtomicInteger errorCounter = new AtomicInteger(0);
    final StringBuilder errors = new StringBuilder();

    @SuppressWarnings("serial")
    ConcurrentUpdateSolrClient concurrentClient = new OutcomeCountingConcurrentUpdateSolrClient(serverUrl,
            cussQueueSize, cussThreadCount, successCounter, errorCounter, errors);

    concurrentClient.setPollQueueTime(0);

    // ensure it doesn't block where there's nothing to do yet
    concurrentClient.blockUntilFinished();

    int poolSize = 5;
    ExecutorService threadPool = ExecutorUtil.newMDCAwareFixedThreadPool(poolSize,
            new SolrjNamedThreadFactory("testCUSS"));

    int numDocs = 100;
    int numRunnables = 5;
    for (int r = 0; r < numRunnables; r++)
        threadPool.execute(new SendDocsRunnable(String.valueOf(r), numDocs, concurrentClient));

    // ensure all docs are sent
    threadPool.awaitTermination(5, TimeUnit.SECONDS);
    threadPool.shutdown();

    // wait until all requests are processed by CUSS 
    concurrentClient.blockUntilFinished();
    concurrentClient.shutdownNow();

    assertEquals("post", TestServlet.lastMethod);

    // expect all requests to be successful
    int expectedSuccesses = TestServlet.numReqsRcvd.get();
    assertTrue(expectedSuccesses > 0); // at least one request must have been sent

    assertTrue("Expected no errors but got " + errorCounter.get() + ", due to: " + errors.toString(),
            errorCounter.get() == 0);
    assertTrue("Expected " + expectedSuccesses + " successes, but got " + successCounter.get(),
            successCounter.get() == expectedSuccesses);

    int expectedDocs = numDocs * numRunnables;
    assertTrue("Expected CUSS to send " + expectedDocs + " but got " + TestServlet.numDocsRcvd.get(),
            TestServlet.numDocsRcvd.get() == expectedDocs);
}

From source file:org.commonjava.util.partyline.ConcurrentReadWithOneErrorClearLocksTest.java

/**
 * Test that locks for mutiple reads clear correctly. This will setup an script of events for
 * a single file, where:/* w  w w .ja v a 2 s  . com*/
 * <ol>
 *     <li>Multiple reads happen simultaneously, read the content, and close</li>
 *     <li>A single write at the end ensures the other locks are clear</li>
 * </ol>
 * @throws Exception
 */
/*@formatter:off*/
@BMRules(rules = {
        // setup the rendezvous for all threads, which will mean everything waits until all threads are started.
        @BMRule(name = "init rendezvous", targetClass = "JoinableFileManager", targetMethod = "<init>", targetLocation = "ENTRY", action = "createCountDown(\"JOIN\", 2)"),

        // When we try to init a new JoinableFile for INPUT, simulate an IOException from somewhere deeper in the stack.
        @BMRule(name = "new JoinableFile error", targetClass = "JoinableFile", targetMethod = "joinStream", targetLocation = "ENTRY", condition = "countDown(\"JOIN\")", action = "debug(\"Throwing test exception in \" + Thread.currentThread().getName()); "
                + "throw new java.io.IOException(\"Test exception\")") })
/*@formatter:on*/
@Test
@BMUnitConfig(debug = true)
//    @Ignore( "Inconsistent result between Maven/IDEA executions; needs to be fixed before release!" )
public void run() throws Exception {
    final ExecutorService execs = Executors.newFixedThreadPool(5);
    final File f = temp.newFile("child.txt");
    FileUtils.write(f, "test data");

    final CountDownLatch latch = new CountDownLatch(4);
    CountDownLatch readBeginLatch = new CountDownLatch(3);
    CountDownLatch readEndLatch = new CountDownLatch(3);

    final JoinableFileManager manager = new JoinableFileManager();
    manager.startReporting(5000, 5000);
    final long start = System.currentTimeMillis();

    execs.execute(writer(manager, f, latch, readEndLatch));

    for (int i = 0; i < 3; i++) {
        final int k = i;
        execs.execute(reader(k, manager, f, latch, readBeginLatch, readEndLatch, null));
    }

    latch.await();
}

From source file:org.apache.hadoop.util.TestStringUtils.java

@Test
//Multithreaded Test GetFormattedTimeWithDiff()
public void testGetFormattedTimeWithDiff() throws InterruptedException {
    ExecutorService executorService = Executors.newFixedThreadPool(16);
    final CyclicBarrier cyclicBarrier = new CyclicBarrier(10);
    for (int i = 0; i < 10; i++) {

        executorService.execute(new Runnable() {
            @Override//from w w w.  j  a  v  a 2  s  .com
            public void run() {
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    //Ignored
                }
                final long end = System.currentTimeMillis();
                final long start = end - 30000;
                String formattedTime1 = StringUtils.getFormattedTimeWithDiff(FAST_DATE_FORMAT, start, end);
                String formattedTime2 = StringUtils.getFormattedTimeWithDiff(FAST_DATE_FORMAT, start, end);
                assertTrue("Method returned inconsistent results indicative of" + " a race condition",
                        formattedTime1.equals(formattedTime2));

            }
        });
    }

    executorService.shutdown();
    executorService.awaitTermination(50, TimeUnit.SECONDS);
}

From source file:org.apache.kylin.storage.hbase.util.StorageCleanupJob.java

private void cleanUnusedHBaseTables(Configuration conf) throws IOException {
    CubeManager cubeMgr = CubeManager.getInstance(KylinConfig.getInstanceFromEnv());
    // get all kylin hbase tables
    Connection conn = HBaseConnection.get(KylinConfig.getInstanceFromEnv().getStorageUrl());
    Admin hbaseAdmin = conn.getAdmin();/*from w  ww  .  j  a  v a2 s .  com*/
    String tableNamePrefix = IRealizationConstants.SharedHbaseStorageLocationPrefix;
    HTableDescriptor[] tableDescriptors = hbaseAdmin.listTables(tableNamePrefix + ".*");
    List<String> allTablesNeedToBeDropped = new ArrayList<String>();
    for (HTableDescriptor desc : tableDescriptors) {
        String host = desc.getValue(IRealizationConstants.HTableTag);
        if (KylinConfig.getInstanceFromEnv().getMetadataUrlPrefix().equalsIgnoreCase(host)) {
            //only take care htables that belongs to self, and created more than 2 days
            allTablesNeedToBeDropped.add(desc.getTableName().getNameAsString());
        }
    }

    // remove every segment htable from drop list
    for (CubeInstance cube : cubeMgr.listAllCubes()) {
        for (CubeSegment seg : cube.getSegments()) {
            String tablename = seg.getStorageLocationIdentifier();
            if (allTablesNeedToBeDropped.contains(tablename)) {
                allTablesNeedToBeDropped.remove(tablename);
                logger.info("Exclude table " + tablename + " from drop list, as the table belongs to cube "
                        + cube.getName() + " with status " + cube.getStatus());
            }
        }
    }

    if (delete == true) {
        // drop tables
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for (String htableName : allTablesNeedToBeDropped) {
            FutureTask futureTask = new FutureTask(new DeleteHTableRunnable(hbaseAdmin, htableName));
            executorService.execute(futureTask);
            try {
                futureTask.get(deleteTimeout, TimeUnit.MINUTES);
            } catch (TimeoutException e) {
                logger.warn("It fails to delete htable " + htableName + ", for it cost more than "
                        + deleteTimeout + " minutes!");
                futureTask.cancel(true);
            } catch (Exception e) {
                e.printStackTrace();
                futureTask.cancel(true);
            }
        }
        executorService.shutdown();
    } else {
        System.out.println("--------------- Tables To Be Dropped ---------------");
        for (String htableName : allTablesNeedToBeDropped) {
            System.out.println(htableName);
        }
        System.out.println("----------------------------------------------------");
    }

    hbaseAdmin.close();
}

From source file:org.commonjava.util.partyline.ConcurrentReadRuntimeErrorsClearLocksTest.java

/**
 * Test that locks for mutiple reads clear correctly. This will setup an script of events for
 * a single file, where:/*from  www.j a  v a 2s .c om*/
 * <ol>
 *     <li>Multiple reads happen simultaneously, read the content, and close</li>
 *     <li>A single write at the end ensures the other locks are clear</li>
 * </ol>
 * @throws Exception
 */
/*@formatter:off*/
@BMRules(rules = {
        // When we try to init a new JoinableFile for INPUT, simulate an IOException from somewhere deeper in the stack.
        @BMRule(name = "new JoinableFile error", targetClass = "JoinableFile", targetMethod = "<init>", targetLocation = "ENTRY", condition = "$4 == false", action = "debug(\"Throwing test exception.\"); "
                + "throw new IllegalStateException(\"Test exception\")") })
/*@formatter:on*/
@BMUnitConfig(debug = true)
@Test
public void run() throws Exception {
    final ExecutorService execs = Executors.newFixedThreadPool(5);
    final File f = temp.newFile("child.txt");
    FileUtils.write(f, "test data");

    final CountDownLatch latch = new CountDownLatch(3);
    final CountDownLatch readBeginLatch = new CountDownLatch(3);
    final CountDownLatch readEndLatch = new CountDownLatch(3);

    final JoinableFileManager manager = new JoinableFileManager();
    manager.startReporting(5000, 5000);
    final long start = System.currentTimeMillis();

    execs.execute(writer(manager, f, latch, readEndLatch));

    for (int i = 0; i < 3; i++) {
        final int k = i;
        execs.execute(reader(k, manager, f, latch, readBeginLatch, readEndLatch, null));
    }

    latch.await();
}

From source file:com.o2d.pkayjava.editor.proxy.ResolutionManager.java

public void createNewResolution(ResolutionEntryVO resolutionEntryVO) {
    ProjectManager projectManager = facade.retrieveProxy(ProjectManager.NAME);
    projectManager.getCurrentProjectInfoVO().resolutions.add(resolutionEntryVO);
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.execute(() -> {
        // create new folder structure
        String projPath = projectManager.getCurrentWorkingPath() + "/"
                + projectManager.getCurrentProjectVO().projectName;
        String sourcePath = projPath + "/" + "assets/orig/images";
        String targetPath = projPath + "/" + "assets/" + resolutionEntryVO.name + "/images";
        createIfNotExist(sourcePath);//w ww . j a  v a  2  s. co m
        createIfNotExist(projPath + "/" + "assets/" + resolutionEntryVO.name + "/pack");
        copyTexturesFromTo(sourcePath, targetPath);
        int resizeWarnings = resizeTextures(targetPath, resolutionEntryVO);
        rePackProjectImages(resolutionEntryVO);
        createResizedAnimations(resolutionEntryVO);
        changePercentBy(5);
        if (resizeWarnings > 0) {
            DialogUtils.showOKDialog(Sandbox.getInstance().getUIStage(), "Warning", resizeWarnings
                    + " images were not resized for smaller resolutions due to already small size ( < 3px )");
        }
        Overlap2DFacade.getInstance().sendNotification(RESOLUTION_LIST_CHANGED);
    });
    executor.execute(() -> {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        projectManager.saveCurrentProject();
        //            handler.progressComplete();
    });
    executor.shutdown();
}

From source file:koper.client.DefaultConsumerLauncher.java

private void startMessageDispatcher(int size) {

    //Start dispatcher thread pool
    ExecutorService dispatcherPool = Executors.newFixedThreadPool(size,
            new NamedThreadFactory(MessageDispatcherThread.class.getSimpleName()));
    try {/* w ww  .j a v  a  2s .c om*/
        for (int i = 0; i < size; i++) {
            MessageDispatcher messageDispatcher = this.messageDispatcherClass.newInstance();
            MessageDispatcherThread messageDispatcherThread = new MessageDispatcherThread(messageDispatcher,
                    this.messageCenter, this.listenerRegistry);
            dispatcherPool.execute(messageDispatcherThread);
        }
    } catch (InstantiationException | IllegalAccessException e) {
        log.error("???,  [{}]", ExceptionUtils.getFullStackTrace(e));
        throw new RuntimeException(e);
    }
}