How to detect Internet connection speed with Java?(如何使用 Java 检测 Internet 连接速度?)
问题描述
在我的 Java 应用程序中,我如何检测 Internet 连接速度有多快?例如,我在家里使用 AT&T Fast DSL,我想知道是否可以编写一个方法来执行以下操作:
In my Java app, how do I detect how fast the Internet connection speed is ? For instance, I use AT&T Fast DSL at home, I wonder if there is a way I can write a method that does the following :
int getInternetConnectionSpeed()
{
...
}
这将返回一个以 kbps 为单位的数字,例如 2800kbps [ 2.8 M ]
Which will return a number in kbps, something like 2800kbps [ 2.8 M ]
我问的原因是在我的应用程序中,我可以打开多个 Internet 流,这取决于用户的 Internet 连接速度,我希望它自动确定打开多少流而不阻塞应用程序.
Edit : The reason I'm asking, is in my app, I can open multiple Internet streams, depending on the users' Internet connection speed, I want it to auto determine how many streams to open without chocking the app.
推荐答案
我认为您可能以错误的方式思考问题.拍摄连接速度的快照仅表明它们在该时刻的吞吐量.当您运行占用他们带宽的测试时,他们很容易运行另一个应用程序,然后您的测量值一文不值.
I think that you could be thinking about the problem in the wrong way. Taking a snapshot of a connection speed is only an indication of their throughput at that instant in time. They could quite easily be running another application when you run test that sucks their bandwidth and then your measured values are worthless.
相反,我认为您应该不断地添加或删除线程,具体取决于它是增加还是减少它们的吞吐量.我建议这样的事情(仅限伪代码):
Instead, I think you should be constantly adding or removing threads depending on whether it increases or decreases their throughput. I'd suggest something like this (pseudo code only):
while(true) {
double speedBeforeAdding = getCurrentSpeed();
addThread();
// Wait for speed to stabilise
sleep(20 seconds);
double speedAfterAdding = getCurrentSpeed();
if(speedAfterAdding < speedBeforeAdding) {
// Undo the addition of the new thread
removeThread();
// Wait for speed to stabilise
sleep(20 seconds);
if(getNumberOfThreads() > 1) {
double speedBeforeRemoving = getCurrentSpeed();
// Remove a thread because maybe there's too many
removeThread();
// Wait for speed to stabilise
sleep(20 seconds);
double speedAfterRemoving = getCurrentSpeed();
if(speedAfterRemoving < speedBeforeRemoving) {
// Add the thread back
addThread();
// Wait for speed to stabilise
sleep(20 seconds);
}
}
}
}
您可以调整适合的睡眠时间.我在这里假设 getCurrentSpeed()
返回 all 下载线程的吞吐量,并且您能够在应用程序执行期间动态打开和关闭线程.
You can fiddle with the sleep timings to suit. I'm assuming here that getCurrentSpeed()
returns the throughput of all download threads and that you're able to dynamically open and close threads during your application's execution.
这篇关于如何使用 Java 检测 Internet 连接速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Java 检测 Internet 连接速度?


基础教程推荐
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01