Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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 尽管使用了执行器,但无法启动更多线程_Java_Multithreading_Concurrency_Executor - Fatal编程技术网

Java 尽管使用了执行器,但无法启动更多线程

Java 尽管使用了执行器,但无法启动更多线程,java,multithreading,concurrency,executor,Java,Multithreading,Concurrency,Executor,有人建议我使用Executors.newCachedThreadPool(),它将能够解决过度生成线程时的问题 但是,当线程数增长超过某一点时,仍然存在错误。是否允许线程在等待系统资源可用时自行等待 [WARN ] Thread table can't grow past 16383 threads. [ERROR][thread ] Could not start thread pool-1-thread-16114. errorcode -1 Exception in thread "Ma

有人建议我使用
Executors.newCachedThreadPool()
,它将能够解决过度生成线程时的问题

但是,当线程数增长超过某一点时,仍然存在错误。是否允许线程在等待系统资源可用时自行等待

[WARN ] Thread table can't grow past 16383 threads.

[ERROR][thread ] Could not start thread pool-1-thread-16114. errorcode -1
Exception in thread "Main Thread" java.lang.Error: errorcode -1
    at java.lang.Thread.start0(Native Method)
    at java.lang.Thread.start(Thread.java:640)
    at java.util.concurrent.ThreadPoolExecutor.addIfUnderMaximumPoolSize(ThreadPoolExecutor.java:727)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:657)
    at sg.java.executors_helloworld.App.main(App.java:15)

public class App {
    public static void main(String args[]) {
        ExecutorService es = Executors.newCachedThreadPool();

        long time = System.currentTimeMillis();

        for (int i = 0; i < 1000000; i++) {
            es.execute(new Car());
        }

        long completedIn = System.currentTimeMillis() - time;

        System.out.println(DurationFormatUtils.formatDuration(completedIn,
                "HH:mm:ss:SS"));
    }
}



public class Car implements Runnable {
    public void run() {
        System.out.println("Car <" + Thread.currentThread().getName()
                + "> doing something");

        try {
            Thread.sleep(10 * 1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
[WARN]线程表不能超过16383个线程。
[错误][线程]无法启动线程池-1-thread-16114。错误代码-1
线程“Main thread”java.lang中出现异常。错误:errorcode-1
位于java.lang.Thread.start0(本机方法)
在java.lang.Thread.start(Thread.java:640)处
位于java.util.concurrent.ThreadPoolExecutor.addIfUnderMaximumPoolSize(ThreadPoolExecutor.java:727)
位于java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:657)
位于sg.java.executors_helloworld.App.main(App.java:15)
公共类应用程序{
公共静态void main(字符串参数[]){
ExecutorService es=Executors.newCachedThreadPool();
长时间=System.currentTimeMillis();
对于(int i=0;i<1000000;i++){
执行(新车());
}
long completedIn=System.currentTimeMillis()-时间;
System.out.println(DurationFormatUtils.formatDuration(completedIn,
"HH:mm:ss:ss);;
}
}
公共级轿车可运行{
公开募捐{
System.out.println(“汽车做某事”);
试一试{
线程。睡眠(10*1000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}

好吧,
newCachedThreadPool

创建一个线程池,该线程池根据需要创建新线程,但在以前构造的线程可用时将重用这些线程

听起来你想要一个最大尺寸的游泳池。例如:

ExecutorService es = Executors.newFixedThreadPool(50);

一次最多使用50个线程。(当然,50可能不是合适的数字,这取决于你在做什么。)

好吧,
newCachedThreadPool

创建一个线程池,该线程池根据需要创建新线程,但在以前构造的线程可用时将重用这些线程

听起来你想要一个最大尺寸的游泳池。例如:

ExecutorService es = Executors.newFixedThreadPool(50);

一次最多使用50个线程。(当然,50可能不是合适的数字,这取决于你在做什么。)

每个线程都使用堆栈空间,当堆栈空间用完时,你无法创建更多的线程。缓存池将为每个任务创建一个线程,但除非您有很多任务花费所有时间等待,即不使用CPU,否则最好将线程数量限制在接近现有内核数量的倍数。e、 每个线程都使用堆栈空间,当堆栈空间用完时,就不能创建更多线程。缓存池将为每个任务创建一个线程,但除非您有很多任务花费所有时间等待,即不使用CPU,否则最好将线程数量限制在接近现有内核数量的倍数。e、 值得注意的是,这将使用50个螺纹,不多不少。实际上,在当前的标准库中很难获得一个核心大小低于最大值的池。值得注意的是,这将使用50个线程,不多也不少。在当前的标准库中,要获得一个核心大小低于最大值的池实际上是相当困难的。