我正在创建一个多线程下载应用程序。
每个线程都会打开一个新连接并下载内容。
我希望下载文件的连接数不超过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
连接(线程)。