API考古学之“C风格的Java API”
2007-12-26 19:05
429 查看
By pongba
#从fishbowl上看到的,非常幽默:
Occasionally in Java, you come across an API that makes you sit up and go “What were they thinking?” Take, for example, the code to list all the threads in the current ThreadGroup. Rather than having the obvious method: i.e. one that returns a list (or array) of threads, the signature looks like this:
You pass an empty array to the method, which will be filled with
To initialise the array, you must rely on
If you’re looking to avoid memory leaks in a non-garbage-collected environment, then it makes perfect sense for an API to push responsibility for memory management back up its caller, and to gracefully handle whichever buffer-size it’s given to fill. When you’ve got pervasive GC, it just looks (and is) clumsy.
So the obvious answer to “What were they thinking?”, of course, is “They were thinking like C programmers”.
Which in turn leads one to suspect that this particular API has been around since before Java was called Java.
The Fishbowl: Charles Miller's Weblog
#另一方面,这个例子完美体现了GC语言的优势。
#从fishbowl上看到的,非常幽默:
Occasionally in Java, you come across an API that makes you sit up and go “What were they thinking?” Take, for example, the code to list all the threads in the current ThreadGroup. Rather than having the obvious method: i.e. one that returns a list (or array) of threads, the signature looks like this:
int enumerate(Thread[] list)
You pass an empty array to the method, which will be filled with
Threadobjects. The method then returns the number of threads it placed in the array. If the array is not long enough to accept all the threads, the overflow will be silently discarded.
To initialise the array, you must rely on
ThreadGroup#activeCount, which only returns an approximation of the number of threads that
enumeratemight return.
If you’re looking to avoid memory leaks in a non-garbage-collected environment, then it makes perfect sense for an API to push responsibility for memory management back up its caller, and to gracefully handle whichever buffer-size it’s given to fill. When you’ve got pervasive GC, it just looks (and is) clumsy.
So the obvious answer to “What were they thinking?”, of course, is “They were thinking like C programmers”.
Which in turn leads one to suspect that this particular API has been around since before Java was called Java.
The Fishbowl: Charles Miller's Weblog
#另一方面,这个例子完美体现了GC语言的优势。
相关文章推荐
- C# 2.0中的 “泛型”的“类型参数”
- [翻译]关于“异步可插协议”(About Asynchronous Pluggable Protocols(APPs))
- 解决PD生成Oracle数据库脚本时,表名和字段带有“”双引号的方法
- “七-七”卢沟桥事变爆发
- [转载]Java 8 日期&时间 API
- java jdk & java api 帮助文档(中文、英文版)
- Excel要不要“引”
- 关于“指定的参数已超出有效值的范围。参数名 utcDate”的解决方案
- JAVA “Run as administrator” “UAC disabled” alternative solution
- sql server “动态pivot行转列”理解
- JAVA基础--JAVA API常见对象(字符串&缓冲区)11
- Java 8 Stream API Features Intermediate operations & Terminal operations
- Java API —— HashMap类 & LinkedHashMap类
- 有点像“高树”<中国…
- 【Java并发编程实战】-----“J.U.C”:ReentrantReadWriteLock
- Java基础--->02.单行注释、多行注释,文档注释和API文档。
- Atitit. 衡量项目规模 ----包含的类的数量 .net java类库包含多少类 多少个api方法??
- Java API —— IO流(数据操作流 & 内存操作流 & 打印流 & 标准输入输出流 & 随机访问流 & 合并流 & 序列化流 & Properties & NIO)
- Excel要不要“引”
- &ldquo;不管什么进程,一旦进入了系统空间,都有相同的页面映射&rdquo;之解疑