Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java tomcat线程可以最大限度地保护spring引导中的对象池耗尽吗?_Java_Spring Boot_Tomcat_Object Pooling - Fatal编程技术网

Java tomcat线程可以最大限度地保护spring引导中的对象池耗尽吗?

Java tomcat线程可以最大限度地保护spring引导中的对象池耗尽吗?,java,spring-boot,tomcat,object-pooling,Java,Spring Boot,Tomcat,Object Pooling,如果存在一个固定大小的对象池(apache commons),并且server.tomcat.threads.max设置为相同的值,那么它是否可以防止池耗尽?如果类是@组件,那么这样做是否安全 弹簧代码如下: 简单盒子类: BoxFactory: application.properties: server.tomcat.threads.max=200 将此设置为300会耗尽池中的水,而当设置为200时,它只会在那里等待 用于测试的Shell脚本(启动400个后台进程) package com

如果存在一个固定大小的对象池(apache commons),并且server.tomcat.threads.max设置为相同的值,那么它是否可以防止池耗尽?如果类是@组件,那么这样做是否安全

弹簧代码如下:

  • 简单盒子类:
  • BoxFactory:
  • application.properties:

    server.tomcat.threads.max=200
    
    将此设置为300会耗尽池中的水,而当设置为200时,它只会在那里等待

    用于测试的Shell脚本(启动400个后台进程)

    package com.example.confusion;
    
    import org.apache.commons.pool2.BasePooledObjectFactory;
    import org.apache.commons.pool2.PooledObject;
    import org.apache.commons.pool2.impl.DefaultPooledObject;
    
    public class BoxFactory extends BasePooledObjectFactory<Box> {
        @Override
        public void passivateObject(PooledObject<Box> p) throws Exception {
            super.passivateObject(p);
        }
    
        @Override
        public Box create() throws Exception {
            return new Box();
        }
    
        @Override
        public PooledObject<Box> wrap(Box box) {
            return new DefaultPooledObject<Box>(box);
        }
    }
    
    package com.example.confusion;
    
    import org.apache.commons.pool2.impl.GenericObjectPool;
    import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
    import org.springframework.stereotype.Component;
    
    @Component
    public class BlaBla {
        GenericObjectPool<Box> pool;
        BoxFactory factory;
        public BlaBla() {
            GenericObjectPoolConfig poolCnf = new GenericObjectPoolConfig<>();
            poolCnf.setMaxTotal(200);
            poolCnf.setMaxIdle(200);
            poolCnf.setMinIdle(200);
            poolCnf.setBlockWhenExhausted(false);
            this.factory = new BoxFactory();
            this.pool = new GenericObjectPool<Box>(factory, poolCnf);
        }
    
        public Box getBox() throws Exception {
            return pool.borrowObject();
        }
    }
    
    package com.example.confusion;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.concurrent.atomic.AtomicInteger;
    
    @RestController
    public class BoxController {
    
        @Autowired
        BlaBla blabla;
    
        static AtomicInteger count;
    
        static {
            count = new AtomicInteger(0);
        }
    
        @RequestMapping("/box")
        public String getObjectAndWaitForever() {
            Box myBox;
            try {
                synchronized(this) {
                    myBox = blabla.getBox();
                    count.incrementAndGet();
                    System.out.println(count);
                }
            }
            catch(Exception e) {
                System.out.println("Box on fire - Objects finished");
            }
    
            while(true) {
                // I will wait forever
            }
        }
    }
    
    server.tomcat.threads.max=200
    
    for i in `seq 1 400`; do curl localhost:8080/box & done