为什么完成的线程比在ExecutorService中启动的线程少?

时间:2018-07-25 14:37:01

标签: java multithreading selenium-webdriver executorservice

据我了解,ExecutorService创建了一个可重用的线程池。我创建的池大小为128,并执行128个可运行任务。它启动了128个线程(通过打印“ started”开始),而只有10个线程“完成”了。 为什么会这样呢?完成的线程是否仍在重用?还是我的线程根本没有完成?

编辑: 添加了我的代码,但是我无法重现结果。但是,我确实发现,当我对保存网站的访问过多时,会遇到socketTimeoutException。

下面是我的代码示例:


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Test {

static class TestConnection extends Thread{

    @Override
    public void run() {
        System.out.println("Started "+this.getName());
        WebDriver driver = new HtmlUnitDriver(true);
        driver.get("http://google.com");
        //System.out.println(driver.getCurrentUrl());
        driver.findElement(By.tagName("html"));
        System.out.println("Finished "+this.getName());
    }
}

public static void main(String args[]) {
    ExecutorService pool = Executors.newFixedThreadPool(128);


    for(int i=0; i < 32;i++){
        TestConnection task = new TestConnection();
        pool.execute(task);
    }
    pool.shutdown();
    try {
        pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

2 个答案:

答案 0 :(得分:0)

如果将while(无效)更改为for,则效果很好。

final int count = 128;
final boolean[] finished = new boolean[count];

class RunTask extends Thread {
    private final int i;

    public RunTask(int i) {
        this.i = i;
    }

    public void run() {
        //System.out.println("start " + this.getName());
        //Do stuff. lots of I/O bound
        System.out.println("finished " + i);
        finished[i] = true;
    }
}

public void test() throws InterruptedException {
    ExecutorService pool = Executors.newFixedThreadPool(128);
    for (int i = 0; i < count; i++) {
        RunTask task = new RunTask(i);
        pool.execute(task);
    }
    pool.shutdown();
    pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    for(int i = 0; i < count; i++) {
        if(!finished[i]) {
            System.out.println("NOT FINISHED! "+i);
        }
    }
}

这不会打印NOT FINISHED

答案 1 :(得分:0)

当我尝试上述代码时,我发现了异常。

Class not found com/gargoylesoftware/htmlunit/WebWindowListener

通过异常处理,它可以按预期工作。为了解决未找到的类的错误,我添加了htmlunit driver dependency并且它工作正常。

您的代码中缺少的一件事是您需要驱动程序,否则您下次可能会遇到websocket错误,因为较早的执行未正确终止。以下是工作代码:

@Override
public void run() {
   System.out.println("Started "+this.getName());
   try {
       WebDriver driver = new HtmlUnitDriver(true);
       driver.get("http://google.com");
       //System.out.println(driver.getCurrentUrl());
       driver.findElement(By.tagName("html"));
       driver.quit();
   } catch (Throwable e) {
       System.err.println(this.getName() + " " +e.getMessage());
   }
   System.out.println("Finished "+this.getName());
}