protected ThreadPoolConfig configureThreadPoolConfig(
      final NetworkListener networkListener, final ThreadPool threadPool) {

    final int maxQueueSize =
        threadPool.getMaxQueueSize() == null
            ? Integer.MAX_VALUE
            : Integer.parseInt(threadPool.getMaxQueueSize());
    final int minThreads = Integer.parseInt(threadPool.getMinThreadPoolSize());
    final int maxThreads = Integer.parseInt(threadPool.getMaxThreadPoolSize());
    final int timeout = Integer.parseInt(threadPool.getIdleThreadTimeoutSeconds());
    final ThreadPoolConfig poolConfig = ThreadPoolConfig.defaultConfig();
    poolConfig.setPoolName(networkListener.getName());
    poolConfig.setCorePoolSize(minThreads);
    poolConfig.setMaxPoolSize(maxThreads);
    poolConfig.setQueueLimit(maxQueueSize);

    // we specify the classloader that loaded this class to ensure
    // we present the same initial classloader no matter what mode
    // GlassFish is being run in.
    // See http://java.net/jira/browse/GLASSFISH-19639
    poolConfig.setInitialClassLoader(this.getClass().getClassLoader());

    poolConfig.setKeepAliveTime(timeout < 0 ? Long.MAX_VALUE : timeout, TimeUnit.SECONDS);
    if (transactionTimeoutMillis > 0 && !Utils.isDebugVM()) {
      poolConfig.setTransactionTimeout(
          obtainDelayedExecutor(), transactionTimeoutMillis, TimeUnit.MILLISECONDS);
    }

    return poolConfig;
  }
  @Override
  public ThreadPoolConfig createDefaultWorkerPoolConfig(final Transport transport) {

    final ThreadPoolConfig config = ThreadPoolConfig.defaultConfig().copy();
    final int coresCount = Runtime.getRuntime().availableProcessors();
    config.setPoolName("Grizzly-worker");
    config.setCorePoolSize(coresCount * 2);
    config.setMaxPoolSize(coresCount * 2);
    config.setMemoryManager(transport.getMemoryManager());
    return config;
  }
  @Test
  public void testCustomThreadPoolWorkerThreadStrategy() throws Exception {

    final int selectorPoolSize = Math.max(Runtime.getRuntime().availableProcessors() / 2, 1);
    final ThreadPoolConfig selectorPoolCfg = ThreadPoolConfig.defaultConfig();
    selectorPoolCfg.setCorePoolSize(selectorPoolSize).setMaxPoolSize(selectorPoolSize);

    final int workerPoolSize = Runtime.getRuntime().availableProcessors() * 2;
    final ThreadPoolConfig workerPoolCfg = ThreadPoolConfig.defaultConfig();
    workerPoolCfg.setCorePoolSize(workerPoolSize).setMaxPoolSize(workerPoolSize);

    final TCPNIOTransport tcpTransport =
        TCPNIOTransportBuilder.newInstance()
            .setReuseAddress(true)
            .setIOStrategy(WorkerThreadIOStrategy.getInstance())
            .setSelectorThreadPoolConfig(selectorPoolCfg)
            .setWorkerThreadPoolConfig(workerPoolCfg)
            .build();
    try {
      tcpTransport.start();
    } finally {
      tcpTransport.shutdownNow();
    }
  }
  /**
   * Added for http://java.net/jira/browse/GRIZZLY-1435.
   *
   * @throws Exception
   */
  @Test
  public void testThreadPoolCoreThreadInitialization() throws Exception {
    final ThreadPoolConfig config = ThreadPoolConfig.defaultConfig();
    config.setCorePoolSize(5);
    config.setMaxPoolSize(5);
    Field workers = AbstractThreadPool.class.getDeclaredField("workers");
    workers.setAccessible(true);

    final SyncThreadPool syncThreadPool = new SyncThreadPool(config);
    assertEquals(
        "Pool did not properly initialize threads based on core pool size configuration.",
        5,
        ((Map) workers.get(syncThreadPool)).size());

    config.setQueue(new ArrayBlockingQueue<Runnable>(5));
    final FixedThreadPool fixedThreadPool = new FixedThreadPool(config);
    assertEquals(
        "Pool did not properly initialize threads based on core pool size configuration.",
        5,
        ((Map) workers.get(fixedThreadPool)).size());
  }
  protected <T extends NIOTransport> T configureDefaultThreadPoolConfigs(final T transport) {
    transport.setKernelThreadPoolConfig(ThreadPoolConfig.defaultConfig());
    transport.setWorkerThreadPoolConfig(ThreadPoolConfig.defaultConfig());

    return transport;
  }