设置线程数限制

时间:2015-10-23 22:05:25

标签: java multithreading

我正在创建一个多线程下载应用程序。

每个线程都会打开一个新连接并下载内容。

我希望下载文件的连接数不超过4个。

她是download()中的代码:

private void download() {
    for (int i = 0; i < 4; i++) {
        Thread thread = new Thread(this);
        thread.start();
        System.out.println("thread active Count: " + Thread.activeCount());
    }
}

但是线程活动计数达到10

结果如下:

thread active Count: 3
thread active Count: 4
thread active Count: 5
thread active Count: 6
thread active Count: 7
thread active Count: 8
thread active Count: 9
thread active Count: 10

我想为特定文件提供4连接(线程)。

0 个答案:

没有答案
相关问题