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.ProjectManager.java

public void importParticlesIntoProject(final Array<FileHandle> fileHandles, ProgressHandler progressHandler) {
    if (fileHandles == null) {
        return;/*  w w w  .jav a  2  s  .  c o  m*/
    }
    final String targetPath = currentProjectPath + "/assets/orig/particles";
    handler = progressHandler;
    currentPercent = 0;
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.execute(() -> {
        Array<FileHandle> imgs = new Array<>();
        for (FileHandle fileHandle : fileHandles) {
            if (!fileHandle.isDirectory() && fileHandle.exists()) {
                try {
                    //copy images
                    boolean allImagesFound = addParticleEffectImages(fileHandle, imgs);
                    if (allImagesFound) {
                        // copy the fileHandle
                        String newName = fileHandle.name();
                        File target = new File(targetPath + "/" + newName);
                        FileUtils.copyFile(fileHandle.file(), target);
                    }
                } catch (Exception e) {
                    //e.printStackTrace();
                    //System.out.println("Error importing particles");
                    //showError("Error importing particles \n Particle Atals not found \n Please place particle atlas and particle effect fileHandle in the same directory ");
                }
            }
        }
        if (imgs.size > 0) {
            copyImageFilesForAllResolutionsIntoProject(imgs, false);
        }
        ResolutionManager resolutionManager = facade.retrieveProxy(ResolutionManager.NAME);
        resolutionManager.rePackProjectImagesForAllResolutions();
    });
    executor.execute(() -> {
        changePercentBy(100 - currentPercent);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        handler.progressComplete();
    });
    executor.shutdown();
}

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

public void importStyleIntoProject(final FileHandle handle, ProgressHandler progressHandler) {
    if (handle == null) {
        return;/*from  w ww  . j a  v a 2 s  .  co  m*/
    }
    final String targetPath = currentProjectPath + "/assets/orig/styles";
    FileHandle fileHandle = Gdx.files.absolute(handle.path());
    final MySkin skin = new MySkin(fileHandle);
    handler = progressHandler;
    currentPercent = 0;
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.execute(() -> {
        for (int i = 0; i < skin.fontFiles.size(); i++) {
            File copyFontFile = new File(handle.path(), skin.fontFiles.get(i) + ".fnt");
            File copyImageFile = new File(handle.path(), skin.fontFiles.get(i) + ".png");
            if (!handle.isDirectory() && handle.exists() && copyFontFile.isFile() && copyFontFile.exists()
                    && copyImageFile.isFile() && copyImageFile.exists()) {
                File fileTarget = new File(targetPath + "/" + handle.name());
                File fontTarget = new File(targetPath + "/" + copyFontFile.getName());
                File imageTarget = new File(targetPath + "/" + copyImageFile.getName());
                try {
                    FileUtils.copyFile(handle.file(), fileTarget);
                    FileUtils.copyFile(copyFontFile, fontTarget);
                    FileUtils.copyFile(copyImageFile, imageTarget);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    System.err.println(e.getMessage());
                    e.printStackTrace();
                }
            } else {
                System.err.println("SOME FILES ARE MISSING");
            }
        }
    });
    executor.execute(new Runnable() {
        @Override
        public void run() {
            changePercentBy(100 - currentPercent);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            handler.progressComplete();
        }
    });
    executor.shutdown();
}

From source file:com.google.api.ads.adwords.jaxws.extensions.processors.onfile.ReportProcessorOnFile.java

private <R extends Report> void processFiles(String userId, String mccAccountId, Class<R> reportBeanClass,
        Collection<File> localFiles, ReportDefinitionDateRangeType dateRangeType, String dateStart,
        String dateEnd) {//  ww  w .  java2  s .c o  m

    final CountDownLatch latch = new CountDownLatch(localFiles.size());
    ExecutorService executorService = Executors.newFixedThreadPool(numberOfReportProcessors);

    // Processing Report Local Files
    LOGGER.info(" Procesing reports...");

    Stopwatch stopwatch = Stopwatch.createStarted();

    for (File file : localFiles) {
        LOGGER.trace(".");
        try {

            ModifiedCsvToBean<R> csvToBean = new ModifiedCsvToBean<R>();
            MappingStrategy<R> mappingStrategy = new AnnotationBasedMappingStrategy<R>(reportBeanClass);

            LOGGER.debug("Parsing file: " + file.getAbsolutePath());
            RunnableProcessorOnFile<R> runnableProcesor = new RunnableProcessorOnFile<R>(file, csvToBean,
                    mappingStrategy, dateRangeType, dateStart, dateEnd, mccAccountId, persister,
                    reportRowsSetSize);
            runnableProcesor.setLatch(latch);
            executorService.execute(runnableProcesor);

        } catch (Exception e) {
            LOGGER.error("Ignoring file (Error when processing): " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    try {
        latch.await();
    } catch (InterruptedException e) {
        LOGGER.error(e.getMessage());
        e.printStackTrace();
    }
    executorService.shutdown();
    stopwatch.stop();
    LOGGER.info("*** Finished processing all reports in " + (stopwatch.elapsed(TimeUnit.MILLISECONDS) / 1000)
            + " seconds ***\n");
}

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

public void importSpriteAnimationsIntoProject(final Array<FileHandle> fileHandles,
        ProgressHandler progressHandler) {
    if (fileHandles == null) {
        return;/* w w w .  j  a v  a 2s . c o m*/
    }
    handler = progressHandler;

    ExecutorService executor = Executors.newSingleThreadExecutor();

    executor.execute(() -> {

        String newAnimName = null;

        String rawFileName = fileHandles.get(0).name();
        String fileExtension = FilenameUtils.getExtension(rawFileName);
        if (fileExtension.equals("png")) {
            Settings settings = new Settings();
            settings.square = true;
            settings.flattenPaths = true;

            TexturePacker texturePacker = new TexturePacker(settings);
            FileHandle pngsDir = new FileHandle(fileHandles.get(0).parent().path());
            for (FileHandle entry : pngsDir.list(Overlap2DUtils.PNG_FILTER)) {
                texturePacker.addImage(entry.file());
            }
            String fileNameWithoutExt = FilenameUtils.removeExtension(rawFileName);
            String fileNameWithoutFrame = fileNameWithoutExt.replaceAll("\\d*$", "");
            String targetPath = currentWorkingPath + "/" + currentProjectVO.projectName
                    + "/assets/orig/sprite-animations" + File.separator + fileNameWithoutFrame;
            File targetDir = new File(targetPath);
            if (targetDir.exists()) {
                try {
                    FileUtils.deleteDirectory(targetDir);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            texturePacker.pack(targetDir, fileNameWithoutFrame);
            newAnimName = fileNameWithoutFrame;
        } else {
            for (FileHandle fileHandle : fileHandles) {
                try {
                    Array<File> imgs = getAtlasPages(fileHandle);
                    String fileNameWithoutExt = FilenameUtils.removeExtension(fileHandle.name());
                    String targetPath = currentWorkingPath + "/" + currentProjectVO.projectName
                            + "/assets/orig/sprite-animations" + File.separator + fileNameWithoutExt;
                    File targetDir = new File(targetPath);
                    if (targetDir.exists()) {
                        FileUtils.deleteDirectory(targetDir);
                    }
                    for (File img : imgs) {
                        FileUtils.copyFileToDirectory(img, targetDir);
                    }
                    FileUtils.copyFileToDirectory(fileHandle.file(), targetDir);
                    newAnimName = fileNameWithoutExt;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        if (newAnimName != null) {
            ResolutionManager resolutionManager = facade.retrieveProxy(ResolutionManager.NAME);
            resolutionManager.resizeSpriteAnimationForAllResolutions(newAnimName, currentProjectInfoVO);
        }
    });
    executor.execute(() -> {
        changePercentBy(100 - currentPercent);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        handler.progressComplete();
    });
    executor.shutdown();
}

From source file:com.zhaimi.message.kafka.KafkaReceiver.java

private void processStreamsByTopic(String topicKeys, List<KafkaStream<byte[], byte[]>> streamList) {
    // init stream thread pool
    ExecutorService streamPool = Executors.newFixedThreadPool(partitions);
    String[] topics = StringUtils.split(topicKeys, ",");
    if (log.isDebugEnabled())
        log.debug("???? KafkaStreamList,topic count={},topics={}, partitions/topic={}",
                topics.length, topicKeys, partitions);

    //??stream//from  ww  w  .  j ava 2  s.  c  o m
    AtomicInteger index = new AtomicInteger(0);
    for (KafkaStream<byte[], byte[]> stream : streamList) {
        Thread streamThread = new Thread() {

            @Override
            public void run() {
                int i = index.getAndAdd(1);
                if (log.isDebugEnabled())
                    log.debug("???KafkaStream -- No.={}, partitions={}", i, partitions + ":" + i);

                ConsumerIterator<byte[], byte[]> consumerIterator = stream.iterator();

                processStreamByConsumer(topicKeys, consumerIterator);
            }
        };
        streamPool.execute(streamThread);
    }
}

From source file:org.sead.nds.repository.BagGenerator.java

private void checkFiles(HashMap<String, String> shaMap, ZipFile zf) {
    ExecutorService executor = Executors.newFixedThreadPool(Repository.getNumThreads());
    ValidationJob.setZipFile(zf);/*from  ww w . j  a  va  2  s  .com*/
    ValidationJob.setBagGenerator(this);
    log.info("Validating hashes for zipped data files");
    int i = 0;
    for (Entry<String, String> entry : shaMap.entrySet()) {

        ValidationJob vj = new ValidationJob(entry.getValue(), entry.getKey());
        executor.execute(vj);
        i++;
        if (i % 1000 == 0) {
            log.info("Queuing Hash Validations: " + i);
        }
    }
    log.info("All Hash Validations Queued: " + i);

    executor.shutdown();
    try {
        while (!executor.awaitTermination(10, TimeUnit.MINUTES)) {
            log.debug("Awaiting completion of hash calculations.");
        }
    } catch (InterruptedException e) {
        log.error("Hash Calculations interrupted", e);
    }
    log.info("Hash Validations Completed");
}

From source file:org.apache.nifi.controller.scheduling.ProcessorLifecycleIT.java

/**
 * Concurrency test that is basically hammers on both stop and start
 * operation validating their idempotency.
 *//*ww w  .  j  a va  2 s. co m*/
@Test
@Ignore
public void validateLifecycleOperationOrderWithConcurrentCallsToStartStop() throws Exception {
    final FlowManagerAndSystemBundle fcsb = this.buildFlowControllerForTest();
    flowManager = fcsb.getFlowManager();

    ProcessGroup testGroup = flowManager.createProcessGroup(UUID.randomUUID().toString());
    final ProcessorNode testProcNode = flowManager.createProcessor(TestProcessor.class.getName(),
            UUID.randomUUID().toString(), fcsb.getSystemBundle().getBundleDetails().getCoordinate());
    testProcNode.setProperties(properties);
    TestProcessor testProcessor = (TestProcessor) testProcNode.getProcessor();

    // sets the scenario for the processor to run
    this.noop(testProcessor);

    ExecutorService executor = Executors.newFixedThreadPool(100);
    int startCallsCount = 10000;
    final CountDownLatch countDownCounter = new CountDownLatch(startCallsCount);
    assertCondition(() -> ScheduledState.STOPPED == testProcNode.getScheduledState());
    final Random random = new Random();
    for (int i = 0; i < startCallsCount / 2; i++) {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                LockSupport.parkNanos(random.nextInt(9000000));
                processScheduler.stopProcessor(testProcNode);
                countDownCounter.countDown();
            }
        });
    }
    for (int i = 0; i < startCallsCount / 2; i++) {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                LockSupport.parkNanos(random.nextInt(9000000));
                processScheduler.startProcessor(testProcNode, true);
                countDownCounter.countDown();
            }
        });
    }
    assertTrue(countDownCounter.await(1000000, TimeUnit.MILLISECONDS));
    String previousOperation = null;
    for (String operationName : testProcessor.operationNames) {
        if (previousOperation == null || previousOperation.equals("@OnStopped")) {
            assertEquals("@OnScheduled", operationName);
        } else if (previousOperation.equals("@OnScheduled")) {
            assertEquals("@OnUnscheduled", operationName);
        } else if (previousOperation.equals("@OnUnscheduled")) {
            assertTrue(operationName.equals("@OnStopped") || operationName.equals("@OnScheduled"));
        }
        previousOperation = operationName;
    }
    executor.shutdownNow();
}

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

public void importSpriteAnimationsIntoProject(final Array<FileHandle> fileHandles,
        ProgressHandler progressHandler) {
    if (fileHandles == null) {
        return;/*from w  w w. j  a  v  a 2s .  com*/
    }
    handler = progressHandler;

    ExecutorService executor = Executors.newSingleThreadExecutor();

    executor.execute(() -> {

        String newAnimName = null;

        String rawFileName = fileHandles.get(0).name();
        String fileExtension = FilenameUtils.getExtension(rawFileName);
        if (fileExtension.equals("png")) {
            Settings settings = new Settings();
            settings.square = true;
            settings.flattenPaths = true;

            TexturePacker texturePacker = new TexturePacker(settings);
            FileHandle pngsDir = new FileHandle(fileHandles.get(0).parent().path());
            for (FileHandle entry : pngsDir.list(Overlap2DUtils.PNG_FILTER)) {
                texturePacker.addImage(entry.file());
            }
            String fileNameWithoutExt = FilenameUtils.removeExtension(rawFileName);
            String fileNameWithoutFrame = fileNameWithoutExt.replaceAll("\\d*$", "");
            String targetPath = currentProjectPath + "/assets/orig/sprite-animations" + File.separator
                    + fileNameWithoutFrame;
            File targetDir = new File(targetPath);
            if (targetDir.exists()) {
                try {
                    FileUtils.deleteDirectory(targetDir);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            texturePacker.pack(targetDir, fileNameWithoutFrame);
            newAnimName = fileNameWithoutFrame;
        } else {
            for (FileHandle fileHandle : fileHandles) {
                try {
                    Array<File> imgs = getAtlasPages(fileHandle);
                    String fileNameWithoutExt = FilenameUtils.removeExtension(fileHandle.name());
                    String targetPath = currentProjectPath + "/assets/orig/sprite-animations" + File.separator
                            + fileNameWithoutExt;
                    File targetDir = new File(targetPath);
                    if (targetDir.exists()) {
                        FileUtils.deleteDirectory(targetDir);
                    }
                    for (File img : imgs) {
                        FileUtils.copyFileToDirectory(img, targetDir);
                    }
                    FileUtils.copyFileToDirectory(fileHandle.file(), targetDir);
                    newAnimName = fileNameWithoutExt;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        if (newAnimName != null) {
            ResolutionManager resolutionManager = facade.retrieveProxy(ResolutionManager.NAME);
            resolutionManager.resizeSpriteAnimationForAllResolutions(newAnimName, currentProjectInfoVO);
        }
    });
    executor.execute(() -> {
        changePercentBy(100 - currentPercent);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        handler.progressComplete();
    });
    executor.shutdown();
}

From source file:com.fluidops.iwb.provider.CkanProvider.java

@Override
public void gather(List<Statement> res) throws Exception {
    // Read CKAN location and establish connection
    URL registryUrl = new URL(config.location);
    HttpURLConnection registryConnection = (HttpURLConnection) registryUrl.openConnection();
    registryConnection.setRequestMethod("GET");

    // Check if connection to CKAN could be established
    if (registryConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
        String msg = String.format("Connection to the CKAN registry could not be established. (%s, %s)",
                registryConnection.getResponseCode(), registryConnection.getResponseMessage());
        logger.info(msg);/*from   w  ww.ja v  a2s.  c  o m*/
        throw new IllegalStateException(msg);
    }
    logger.trace("Connection to CKAN established successfully.");

    String siteContent = GenUtil.readUrl(registryConnection.getInputStream());

    JSONObject groupAsJson = null;
    JSONArray packageListJsonArray = null;
    try {
        groupAsJson = new JSONObject(new JSONTokener(siteContent));
        packageListJsonArray = groupAsJson.getJSONArray("packages");
    } catch (JSONException e) {
        String msg = String.format("Returned content %s is not valid JSON. Check if the registry URL is valid.",
                siteContent);
        logger.debug(msg);
        throw new IllegalStateException(msg);
    }
    logger.trace("Extracted JSON from CKAN successfully");

    // Create metadata about LOD catalog
    res.add(ProviderUtils.createStatement(CKAN.CKAN_CATALOG, RDF.TYPE, Vocabulary.DCAT.CATALOG));
    res.add(ProviderUtils.createStatement(CKAN.CKAN_CATALOG, RDFS.LABEL, CKAN.CKAN_CATALOG_LABEL));

    // Extract metadata for individual data sets listed in CKAN
    MultiThreadedHttpConnectionManager connectionManager = null;
    ExecutorService pool = null;
    try {
        pool = Executors.newFixedThreadPool(10);
        connectionManager = new MultiThreadedHttpConnectionManager();
        HttpClient client = new HttpClient(connectionManager);

        List<Statement> synchedList = Collections.synchronizedList(res);
        for (int i = 0; i < packageListJsonArray.length(); i++) {
            String host = "http://www.ckan.net/package/" + packageListJsonArray.get(i).toString();
            String baseUri = findBaseUri(
                    "http://www.ckan.net/api/rest/package/" + packageListJsonArray.get(i).toString());
            baseUri = (baseUri == null) ? host : baseUri;
            pool.execute(new MetadataReader(client, host, baseUri, CKAN.CKAN_CATALOG, synchedList));
        }
    } finally {
        if (pool != null) {
            pool.shutdown();
            pool.awaitTermination(4, TimeUnit.HOURS);
        }
        if (connectionManager != null)
            connectionManager.shutdown();
    }
}

From source file:com.aerospike.benchmarks.Main.java

private void doInserts(AerospikeClient client) throws Exception {
    ExecutorService es = Executors.newFixedThreadPool(this.nThreads);

    // Create N insert tasks
    int ntasks = this.nThreads < this.nKeys ? this.nThreads : this.nKeys;
    int start = this.startKey;
    int keysPerTask = this.nKeys / ntasks + 1;

    for (int i = 0; i < ntasks; i++) {
        InsertTask it = new InsertTaskSync(client, args, counters, start, keysPerTask);
        es.execute(it);
        start += keysPerTask;//  ww  w . j  av a 2 s  .c o m
    }
    collectInsertStats();
    es.shutdownNow();
}