﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-神奇好望角 The Magical Cape of Good Hope</title><link>http://www.blogjava.net/shinzey/</link><description>Sic vis pacem para bellum.</description><language>zh-cn</language><lastBuildDate>Sun, 07 Sep 2008 03:10:59 GMT</lastBuildDate><pubDate>Sun, 07 Sep 2008 03:10:59 GMT</pubDate><ttl>60</ttl><item><title>用接口实现回调</title><link>http://www.blogjava.net/shinzey/articles/185195.html</link><dc:creator>暴风雨骑士</dc:creator><author>暴风雨骑士</author><pubDate>Mon, 10 Mar 2008 13:47:00 GMT</pubDate><guid>http://www.blogjava.net/shinzey/articles/185195.html</guid><wfw:comment>http://www.blogjava.net/shinzey/comments/185195.html</wfw:comment><comments>http://www.blogjava.net/shinzey/articles/185195.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shinzey/comments/commentRss/185195.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shinzey/services/trackbacks/185195.html</trackback:ping><description><![CDATA[<table cellspacing="0" cellpadding="5" width="100%" border="0">
    <tbody>
        <tr>
            <td width="50%"><strong style="font-size: 12pt; font-family: 微软雅黑">用接口实现回调</strong></td>
            <td style="border-left: #000000 thin solid"><strong style="font-size: 12pt; font-family: 微软雅黑">Implementing Callback with Interface</strong></td>
        </tr>
        <tr>
            <td width="50%">　　C 语言里的函数指针，JavaScript 里的函数参数可以实现回调，从而完成很多动态功能。请看下面的 JavaScript 代码：</td>
            <td style="border-left: #000000 thin solid">C's function pointer and JavaScript's function parameter can implement callback, accomplishing lots of dynamic functionalities. Please look at the following JavaScript code:</td>
        </tr>
        <tr>
            <td colspan="2">
            <div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 5px; padding-bottom: 5px; border-left: #cccccc 1px solid; word-break: break-all; padding-top: 5px; border-bottom: #cccccc 1px solid; font-family: Courier New; background-color: #eeeeee">
            <ol style="margin-top: 0px; margin-bottom: 0px; font-family: Courier New">
                <li><span style="color: #0000ff">function</span> add(a, b) {
                <li>&nbsp;&nbsp;&nbsp; <span style="color: #0000ff">return</span> a + b;
                <li>}
                <li>
                <li><span style="color: #0000ff">function</span> sub(a, b) {
                <li>&nbsp;&nbsp;&nbsp; <span style="color: #0000ff">return</span> a - b;
                <li>}
                <li>
                <li><span style="color: #0000ff">function</span> cal(a, b, callback) {
                <li>&nbsp;&nbsp;&nbsp; alert(callback(a, b));
                <li>}
                <li>
                <li>cal(2, 1, add);
                <li>cal(2, 1, sub);
                <li>cal(2, 1, <span style="color: #0000ff">function</span> (a, b) {
                <li>&nbsp;&nbsp;&nbsp; <span style="color: #0000ff">return</span> a * b;
                <li>});
                <li></li>
            </ol>
            </div>
            </td>
        </tr>
        <tr>
            <td width="50%">　　在对 <span style="font-family: Courier New">cal</span> 函数的三次调用中，变量 <span style="font-family: Courier New">callback</span> 分别指向三个函数（包括一个匿名函数），从而在运行时产生不同的逻辑。如果仔细研究网上各种开源的 JS 库，会发现大量此类回调。</td>
            <td style="border-left: #000000 thin solid">In the three invokings to function <span style="font-family: Courier New">cal</span>, variable <span style="font-family: Courier New">callback</span> points to three different functions (including one anonymous function), which generates different logics at runtime. If you study various open source JS libraries on the Internet&nbsp;in depth, you will find many callbacks of this kind.</td>
        </tr>
        <tr>
            <td width="50%">　　Java 语言本身不支持指针，所以无法像 JavaScript 那样将方法名直接作为参数传递。但是利用接口，完全可以达到相同效果：</td>
            <td style="border-left: #000000 thin solid">Java language itself doesn't support pointer, so the method name can't be directly passed as a parameter like JavaScript. But&nbsp;with interface, the completely same effect can be achieved:</td>
        </tr>
        <tr>
            <td colspan="2">
            <div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 5px; padding-bottom: 5px; border-left: #cccccc 1px solid; word-break: break-all; padding-top: 5px; border-bottom: #cccccc 1px solid; font-family: Courier New; background-color: #eeeeee">
            <ol style="margin-top: 0px; margin-bottom: 0px; font-family: Courier New">
                <li><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> <strong>Cal</strong> {
                <li>
                <li>&nbsp;&nbsp;&nbsp; <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> <strong>cal</strong>(<span style="color: #0000ff">int</span> a, <span style="color: #0000ff">int</span> b);
                <li>
                <li>}
                <li>
                <li><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> <strong>Add</strong> <span style="color: #0000ff">implements</span> Cal {
                <li>
                <li>&nbsp;&nbsp;&nbsp; <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> <strong>cal</strong>(<span style="color: #0000ff">int</span> a, <span style="color: #0000ff">int</span> b) {
                <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">return</span> a + b;
                <li>&nbsp;&nbsp;&nbsp; }
                <li>
                <li>}
                <li>
                <li><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span>&nbsp;<strong>Sub</strong> <span style="color: #0000ff">implements</span> Cal {
                <li>
                <li>&nbsp;&nbsp;&nbsp; <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> <strong>cal</strong>(<span style="color: #0000ff">int</span> a, <span style="color: #0000ff">int</span> b) {
                <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">return</span> a&nbsp;- b;
                <li>&nbsp;&nbsp;&nbsp; }
                <li>
                <li>}
                <li>
                <li><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> <strong>Test</strong> {
                <li>
                <li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> <em><strong>main</strong></em>(String[] args) {
                <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>test</em>(2, 1, <span style="color: #0000ff">new</span> Add());
                <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>test</em>(2, 1, <span style="color: #0000ff">new</span> Sub());
                <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>test</em>(2, 1, <span style="color: #0000ff">new</span> Cal() {
                <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> cal(<span style="color: #0000ff">int</span> a, <span style="color: #0000ff">int</span> b) {
                <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">return</span> a * b;
                <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
                <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
                <li>&nbsp;&nbsp;&nbsp;&nbsp;}
                <li>
                <li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">private</span> <span style="color: #0000ff">static void</span>&nbsp;<strong><em>test</em></strong>(a, b, Cal c) {
                <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<em style="color: #008000">out</em>.println(c.cal(a, b));
                <li>&nbsp;&nbsp;&nbsp;&nbsp;}
                <li>
                <li>}
                <li></li>
            </ol>
            </div>
            </td>
        </tr>
    </tbody>
</table>
<img src ="http://www.blogjava.net/shinzey/aggbug/185195.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shinzey/" target="_blank">暴风雨骑士</a> 2008-03-10 21:47 <a href="http://www.blogjava.net/shinzey/articles/185195.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C 库函数 feof(FILE*) 判断文件末尾的问题</title><link>http://www.blogjava.net/shinzey/articles/165906.html</link><dc:creator>暴风雨骑士</dc:creator><author>暴风雨骑士</author><pubDate>Thu, 06 Dec 2007 15:05:00 GMT</pubDate><guid>http://www.blogjava.net/shinzey/articles/165906.html</guid><wfw:comment>http://www.blogjava.net/shinzey/comments/165906.html</wfw:comment><comments>http://www.blogjava.net/shinzey/articles/165906.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shinzey/comments/commentRss/165906.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shinzey/services/trackbacks/165906.html</trackback:ping><description><![CDATA[<table cellspacing="0" cellpadding="5" width="100%" border="0">
    <tbody>
        <tr>
            <td width="50%"><strong style="font-size: 12pt; font-family: 微软雅黑">C 库函数 feof(FILE*) 判断文件末尾的问题</strong></td>
            <td style="border-left: #000000 thin solid"><strong style="font-size: 12pt; font-family: 微软雅黑">A Problem on Using C Library Function feof(FILE*) to Judge The End of A File</strong></td>
        </tr>
        <tr>
            <td width="50%">　　嘟嘟用 C 写了一个程序读取 32768 字节大小的文件，每次读&nbsp;16 个字节，应该是 2048 次读完。但结果读了 2049 次，并且最后两次的数据相同，似乎重复读取了最后 16 个字节。源代码如下：</td>
            <td style="border-left: #000000 thin solid">&nbsp;&nbsp;&nbsp; Dudu wrote a program with C, which read a file of 32768 bytes, 16 bytes each time, and it should finish reading after 2048 times. But the reault was&nbsp;it read 2049 times, and the data of last two times are the same, which seemed the last 16 bytes were read twice. Here is the code:</td>
        </tr>
        <tr>
            <td colspan="2">
            <div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 5px; padding-bottom: 5px; border-left: #cccccc 1px solid; word-break: break-all; padding-top: 5px; border-bottom: #cccccc 1px solid; font-family: Courier New; background-color: #eeeeee">
            <ol style="margin-top: 0px; margin-bottom: 0px; font-family: Courier New">
                <li><span style="color: #0000ff">int</span> loop = 0;
                <li><span style="color: #0000ff">while</span> (!feof(file)) {
                <li>&nbsp;&nbsp;&nbsp; loop++;</font></font>
                <li>&nbsp;&nbsp;&nbsp; fread(buffer, 16, 1, file);</font>
                <li>&nbsp;&nbsp;&nbsp; ......
                <li>}
                <li>printf(<span style="color: #ff6600">"%d\n"</span>, loop);&nbsp;&nbsp;&nbsp; // 2049 </li>
            </ol>
            </div>
            </td>
        </tr>
        <tr>
            <td>　　我看了一阵，发现导致这个错误的原因是&nbsp;<span style="font-family: Courier New">feof(FILE*)</span> 判断文件末尾的机制：文件指针在文件末尾的时候，除非再读一次导致发生 I/O 错误，<span style="font-family: Courier New">feof(FILE*)</span> 依然返回 0。因此用 <span style="font-family: Courier New">feof(FILE*)</span> 作为判断条件的 while&nbsp;循环始终会多读一次，而最后一次的读取是失败的，buffer 也就没有改变，看起来就像是重复读了一次。</td>
            <td style="border-left: #000000 thin solid">&nbsp;&nbsp;&nbsp; I reviewed it for a whil and found the reason that produced this error is the mechanism&nbsp;<span style="font-family: Courier New">feof(FILE*)</span> used to judge the end of a file: When the file pointer is at the end of a file, <span style="font-family: Courier New">feof(FILE*)</span> still returns 0 unless reads one more time&nbsp;to course a I/O error. Therefore, a while loop using <span style="font-family: Courier New">feof(FILE*)</span>&nbsp;as the judgment condition always reads one more time, and the last time of reading will fail, so buffer stayed unchanged which looked like it repeated reading once.</td>
        </tr>
        <tr>
            <td>　　用下面的代码就没问题了：</td>
            <td style="border-left: #000000 thin solid">&nbsp;&nbsp;&nbsp; Use the code&nbsp;below to solve this problem:</td>
        </tr>
        <tr>
            <td colspan="2">
            <div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 5px; padding-bottom: 5px; border-left: #cccccc 1px solid; word-break: break-all; padding-top: 5px; border-bottom: #cccccc 1px solid; font-family: Courier New; background-color: #eeeeee">
            <ol style="margin-top: 0px; margin-bottom: 0px; font-family: Courier New">
                <li><span style="color: #0000ff">int</span> loop = 0;
                <li><span style="color: #0000ff">while</span> (fread(buffer, 16, 1, file) == 1) {
                <li>&nbsp;&nbsp;&nbsp; loop++;
                <li>&nbsp;&nbsp;&nbsp; ......
                <li>}
                <li>printf(<span style="color: #ff6600">"%d\n"</span>, loop); // 2048 </li>
            </ol>
            </div>
            </td>
        </tr>
    </tbody>
</table>
<img src ="http://www.blogjava.net/shinzey/aggbug/165906.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shinzey/" target="_blank">暴风雨骑士</a> 2007-12-06 23:05 <a href="http://www.blogjava.net/shinzey/articles/165906.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java 中对象引用的类型</title><link>http://www.blogjava.net/shinzey/articles/164699.html</link><dc:creator>暴风雨骑士</dc:creator><author>暴风雨骑士</author><pubDate>Sun, 02 Dec 2007 12:43:00 GMT</pubDate><guid>http://www.blogjava.net/shinzey/articles/164699.html</guid><wfw:comment>http://www.blogjava.net/shinzey/comments/164699.html</wfw:comment><comments>http://www.blogjava.net/shinzey/articles/164699.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shinzey/comments/commentRss/164699.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shinzey/services/trackbacks/164699.html</trackback:ping><description><![CDATA[<table cellspacing="0" cellpadding="5" width="100%" border="0">
    <tbody>
        <tr>
            <td width="50%"><strong style="font-size: 12pt; font-family: 微软雅黑">Java 中对象引用的类型</strong></td>
            <td style="border-left: #000000 thin solid"><strong style="font-size: 12pt; font-family: 微软雅黑">Object Reference Types in Java</strong></td>
        </tr>
        <tr>
            <td width="50%">　　弱引用早有耳闻，但从来没去认真看过。前天改编陈维雷先生的下雪动画时，发现他使用了弱引用，于是趁机把 Java 的对象引用类型看了个究竟。</td>
            <td style="border-left: #000000 thin solid">&nbsp;&nbsp;&nbsp; I've heard of weak reference for a long time, but have never study it seriously&nbsp;yet. The day before yesterday, when I was modifying Mr.&nbsp;William Chen's snowing animation, I&nbsp;saw weak reference was utilized, and then&nbsp;took the chance to&nbsp;read the&nbsp;details&nbsp;of Java's reference type.</td>
        </tr>
        <tr>
            <td>　　除了通常意义下的强引用，包 <span style="font-family: Courier New">java.lang.ref</span> 还定义了其他三种平时不太用到的引用：软引用、弱引用和虚引用，但 API 文档的解释比较含糊。我在网上搜到了一些资料，简单归纳一下。</td>
            <td style="border-left: #000000 thin solid">&nbsp;&nbsp;&nbsp; Except the strong reference of common purpose, package <span style="font-family: Courier New">java.lang.ref</span>&nbsp;defines three other references which are less often used: soft reference, weak reference and phantom reference, but they have obscure explanations in the API documention. I searched online and got some stuffs and here are my summaries.</td>
        </tr>
        <tr>
            <td>　　<strong>强引用</strong>。当一个对象具有强引用时，Java 虚拟机宁愿抛出 <span style="font-family: Courier New">OutOfMemeryError</span>，也绝不让垃圾回收器回收它。</td>
            <td style="border-left: #000000 thin solid">&nbsp;&nbsp;&nbsp; <strong>Strong Reference</strong>. When an object holds&nbsp;strong references, Java Virtue Machine would rather throw an <span style="font-family: Courier New">OutOfMemeryError</span> than&nbsp;let garbage collector (GC)&nbsp;collect it.</td>
        </tr>
        <tr>
            <td>　　<strong>软引用</strong>。当一个对象只具有软引用时，垃圾回收器只在内存不足的时候才回收它。</td>
            <td style="border-left: #000000 thin solid">&nbsp;&nbsp;&nbsp; <strong>Soft Reference</strong>. When an object holds only soft references, GC collects it only if there is not enough memory.</td>
        </tr>
        <tr>
            <td>　　<strong>弱引用</strong>。当一个对象只具有弱引用时，一旦被垃圾回收器发现就会被回收。因为垃圾回收器是一个优先级很低的线程，所以弱引用对象也不一定会马上就会被回收。</td>
            <td style="border-left: #000000 thin solid">&nbsp;&nbsp;&nbsp; <strong>Weak Reference</strong>. When an object holds only&nbsp;weak references, GC collects it as soon as finds it. GC is a thread of very low priority, so&nbsp;a weak reference object may not be collected immediately.</td>
        </tr>
        <tr>
            <td>　　<strong>虚引用</strong>。虚引用和对象的生命周期无关。虚引用必须和引用队列联合使用，对象将被回收前，其虚引用将被加入到引用队列。虚引用只是用来监视对象的回收。</td>
            <td style="border-left: #000000 thin solid">&nbsp;&nbsp;&nbsp; <strong>Phantom Reference</strong>. Phantom reference has nothing to do with the life cycle of an object. Phantom reference must be used together with reference queue, and the object's phantom reference will be added into that reference queue right&nbsp;before collected. Phantom reference is only used to monitor object collecting.</td>
        </tr>
        <tr>
            <td>　　从以上是否能看出，一个对象不能同时具有软引用和弱引用？</td>
            <td style="border-left: #000000 thin solid">&nbsp;&nbsp;&nbsp; From above shall we say that an object can't have a soft reference and&nbsp;a weak reference at the same time?</td>
        </tr>
    </tbody>
</table>
<img src ="http://www.blogjava.net/shinzey/aggbug/164699.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shinzey/" target="_blank">暴风雨骑士</a> 2007-12-02 20:43 <a href="http://www.blogjava.net/shinzey/articles/164699.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JDK 源代码中的搞笑之处</title><link>http://www.blogjava.net/shinzey/articles/164339.html</link><dc:creator>暴风雨骑士</dc:creator><author>暴风雨骑士</author><pubDate>Fri, 30 Nov 2007 09:47:00 GMT</pubDate><guid>http://www.blogjava.net/shinzey/articles/164339.html</guid><wfw:comment>http://www.blogjava.net/shinzey/comments/164339.html</wfw:comment><comments>http://www.blogjava.net/shinzey/articles/164339.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shinzey/comments/commentRss/164339.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shinzey/services/trackbacks/164339.html</trackback:ping><description><![CDATA[<table cellspacing="0" cellpadding="5" width="100%" border="0">
    <tbody>
        <tr>
            <td width="50%"><strong style="font-size: 12pt; font-family: 微软雅黑">JDK 源代码中的搞笑之处</strong></td>
            <td style="border-left: #000000 thin solid"><strong style="font-size: 12pt; font-family: 微软雅黑">Funny Things in JDK Source</strong></td>
        </tr>
        <tr>
            <td width="50%">　　虽然完整版的 JDK 源代码现已开放了，但安装在 Java\jdk[版本号] 目录下的公共&nbsp;src.zip 仍然是我最经常参考的资源。每次我遇到一个 API 问题，都会刊这个公共源代码。解决问题之余，我还找到很多有趣的东西，有时还搞笑。这里距三个例子。</td>
            <td style="border-left: #000000 thin solid">&nbsp;&nbsp;&nbsp; Though the full version of JDK source is available now, but the public&nbsp;src.zip installed&nbsp;under Java\jdk[version_number] directory is still my&nbsp;most frequent refered resource. Every time I encounter an API&nbsp;problem, this public source is read. And besides solving those problems, I've also found many interesting things which are&nbsp;sometimes also funny. Here are three exaples.</td>
        </tr>
        <tr>
            <td>　　大概从 JDK 5.0 开始，类 <span style="font-family: Courier New">java.lang.Object</span> 引入了一个叫 <span style="font-family: Courier New">wait(long timeout, int nanos)</span> 的方法。等等，nanos，纳秒？众所周知，即使在强大的 Windows 多媒体 API 里面，计时器的精度也只有一毫秒，也就是一兆纳秒。尽管 Java 非常棒，但不能处理纳秒。而源代码证明了这一点，纳秒被舍入到最接近的毫秒，0 或 1&#8230;&#8230;精彩&#8230;&#8230;</td>
            <td style="border-left: #000000 thin solid">&nbsp;&nbsp;&nbsp; Maybe since JDK 5.0, a method called <span style="font-family: Courier New">wait(long timeout, int nanos)</span>is introduced into Class&nbsp;<span style="font-family: Courier New">java.lang.Object</span>.Object. Wait a minute, nanos, is it nanoseconds? It's no secret thst even in&nbsp;powerful Windows multimedia API, the precision of timer is only one millisecond, that is a million nanosecond. Though Java is pretty great, it can not deal with nanoseconds. And the source proves it, that nanoseconds are rounded to the nearest millisecond, 0 or 1... Amazing...</td>
        </tr>
        <tr>
            <td>　　今天我想得到一个 <span style="font-family: Courier New">JDialog</span> 的所有者，但却没有 <span style="font-family: Courier New">getOwner()</span> 方法。最后我才明白 <span style="font-family: Courier New">JDialog</span> 的所有者就是它的父组件，用 <span style="font-family: Courier New">getParent()</span> 就可以了。那现在所有者等同于父级了？</td>
            <td style="border-left: #000000 thin solid">&nbsp;&nbsp;&nbsp; Today I&nbsp;wanted to get&nbsp;a <span style="font-family: Courier New">JDialog</span>'s owner,&nbsp;but there's no method called <span style="font-family: Courier New">getOwner()</span>. Finally I was awear that the owner of a <span style="font-family: Courier New">JDialog</span> is exactly its parent component, and just using&nbsp;<span style="font-family: Courier New">getParent()</span> is okey. So owner is&nbsp;synonymous&nbsp;with parent&nbsp;now?</td>
        </tr>
        <tr>
            <td>　　最后，我想提下 <span style="font-family: Courier New">JSpinner</span> 的实现有错。一些安装在 <span style="font-family: Courier New">JSpinner</span> 上的侦听器丝毫不起作用。我在 <strong>JSpinner.java</strong> 里找到这段注释：&#8220;还是不对，我们没其他办法了，<span style="font-family: Courier New">SpinnerModel</span> 和 <span style="font-family: Courier New">JFormattedTextField</span> 现已不同步了。&#8221;JDK 的开发者的诚实值得感谢。我的解决方法是直接操控复合式组件 <span style="font-family: Courier New">JSpinner</span> 中的 <span style="font-family: Courier New">JFormattedTextField</span>。</td>
            <td style="border-left: #000000 thin solid">&nbsp;&nbsp;&nbsp; At last, I wanna mention the <span style="font-family: Courier New">JSpinner</span> implementation is bugged. Some kinds of listener installed on a <span style="font-family: Courier New">JSpinner</span> take no effect at all. I found this comment in <strong>JSpinner.java</strong>: "Still bogus, nothing else we can do, the&nbsp;<span style="font-family: Courier New">SpinnerModel</span> and <span style="font-family: Courier New">JFormattedTextField</span> are now out&nbsp;of sync." The JDK developers deserve a thank for honesty. My solution is to directly&nbsp;manipulate the <span style="font-family: Courier New">JFormattedTextField</span> within&nbsp;<span style="font-family: Courier New">JSpinner</span>, a compound JComponent. </td>
        </tr>
    </tbody>
</table><img src ="http://www.blogjava.net/shinzey/aggbug/164339.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shinzey/" target="_blank">暴风雨骑士</a> 2007-11-30 17:47 <a href="http://www.blogjava.net/shinzey/articles/164339.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>My new understanding of Java's "synchronized" keyword</title><link>http://www.blogjava.net/shinzey/articles/163834.html</link><dc:creator>暴风雨骑士</dc:creator><author>暴风雨骑士</author><pubDate>Wed, 28 Nov 2007 14:35:00 GMT</pubDate><guid>http://www.blogjava.net/shinzey/articles/163834.html</guid><wfw:comment>http://www.blogjava.net/shinzey/comments/163834.html</wfw:comment><comments>http://www.blogjava.net/shinzey/articles/163834.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shinzey/comments/commentRss/163834.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shinzey/services/trackbacks/163834.html</trackback:ping><description><![CDATA[<table cellspacing="0" cellpadding="5" width="100%" border="0">
    <tbody>
        <tr>
            <td width="50%"><strong style="font-size: 12pt; font-family: 微软雅黑">我对 Java 关键字&nbsp;<span style="font-family: Courier New">Synchronized</span> 的新理解</strong></td>
            <td style="border-left: #000000 thin solid"><strong style="font-size: 12pt; font-family: 微软雅黑">My New Understanding of Java's <span style="font-family: Courier New">Synchronized</span> Keyword</strong></td>
        </tr>
        <tr>
            <td width="50%">　　说实话，我对 Java 并发编程知之不多。我曾经常用关键字 <span style="font-family: Courier New">volatile</span> 试图&#8220;强制原子操作&#8221;，结果带来的麻烦比解决的还多。Sun Java 教程中的并发课程我以前从没看完过，现在该通读一遍了。</td>
            <td style="border-left: #000000 thin solid">&nbsp;&nbsp;&nbsp; To be honest, I&nbsp;knew only a little about concurrent programming in Java.&nbsp;I uesed to use keyword <span style="font-family: Courier New">volatile</span> as an attempt to "enforce atomic operations" which had brought me more troubles than solved. Time to&nbsp;walk through&nbsp;the Concurrency Trail of Sun's Java Tutorials that I never finished reading in the past.</td>
        </tr>
        <tr>
            <td width="50%">　　我其实知道并经常看到关键字 <span style="font-family: Courier New">synchronized</span> 的使用，但直到昨天我还没发觉就这个字消除了很多同步问题。然而，真正的答案在我第一次看这个教程时就在里面了，到这次才弄清。</td>
            <td style="border-left: #000000 thin solid">&nbsp;&nbsp;&nbsp;&nbsp;I do know and often see the usage of keyword <span style="font-family: Courier New">synchronized</span>, but until yesterday I hadn't figured out how thie single word elimated so many synchronization problems. However, the very&nbsp;answer lies in those tutorials ever since I first read it and this time it has been clearly understood.</td>
        </tr>
        <tr>
            <td width="50%">　　每个对象都关联有一个内部锁，也被称作监视器锁或简称监视器。当一个线程调用一个同步方法时，它自动请求此方法的内部锁，并在方法返回时释放。即使是未捕获的异常造成了返回，也会发生锁的释放。而对静态同步方法，方法所在类的 <span style="font-family: Courier New">Class</span> 对象的内部锁被请求。同步语句的内部行为没什么两样，只是还需要显示指定一个需要请求其内部锁的任意对象。</td>
            <td style="border-left: #000000 thin solid">
            <p>&nbsp;&nbsp;&nbsp;&nbsp;Every boject has an intrinsic&nbsp;lock, which is also known as monitor lock or monitor for short, associated with it. When a thread invokes a synchronized method, it automatically acquires the intrinsic lock for that method's object and releases it when the method returns. The lock release occurs even if the return was caused by an uncaught exception.&nbsp;As for a static synchronized method, an intrinsic lock&nbsp;for the <span style="font-family: Courier New">Class</span> object of that method's Class is acquired instead. Synchronized statements internally&nbsp;behaves no differently except in addition to this,&nbsp;an arbitrary object whose intrinsic lock will be acquired can be and should be&nbsp;explicitly specified.</p>
            </td>
        </tr>
        <tr>
            <td width="50%">　　总之，<span style="font-family: Courier New">synchronized</span> 关键字是锁定对象的简单方式，也有很多局限。<span style="font-family: Courier New">java.util.concurrency.locks</span> 包支持更高深的锁定用法，也是我将要学的。</td>
            <td style="border-left: #000000 thin solid">
            <p>&nbsp;&nbsp;&nbsp; In conclusion,&nbsp;<span style="font-family: Courier New">synchronized</span> keyword is a simplified way of locking objects, and also has many limitations. More sophisticated&nbsp;locking idioms are supported by the <span style="font-family: Courier New">java.util.concurrency.locks</span> package which I am going to learn.</p>
            </td>
        </tr>
    </tbody>
</table>
<img src ="http://www.blogjava.net/shinzey/aggbug/163834.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shinzey/" target="_blank">暴风雨骑士</a> 2007-11-28 22:35 <a href="http://www.blogjava.net/shinzey/articles/163834.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>