Example usage for java.util.concurrent Executors newFixedThreadPool

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

Introduction

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

Prototype

public static ExecutorService newFixedThreadPool(int nThreads) 

Source Link

Document

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue.

Usage

From source file:com.google.code.gerrytan.executorservice.HeavyTaskRunner.java

public HeavyTaskRunner() {
    executorService = Executors.newFixedThreadPool(NUM_THREADS);
}

From source file:com.netflix.dyno.connectionpool.impl.lb.CircularListTest.java

@BeforeClass
public static void beforeClass() {
    threadPool = Executors.newFixedThreadPool(5);
}

From source file:org.lambdamatic.internal.elasticsearch.codec.ObjectMapperFactory.java

/**
 * Initializes an {@link ObjectMapper} configured with mixins to support serialization and
 * deserialization of all built-in and user-defined domain types.
 * <p>/* ww  w.ja  v  a 2 s.c  o m*/
 * <strong>Note:</strong>The {@link ObjectMapper} is instantiated and initialized once and then
 * kept in cache, so multiple calls will retrieve the same instance.
 * </p>
 * 
 * @return the {@link ObjectMapper}
 * 
 */
public static ObjectMapper getObjectMapper() {
    if (instance.objectMapper == null) {
        LOGGER.info("Initializing the ObjectMapper");
        final ObjectMapper mapper = new ObjectMapper();
        final ExecutorService availableProcessorsThreadPool = Executors
                .newFixedThreadPool(Runtime.getRuntime().availableProcessors());
        final Reflections reflections = new Reflections(new ConfigurationBuilder()

                // TODO: allow for configuration settings to reduce the scope of searching, using package
                // names instead of a classloader
                .setUrls(ClasspathHelper.forJavaClassPath())
                // .setUrls(ClasspathHelper.forClassLoader())
                .setScanners(new SubTypesScanner(), new TypeAnnotationsScanner())
                .setExecutorService(availableProcessorsThreadPool));
        // thread pool must be closed after it has been used, to avoid leaking threads in the JVM.
        availableProcessorsThreadPool.shutdown();
        // final Reflections reflections = new Reflections();
        reflections.getTypesAnnotatedWith(Mixin.class).stream().forEach(mixin -> {
            final Mixin mixinAnnotation = mixin.getAnnotation(Mixin.class);
            LOGGER.info("Adding mixin {} to {}", mixin, mixinAnnotation.target());
            mapper.addMixIn(mixinAnnotation.target(), mixin);
        });
        mapper.registerModule(new JavaTimeModule());
        // configure LocalDate serialization as string with pattern: YYYY-mm-dd
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        instance.objectMapper = mapper;
    }
    return instance.objectMapper;
}

From source file:mase.spec.SilhouetteDistanceCalculator.java

@Override
public void setup(EvolutionState state, Parameter base) {
    boolean threadedCalculation = state.parameters.getBoolean(base.push(P_THREADED), null, false);
    if (threadedCalculation) {
        executor = Executors.newFixedThreadPool(state.evalthreads);
    }//from  ww w .  j  a  va  2 s .co m
}

From source file:com.filesprocessing.listener.FilesListener.java

public FilesListener(final Configuration conf) {
    this.conf = conf;
    this.pool = Executors.newFixedThreadPool(Integer.valueOf(this.conf.getNumPoolThread()));
    pattern = Pattern.compile(conf.getFileMask());
}

From source file:gda.device.detector.mythen.data.MythenDataFileUtils.java

/**
 * Reads the specified Mythen processed data files.
 * /* w  w  w.  j  av  a 2 s.c o m*/
 * @param filenames
 *            the names of the files to read
 * @return 3D double array of data
 */
public static double[][][] readMythenProcessedDataFiles(String filenames[]) {
    // 3D array of data; will be filled in by tasks (one task per file to be loaded)
    final double[][][] data = new double[filenames.length][][];

    // thread pool for loading files
    // 4 threads seems to give good results
    ExecutorService executor = Executors.newFixedThreadPool(4);

    // create and execute a task for each file to be loaded
    for (int i = 0; i < filenames.length; i++) {
        final int index = i;
        final String filename = filenames[i];
        Runnable r = new Runnable() {
            @Override
            public void run() {
                data[index] = readMythenProcessedDataFile(filename, false);
            }

        };
        executor.execute(r);
    }

    // wait until executor has shut down
    executor.shutdown();
    try {
        boolean terminated = executor.awaitTermination(1, TimeUnit.MINUTES);
        if (!terminated) {
            throw new Exception("Timed out waiting for files to load");
        }
    } catch (Exception e) {
        throw new RuntimeException("Unable to load data", e);
    }

    return data;
}

From source file:ws.salient.session.SessionsTest.java

@Before
public void before() {
    executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);
    json = new ObjectMapper();
    sessions = new Sessions(new ClasspathRepository(), new Profiles() {
    }, new SessionStore() {
    }, Guice.createInjector(), executor, executor);
}

From source file:org.shredzone.cilla.plugin.flattr.FlattrQueue.java

@PostConstruct
public void setup() {
    executor = Executors.newFixedThreadPool(2);
}

From source file:eu.cloudscale.showcase.servlets.helpers.PaymentService.java

@Async
public Future<String> callPaymentService(String distribution, String attr1, String attr2, String attr3) {
    try {/*from  www . jav a 2 s  .  co  m*/
        ExecutorService executor = Executors.newFixedThreadPool(1);
        String url = this.getUrl(distribution, attr1, attr2, attr3);
        Future<Response> response = executor.submit(new Request(new URL(url)));
        InputStream input = response.get().getBody();
        executor.shutdown();

        String body = IOUtils.toString(input, "UTF-8");
        return new AsyncResult<String>(body);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:eionet.gdem.qa.WQExecutor.java

private WQExecutor() {
    executor = Executors.newFixedThreadPool(Properties.wqMaxJobs);
    LOGGER.debug("WQExecutor started");
}