简体   繁体   English

Java执行程序服务连接池

[英]Java Executor Service Connection Pool

I am attempting to use connection pooling for Executor Service. 我正在尝试将连接池用于执行器服务。

I am facing some problem when connection pool config is initialSize=3, maxToal=5, maxIdle=5. 连接池配置为initialSize = 3,maxToal = 5,maxIdle = 5时,我遇到一些问题。

I need to process 10 services at a time for every minute. 我需要每分钟一次处理10个服务。 But its picking only 5 services for every minute. 但是它每分钟仅选择5个服务。

If i configure initialSize=3, maxToal=10, maxIdle=10 then its picking 10 services for every minute.. 如果我配置initialSize = 3,maxToal = 10,maxIdle = 10,则它每分钟选择10个服务。

I am new to multithreading and connection. 我是多线程和连接的新手。 Below is my code snippet. 以下是我的代码段。 Please provide suggestion. 请提供建议。

public class TestScheduledExecutorService {
    public static void main (String a[]) {
        ScheduledExecutorService service = null;
        try {
            TestObject runnableBatch = new TestObject() {
                public void run() {
                    testMethod ();
                }
            };
            service = Executors.newSingleThreadScheduledExecutor();
            service.scheduleAtFixedRate(runnableBatch, 0, 30, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public class TestObject implements Runnable{

    public void testMethod (int inc) {
        ExecutorService service = null;
        try {
            service = Executors.newFixedThreadPool(10);
            for (int i = 0; i < 10; i++) {
                service.submit(new TestService());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
    }
}

public class TestService implements Callable{

    Connection conn = null;

    public void process(Connection conn) {
        try {
            if (conn != null) {
                System.out.println("Thread & Connection pool conn : "+Thread.currentThread() + " :: " +conn);
                // service process here
            } else {
                System.out.println("Connection pool conn is null : ");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }

    @Override
    public Object call() throws Exception {
        ConnectionPoolTest cp = ConnectionPoolTest.getInstance();
        BasicDataSource bds = cp.getBasicDataSource();
        conn = bds.getConnection();
        System.out.println(" call() "); **// it prints only 5 times for every minute eventhough total services are 10**
        process(conn);
        return null;
    }

}

public class ConnectionPoolTest {

private static ConnectionPoolTest dataSource = new ConnectionPoolTest();

    private static BasicDataSource basicDataSource = null;

    private ConnectionPoolTest() {  
    }

    public static ConnectionPoolTest getInstance() { 
        if (dataSource == null) 
            dataSource = new ConnectionPoolTest();
        return dataSource;
    }

    public BasicDataSource getBasicDataSource() throws Exception {
        try {
            basicDataSource = new BasicDataSource();

            basicDataSource.setInitialSize(3);
            basicDataSource.setMaxTotal(10);
            basicDataSource.setMaxIdle(10);
        } catch (Exception e) {
            throw e;
        }
        return basicDataSource;
    }

}

For Executor Service 对于执行人服务

 initialSize : Specified Number of Threads to spin , when New executor is created.
 maxTotal    : Number of Threads that can exist at max peak load.
 maxIdle     : Number of Thread that are kept active even if load goes below threshold.

As you mentioned, you want to pick up 10 number of tasks in parallel, we should have maxTotal set at 10. intialSize can be configured to a number that you think is optimal at the start , lets say 3 - 5. maxIdle is the number of threads you want to keep active , we generally assume how many threads are required if tasks are submitted. 正如您提到的,您要并行处理10个任务,我们应该将maxTotal设置为10。intialSize可以配置为一个您认为在一开始就最佳的数字,比如说3-5。maxIdle是该数字在您要保持活动状态的线程数中,我们通常假设提交任务后需要多少个线程。 though there is no standard recomendation, vaues might be determined a number of various factors like . 尽管没有标准建议,但可以通过多种因素来确定值。

  1. Distribution of task submitted during the minute 分钟内提交的任务分配
  2. Duration of Task 任务持续时间
  3. Urgency of executing those tasks in parallel. 迫切需要并行执行这些任务。

As you mentioned you need 10 parallel tasks, then you will have to configure 10 as maxTotal, considering your task distribution and Duration causes overlap. 如前所述,您需要10个并行任务,然后必须将10个配置为maxTotal,这要考虑到任务分配和Duration原因重叠。 If duration is pretty small , and distribution is even you can also survive with a lower number too. 如果持续时间很小,并且分布均匀,那么您也可以使用较小的数字来生存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM