﻿<?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-人在江湖-随笔分类-java</title><link>http://www.blogjava.net/vcycyv/category/47721.html</link><description /><language>zh-cn</language><lastBuildDate>Sun, 21 Jun 2015 02:10:31 GMT</lastBuildDate><pubDate>Sun, 21 Jun 2015 02:10:31 GMT</pubDate><ttl>60</ttl><item><title>Encountered an exception that google doesn't know: NoSuchMethodError: findMergedAnnotationAttributes(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/String;ZZ)Lorg/springframework/core/annotation/AnnotationAttributes;</title><link>http://www.blogjava.net/vcycyv/archive/2015/06/21/425810.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Sun, 21 Jun 2015 01:08:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2015/06/21/425810.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/425810.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2015/06/21/425810.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/425810.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/425810.html</trackback:ping><description><![CDATA[维基百科的创始人说过一个理论，如果一个事物在google上查不到，那基本上就说明这世界上没这个事物。我今天偏偏需要了一个google都搜不到的exception.&nbsp;<br />我在写一个用spring boot的应用，需要了这个exception:<br /><div>NoSuchMethodError: findMergedAnnotationAttributes(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/String;ZZ)Lorg/springframework/core/annotation/AnnotationAttributes;</div><div><span style="white-space:pre">	</span>at org.springframework.test.util.MetaAnnotationUtils$AnnotationDescriptor.&lt;init&gt;(MetaAnnotationUtils.java:290)</div><div><span style="white-space:pre">	</span>at org.springframework.test.util.MetaAnnotationUtils$UntypedAnnotationDescriptor.&lt;init&gt;(MetaAnnotationUtils.java:365)</div><div><span style="white-space:pre">	</span>at org.springframework.test.util.MetaAnnotationUtils$UntypedAnnotationDescriptor.&lt;init&gt;(MetaAnnotationUtils.java:360)</div><div><span style="white-space:pre">	</span>at org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptorForTypes(MetaAnnotationUtils.java:191)</div><div><span style="white-space:pre">	</span>at org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptorForTypes(MetaAnnotationUtils.java:208)</div><div><span style="white-space:pre">	</span>at org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptorForTypes(MetaAnnotationUtils.java:166)</div><div><span style="white-space:pre">	</span>at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:274)</div><div><span style="white-space:pre">	</span>at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:110)</div><div><span style="white-space:pre">	</span>at org.springframework.test.context.TestContextManager.&lt;init&gt;(TestContextManager.java:120)</div><div><span style="white-space:pre">	</span>at org.springframework.test.context.TestContextManager.&lt;init&gt;(TestContextManager.java:105)</div><div><span style="white-space:pre">	</span>at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:143)</div><div><span style="white-space:pre">	</span>at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.&lt;init&gt;(SpringJUnit4ClassRunner.java:134)</div><div><span style="white-space:pre">	</span>at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)</div><div><span style="white-space:pre">	</span>at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)</div><div><span style="white-space:pre">	</span>at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)</div><div><span style="white-space:pre">	</span>at java.lang.reflect.Constructor.newInstance(Unknown Source)</div><div><span style="white-space:pre">	</span>at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)</div><div><span style="white-space:pre">	</span>at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)</div><div><span style="white-space:pre">	</span>at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)</div><div><span style="white-space:pre">	</span>at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)</div><div><span style="white-space:pre">	</span>at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)</div><div><span style="white-space:pre">	</span>at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)</div><div><span style="white-space:pre">	</span>at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.&lt;init&gt;(JUnit4TestReference.java:33)</div><div><span style="white-space:pre">	</span>at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.&lt;init&gt;(JUnit4TestClassReference.java:25)</div><div><span style="white-space:pre">	</span>at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)</div><div><span style="white-space:pre">	</span>at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)</div><div><span style="white-space:pre">	</span>at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)</div><div><span style="white-space:pre">	</span>at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)</div><div><span style="white-space:pre">	</span>at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)</div><div><span style="white-space:pre">	</span>at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)<br /><br />The solution is: in the pom.xml, instead of using&nbsp;<br /><div>&lt;parent&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &lt;version&gt;1.3.0.BUILD-SNAPSHOT&lt;/version&gt;</div><div>&lt;/parent&gt;<br /><br />Use a specific version:</div><div>&nbsp; &nbsp; &lt;parent&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &lt;version&gt;1.2.4.RELEASE&lt;/version&gt;</div><div>&nbsp; &nbsp; &lt;/parent&gt;</div></div><div></div><img src ="http://www.blogjava.net/vcycyv/aggbug/425810.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2015-06-21 09:08 <a href="http://www.blogjava.net/vcycyv/archive/2015/06/21/425810.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>为QA准备的用以性能测试的java秒表</title><link>http://www.blogjava.net/vcycyv/archive/2013/04/04/397392.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Thu, 04 Apr 2013 14:28:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2013/04/04/397392.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/397392.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2013/04/04/397392.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/397392.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/397392.html</trackback:ping><description><![CDATA[<div style="font-family: 微软雅黑; font-size: 18px; line-height: normal; orphans: 2; text-align: -webkit-auto; widows: 2;">以前QA做性能测试用手机的秒表，很不方便。前两年为QA做了一个java秒表，记录鼠标两次点击之间的时间差。但是那是基于JInvoke的，JInvoke是收费软件，有试用的时限，用来封装JNI编程的复杂性。最近QA要求我再搞一个完全免费的类似软件。这次用的是jnativehook.</div><div style="font-family: 微软雅黑; font-size: 18px; line-height: normal; orphans: 2; text-align: -webkit-auto; widows: 2;"></div><div style="font-family: 微软雅黑; font-size: 18px; line-height: normal; orphans: 2; text-align: -webkit-auto; widows: 2;">jnativehook可以用来监控键盘和鼠标的事件。底层通过调用不同平台的native code屏蔽了平台相关性。它强大的地方在于，基于jnativehook的软件可以后台运行，没有类似&#8220;监控<strong>某窗口</strong>内键盘/鼠标事件&#8221;的限制。</div><div style="font-family: 微软雅黑; font-size: 18px; line-height: normal; orphans: 2; text-align: -webkit-auto; widows: 2;"></div><div style="font-family: 微软雅黑; font-size: 18px; line-height: normal; orphans: 2; text-align: -webkit-auto; widows: 2;">扩展jnativehook的这个小工具可以记录两次鼠标点击之间的时间差，更确切地说，是记录<strong>第一次鼠标抬起和第二次鼠标按下</strong>的时间差。举一个QA使用的例子，比如在UI上点击一个button，等着执行成功弹出的对话框。QA在点击button时，鼠标一抬起，这个小工具就开始计时，当QA看见对话框时，马上在屏幕任意位置点鼠标，软件就记录了这两次点击之间的时间差。当然，这样做还是不够精确，存在人反应时间的误差。</div><div style="font-family: 微软雅黑; font-size: 18px; line-height: normal; orphans: 2; text-align: -webkit-auto; widows: 2;"></div><div style="font-family: 微软雅黑; font-size: 18px; line-height: normal; orphans: 2; text-align: -webkit-auto; widows: 2;">jnativehook项目的主页在 https://code.google.com/p/jnativehook/</div><div style="font-family: 微软雅黑; font-size: 18px; line-height: normal; orphans: 2; text-align: -webkit-auto; widows: 2;"></div><div style="font-family: 微软雅黑; font-size: 18px; line-height: normal; orphans: 2; text-align: -webkit-auto; widows: 2;">我扩展程序的源代码如下：</div><div style="font-family: 微软雅黑; font-size: 18px; line-height: normal; orphans: 2; text-align: -webkit-auto; widows: 2;"><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">import</span>&nbsp;org.jnativehook.GlobalScreen;<br /><span style="color: #0000FF; ">import</span>&nbsp;org.jnativehook.NativeHookException;<br /><span style="color: #0000FF; ">import</span>&nbsp;org.jnativehook.mouse.NativeMouseEvent;<br /><span style="color: #0000FF; ">import</span>&nbsp;org.jnativehook.mouse.NativeMouseInputListener;<br /><br /><br /><span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">class</span>&nbsp;MouseHook&nbsp;&nbsp;<span style="color: #0000FF; ">implements</span>&nbsp;NativeMouseInputListener&nbsp;&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">static</span>&nbsp;<span style="color: #0000FF; ">double</span>&nbsp;startPoint&nbsp;=&nbsp;0.0;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">/**</span><span style="color: #008000; "><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span><span style="color: #808080; ">@param</span><span style="color: #008000; ">&nbsp;args<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">*/</span><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">static</span>&nbsp;<span style="color: #0000FF; ">void</span>&nbsp;main(String[]&nbsp;args)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">try</span>&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GlobalScreen.registerNativeHook();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">catch</span>&nbsp;(NativeHookException&nbsp;ex)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.err.println("There&nbsp;was&nbsp;a&nbsp;problem&nbsp;registering&nbsp;the&nbsp;native&nbsp;hook.");<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.err.println(ex.getMessage());<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.exit(1);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">Construct&nbsp;the&nbsp;example&nbsp;object.</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MouseHook&nbsp;example&nbsp;=&nbsp;<span style="color: #0000FF; ">new</span>&nbsp;MouseHook();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">Add&nbsp;the&nbsp;appropriate&nbsp;listeners&nbsp;for&nbsp;the&nbsp;example&nbsp;object.</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;GlobalScreen.getInstance().addNativeMouseListener(example);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;@Override<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">void</span>&nbsp;nativeMouseClicked(NativeMouseEvent&nbsp;arg0)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;TODO&nbsp;Auto-generated&nbsp;method&nbsp;stub</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;@Override<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">void</span>&nbsp;nativeMousePressed(NativeMouseEvent&nbsp;arg0)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println((System.currentTimeMillis()&nbsp;-&nbsp;startPoint)/1000);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;@Override<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">void</span>&nbsp;nativeMouseReleased(NativeMouseEvent&nbsp;arg0)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;startPoint&nbsp;=&nbsp;System.currentTimeMillis();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;@Override<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">void</span>&nbsp;nativeMouseDragged(NativeMouseEvent&nbsp;arg0)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;TODO&nbsp;Auto-generated&nbsp;method&nbsp;stub</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;@Override<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">void</span>&nbsp;nativeMouseMoved(NativeMouseEvent&nbsp;arg0)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;TODO&nbsp;Auto-generated&nbsp;method&nbsp;stub</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />}</div></div><div style="font-family: 微软雅黑; font-size: 18px; line-height: normal; orphans: 2; text-align: -webkit-auto; widows: 2;">可以在eclipse里右键点java 项目，在export...里选择Java目录下的Runnable Jar File</div><div style="font-family: 微软雅黑; font-size: 18px; line-height: normal; orphans: 2; text-align: -webkit-auto; widows: 2;">假如你保存的jar文件叫mousehook.jar. 可以直接使用java -jar mousehook.jar启动就可以使用了。</div><img src ="http://www.blogjava.net/vcycyv/aggbug/397392.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2013-04-04 22:28 <a href="http://www.blogjava.net/vcycyv/archive/2013/04/04/397392.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于调优之数据库篇（一）</title><link>http://www.blogjava.net/vcycyv/archive/2013/03/31/397206.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Sun, 31 Mar 2013 13:49:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2013/03/31/397206.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/397206.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2013/03/31/397206.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/397206.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/397206.html</trackback:ping><description><![CDATA[<div style="font-family: 微软雅黑; line-height: normal; orphans: 2; text-align: -webkit-auto; widows: 2;">这篇文字主要讨论sql的一般编写原则。下一篇讨论根据执行计划进行调优的话题。</div>
<div style="font-family: 微软雅黑; line-height: normal; orphans: 2; text-align: -webkit-auto; widows: 2;">网上这类文章很多，但往往只是给出结论，比如，这样写sql会比那样写sql效率更高。阅读者如果打算打开数据库自己做一遍实验，看看效率差异到底有多少，需要自己造数据，还是比较麻烦。这篇文字会把DDL和DML以及sql的执行时间都写出来，一方面给一个更直观的印象，另一方面方便阅读者自己实验。</div>
<div style="font-family: 微软雅黑; line-height: normal; orphans: 2; text-align: -webkit-auto; widows: 2;"></div>
<div style="font-family: 微软雅黑; line-height: normal; orphans: 2; text-align: -webkit-auto; widows: 2;">以Oracle为实验用数据库，使用著名的emp表。安装oracle数据库之后，scott用户的密码是tiger. 在他的schema下有几张sample表，很多sql教程都以这几张表为基础. 据说，熟悉oracle数据库的人提到smith这个人名，就能联想起sample表中他的工作是clerk. emp表就是sample表之一。这张表原始数据只有14行，为了体现不同sql性能上的差异，我们需要多填充一些数据进去。作为填充数据的预备知识，我们可以看一下如何生成一系列从小到大的id:<br />
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000FF; ">SELECT</span>&nbsp;ROWNUM<br />
<span style="color: #0000FF; ">FROM</span>&nbsp;DUAL<br />
CONNECT&nbsp;<span style="color: #0000FF; ">BY</span>&nbsp;<span style="color: #0000FF; ">LEVEL</span>&nbsp;<span style="color: #808080; ">&lt;</span>&nbsp;<span style="color: #800000; font-weight: bold; ">10000</span>;</div>
<span style="font-family: monospace, Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 13px; line-height: 14px; white-space: pre;">填充随机数据可以借助于dbms_random包，不想覆盖已有的表，所以新创建一个表结构基本一样的：</span>&nbsp;<br />
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000FF; ">create</span>&nbsp;<span style="color: #0000FF; ">table</span>&nbsp;emp_new<br />
<span style="color: #0000FF; ">as</span><br />
<span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #0000FF; ">level</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;empno,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SYS.dbms_random.String(<span style="color: #FF0000; ">'</span><span style="color: #FF0000; ">u</span><span style="color: #FF0000; ">'</span>,&nbsp;SYS.dbms_random.value(<span style="color: #800000; font-weight: bold; ">3</span>,<span style="color: #800000; font-weight: bold; ">10</span>))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ename,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SYS.dbms_random.String(<span style="color: #FF0000; ">'</span><span style="color: #FF0000; ">u</span><span style="color: #FF0000; ">'</span>,&nbsp;SYS.dbms_random.value(<span style="color: #800000; font-weight: bold; ">3</span>,<span style="color: #800000; font-weight: bold; ">9</span>))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;job,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #FF00FF; ">round</span>(SYS.dbms_random.value(<span style="color: #800000; font-weight: bold; ">1000</span>,<span style="color: #800000; font-weight: bold; ">9999</span>))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mgr,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TO_DATE&nbsp;(&nbsp;&nbsp;&nbsp;<span style="color: #FF00FF; ">ROUND</span>&nbsp;(DBMS_RANDOM.VALUE&nbsp;(<span style="color: #800000; font-weight: bold; ">1</span>,&nbsp;<span style="color: #800000; font-weight: bold; ">28</span>))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080; ">||</span>&nbsp;<span style="color: #FF0000; ">'</span><span style="color: #FF0000; ">-</span><span style="color: #FF0000; ">'</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080; ">||</span>&nbsp;<span style="color: #FF00FF; ">ROUND</span>&nbsp;(DBMS_RANDOM.VALUE&nbsp;(<span style="color: #800000; font-weight: bold; ">1</span>,&nbsp;<span style="color: #800000; font-weight: bold; ">12</span>))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080; ">||</span>&nbsp;<span style="color: #FF0000; ">'</span><span style="color: #FF0000; ">-</span><span style="color: #FF0000; ">'</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080; ">||</span>&nbsp;<span style="color: #FF00FF; ">ROUND</span>&nbsp;(DBMS_RANDOM.VALUE&nbsp;(<span style="color: #800000; font-weight: bold; ">1980</span>,&nbsp;<span style="color: #800000; font-weight: bold; ">2012</span>)),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #FF0000; ">'</span><span style="color: #FF0000; ">DD-MM-YYYY</span><span style="color: #FF0000; ">'</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hiredate,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #FF00FF; ">round</span>(SYS.dbms_random.value(<span style="color: #800000; font-weight: bold; ">300</span>,<span style="color: #800000; font-weight: bold; ">9999</span>))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sal,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #FF00FF; ">round</span>(SYS.dbms_random.value(<span style="color: #800000; font-weight: bold; ">1</span>,<span style="color: #800000; font-weight: bold; ">6</span>))&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #800000; font-weight: bold; ">100</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;comm,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #FF00FF; ">round</span>(SYS.dbms_random.value(<span style="color: #800000; font-weight: bold; ">1</span>,<span style="color: #800000; font-weight: bold; ">4</span>))&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #800000; font-weight: bold; ">10</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deptno<br />
&nbsp;<span style="color: #0000FF; ">FROM</span>&nbsp;DUAL<br />
CONNECT&nbsp;<span style="color: #0000FF; ">BY</span>&nbsp;<span style="color: #0000FF; ">LEVEL</span>&nbsp;<span style="color: #808080; ">&lt;</span>&nbsp;<span style="color: #800000; font-weight: bold; ">1000000</span>;</div>
<div number3="" index2=""  alt2"="" style="color: #222222; font-family: monospace, Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 13px; line-height: 1.1em !important; outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important; white-space: pre !important;"><br />
</div>
<div number3="" index2=""  alt2"="" style="color: #222222; font-family: monospace, Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 13px; line-height: 1.1em !important; outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important; white-space: pre !important;"><span style="font-size: 14px; ">这里有一个局限，原本的emp表mgr列reference empno列。上面新创建的emp_new中失去了这个constraint. 这点可以从Oracle SQL Developer中看到。</span></div>
<div number3="" index2=""  alt2"="" style="color: #222222; font-family: monospace, Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 13px; line-height: 1.1em !important; outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important; white-space: pre !important;"><span style="font-size: 14px;">emp表：<br />
<a href="http://www.tuzhan.com/vcycyv/photo/a889617f745d7ba5.html" target="_blank"><img src="http://f-1.tuzhan.com/adb507d00a2a/p-2/l/2013/03/31/09/7c29f2ddaa7d47f29535dbe8b892c36a.png" alt="with_constrant.png" border="0" /><br />
</a></span>
<span style="font-size: 14px; line-height: 14px;">emp_new表：</span>
<span style="font-size: 14px;"><br />
<a href="http://www.tuzhan.com/vcycyv/photo/ab92069654042edb.html" target="_blank"><img src="http://f-1.tuzhan.com/33bdd567b454/p-2/l/2013/03/31/09/485143eb02104036ba1f146fb0d99375.png" alt="no_constrant.png" border="0" /></a>
</span>
<div style="color: #000000; font-family: 微软雅黑; font-size: 14px; line-height: normal; text-align: -webkit-auto; white-space: normal;"><table border="0" cellpadding="0" cellspacing="0" style="width: 402px; color: #222222; font-family: monospace, Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 13px !important; outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; box-sizing: content-box !important; direction: ltr !important;"><tbody style="outline-width: 0px !important; outline-style: initial !important; outline-color: initial !important; border-top-width: 0px !important; border-right-width: 0px !important; border-bottom-width: 0px !important; border-left-width: 0px !important; border-style: initial !important; border-color: initial !important; border-image: initial !important; vertical-align: baseline !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; overflow-x: visible !important; overflow-y: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important; "><tr style="outline-width: 0px !important; outline-style: initial !important; outline-color: initial !important; border-top-width: 0px !important; border-right-width: 0px !important; border-bottom-width: 0px !important; border-left-width: 0px !important; border-style: initial !important; border-color: initial !important; border-image: initial !important; vertical-align: baseline !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; overflow-x: visible !important; overflow-y: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important; "><td class="code" style="word-break: break-all; outline-width: 0px !important; outline-style: initial !important; outline-color: initial !important; border-top-width: 0px !important; border-right-width: 0px !important; border-bottom-width: 0px !important; border-left-width: 0px !important; border-style: initial !important; border-color: initial !important; border-image: initial !important; vertical-align: baseline !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow-x: visible !important; overflow-y: visible !important; position: static !important; right: auto !important; top: auto !important; width: 402px; box-sizing: content-box !important; direction: ltr !important; "><div class="container" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: relative !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; white-space: pre; ">下面就开始测试sql了：</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; white-space: pre; ">1. 先比较一下加primary key前后的结果：</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; white-space: pre; ">select * from emp_new where empno=1;</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; white-space: pre; ">加primary key constraint之前运行0.023秒。 加了primary key constraint之后0.001秒。加</span><span style="font-size: 14px; white-space: pre; line-height: 1.1em; ">primary key constraint在100万条数据上大约花费4秒钟。</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; ">2. where子句 vs. having子句</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; ">select deptno, avg(sal)  from emp_new group by deptno having deptno != 10 and deptno != 20;</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; ">0.24秒</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; ">select deptno, avg(sal)  from emp_new where deptno != 10 and deptno != 20 group by deptno ;</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; ">0.16秒</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; ">所以having中的条件一般用于对一些集合函数的比较，如count()等，除此之外，一般条件应该写在where子句中。</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; "><br /></span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; ">3. 减少对表的查询</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; ">update emp_new set sal=(select max(sal) from emp_new), comm=(select max(comm) from emp_new) where empno=1237;</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; ">0.11秒左右</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; ">update emp_new set (sal, comm) =(select max(sal), max(comm) from emp_new) where empno=1224;</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; ">0.07秒到0.08秒之间</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; "><br /></span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; ">注意：以上三个测试都只fetch前50条数据。</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; "><br /></span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; ">4. 传说中用exists替代in通常可提高查询效率， not exists 也比not in 快。<br />先生成dept_new表：<br /></span><div style="background-color: #eeeeee; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all;"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">create</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">table</span><span style="color: #000000; ">&nbsp;dept_new<br /></span><span style="color: #0000FF; ">as</span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">select</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">level</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deptno,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SYS.dbms_random.String(</span><span style="color: #FF0000; ">'</span><span style="color: #FF0000; ">u</span><span style="color: #FF0000; ">'</span><span style="color: #000000; ">,&nbsp;SYS.dbms_random.value(</span><span style="color: #800000; font-weight: bold; ">3</span><span style="color: #000000; ">,</span><span style="color: #800000; font-weight: bold; ">10</span><span style="color: #000000; ">))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dname,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SYS.dbms_random.String(</span><span style="color: #FF0000; ">'</span><span style="color: #FF0000; ">u</span><span style="color: #FF0000; ">'</span><span style="color: #000000; ">,&nbsp;SYS.dbms_random.value(</span><span style="color: #800000; font-weight: bold; ">3</span><span style="color: #000000; ">,</span><span style="color: #800000; font-weight: bold; ">9</span><span style="color: #000000; ">))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loc<br /></span><span style="color: #0000FF; ">FROM</span><span style="color: #000000; ">&nbsp;DUAL<br />CONNECT&nbsp;</span><span style="color: #0000FF; ">BY</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">LEVEL</span><span style="color: #000000; ">&nbsp;</span><span style="color: #808080; ">&lt;</span><span style="color: #000000; ">&nbsp;</span><span style="color: #800000; font-weight: bold; ">10000</span><span style="color: #000000; ">;</span></div></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; ">实际测试中，无论是执行计划还是实际测试的速度都是基本一致的。</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; ">第一组</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; ">select * from emp_new e where e.empno &gt; 986000 and e.deptno in (select d.deptno from dept_new d where d.loc='AYDN')
select * from emp_new e where empno &gt; 986000 and exists (select * from dept_new d where d.deptno = e.deptno and d.loc='AYDN')
第二组
select e.empno from emp_new e where e.empno &gt; 996000 and not exists (select 1 from dept_new d where d.deptno = e.deptno and loc like 'A%')
select e.empno from emp_new e where e.empno &gt; 996000 and e.deptno not in (select d.deptno from dept_new d where loc like 'A%')</span></div><div class="line number3 index2 alt2" style="outline: 0px !important; border: 0px !important; vertical-align: baseline !important; margin: 0px !important; padding: 2px 1em 2px 0em !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; direction: ltr !important;"><span style="font-size: 14px; line-height: 15px; white-space: pre; "><br /></span></div></div></td></tr></tbody></table></div><div style="color: #000000; font-family: 微软雅黑; font-size: 14px; line-height: normal; text-align: -webkit-auto; white-space: normal;"></div><div style="color: #000000; font-family: 微软雅黑; font-size: 14px; line-height: normal; text-align: -webkit-auto; white-space: normal;">这篇文字主要参考两篇文章：</div><div style="color: #000000; font-family: 微软雅黑; font-size: 14px; line-height: normal; text-align: -webkit-auto; white-space: normal;">Generating Random Data in<a href="file:///C:/Users/Chuyang/AppData/Local/youdao/ynote/editor/web/Oracle%EF%BC%9Ahttp://viralpatel.net/blogs/generating-random-data-in-oracle/" style="cursor: pointer; ">&nbsp;Oracle：http://viralpatel.net/blogs/generating-random-data-in-oracle/</a></div><div style="color: #000000; font-family: 微软雅黑; font-size: 14px; line-height: normal; text-align: -webkit-auto; white-space: normal;">Oracle sql &nbsp;性能优化调整：<a href="file:///C:/Users/Chuyang/AppData/Local/youdao/ynote/editor/web/&amp;nbsp;http://wenku.baidu.com/view/571cddd4195f312b3169a507" style="cursor: pointer; ">&nbsp;http://wenku.baidu.com/view/571cddd4195f312b3169a507</a>&nbsp;</div>
<span style="font-size: 14px;"><br />
<br />
<br />
<br />
</span></div>
</div><img src ="http://www.blogjava.net/vcycyv/aggbug/397206.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2013-03-31 21:49 <a href="http://www.blogjava.net/vcycyv/archive/2013/03/31/397206.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>自录performance tuning, jvm, visualvm视频(更新：已发送视频至优酷)</title><link>http://www.blogjava.net/vcycyv/archive/2012/08/24/386164.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Fri, 24 Aug 2012 00:30:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2012/08/24/386164.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/386164.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2012/08/24/386164.html#Feedback</comments><slash:comments>12</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/386164.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/386164.html</trackback:ping><description><![CDATA[<p>visualvm是Sun JDK自带的Java profiling的工具。自己录了一个视频,35分钟，简单的介绍了performance tuning的lifecycle, jvm的一点点理论，以及visualvm的操作：定位死锁，查看out of memory, 定位运行缓慢的method.</p> <p>&nbsp;</p> <p>视频文件在：<a title="https://docs.google.com/open?id=0B22ayLe4teeYQm56QnhaWkUxM2s" href="https://docs.google.com/open?id=0B22ayLe4teeYQm56QnhaWkUxM2s">https://docs.google.com/open?id=0B22ayLe4teeYQm56QnhaWkUxM2s</a></p> <p>ppt在：<a title="https://docs.google.com/open?id=0B22ayLe4teeYdEhsMkItTk16UDg" href="https://docs.google.com/open?id=0B22ayLe4teeYdEhsMkItTk16UDg">https://docs.google.com/open?id=0B22ayLe4teeYdEhsMkItTk16UDg<br /><br /></a>有人说google drive上不容易下载大文件，建议压缩一下，但是我不太会用视频压缩软件，压过之后效果不好了，倒是也能将就着看：<br /></p><div>https://docs.google.com/open?id=0B22ayLe4teeYY1pvNlVtSWdUOVU<br /><br />我的声音就像梦游一样：<a href="http://v.youku.com/v_show/id_XNDQ1Nzk0NDYw.html">http://v.youku.com/v_show/id_XNDQ1Nzk0NDYw.html</a><br />&nbsp;</div><p>&nbsp;</p><img src ="http://www.blogjava.net/vcycyv/aggbug/386164.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2012-08-24 08:30 <a href="http://www.blogjava.net/vcycyv/archive/2012/08/24/386164.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>总结Java的Generics</title><link>http://www.blogjava.net/vcycyv/archive/2012/08/20/385803.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Mon, 20 Aug 2012 00:13:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2012/08/20/385803.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/385803.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2012/08/20/385803.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/385803.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/385803.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 从JDK1.4到JDK5中间经历了两年半左右的时间，从JDK5到JDK6经历了两年左右的时间，从JDK6到JDK7经历了4年半多的时间。JDK5，6，7这三个版本，只有JDK5有横空出世的惊艳，一系列new feature明显改变了Java程序员日常的coding工作：Generics, Annotation, Autoboxing, for each statement.... 其中Java G...&nbsp;&nbsp;<a href='http://www.blogjava.net/vcycyv/archive/2012/08/20/385803.html'>阅读全文</a><img src ="http://www.blogjava.net/vcycyv/aggbug/385803.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2012-08-20 08:13 <a href="http://www.blogjava.net/vcycyv/archive/2012/08/20/385803.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>发现thinking in java 4th edition的一个错误</title><link>http://www.blogjava.net/vcycyv/archive/2012/07/30/384290.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Mon, 30 Jul 2012 02:30:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2012/07/30/384290.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/384290.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2012/07/30/384290.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/384290.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/384290.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: thinking in java看了差不多有5，6遍，但是之前有些章节看得不仔细。最近有感于自己技术的不扎实，开始重看thinking in java, 发现了书中的一个错误，在原书的491页。 &nbsp;     1: //: generics/Wildcards.java   2: // Exploring the meaning of wildcards.   3:&nbsp;    4:...&nbsp;&nbsp;<a href='http://www.blogjava.net/vcycyv/archive/2012/07/30/384290.html'>阅读全文</a><img src ="http://www.blogjava.net/vcycyv/aggbug/384290.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2012-07-30 10:30 <a href="http://www.blogjava.net/vcycyv/archive/2012/07/30/384290.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>remove element without causing ConcurrentModificationException &amp; i18n number input solution &amp; 免费录屏软件</title><link>http://www.blogjava.net/vcycyv/archive/2012/06/23/381353.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Sat, 23 Jun 2012 08:28:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2012/06/23/381353.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/381353.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2012/06/23/381353.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/381353.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/381353.html</trackback:ping><description><![CDATA[1.&nbsp;remove element without causing ConcurrentModificationException<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->List&lt;String&gt;&nbsp;names&nbsp;=&nbsp;<img src="http://www.blogjava.net/Images/dot.gif"  alt="" />.<br />Iterator&lt;String&gt;&nbsp;i&nbsp;=&nbsp;names.iterator();<br /><span style="color: #0000FF; ">while</span>&nbsp;(i.hasNext())&nbsp;{<br />&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;Do&nbsp;something</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;i.remove();<br />}</div><br /><div><span style="border-collapse: separate; font-family: Tahoma; line-height: normal; border-spacing: 0px; font-size: medium; ">Note that you must call i.next() before you can call i.remove()<br /><br />2. 法语，德语中数字小数点用,表示，千位符是"." 如果做支持I18n的UI， 需要显示数字并接受他们输入的数字,可以这样做：<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->&nbsp; &nbsp;&nbsp;<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">static</span>&nbsp;<span style="color: #0000FF; ">void</span>&nbsp;main(String[]&nbsp;args)&nbsp;<span style="color: #0000FF; ">throws</span>&nbsp;ParseException{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;numString&nbsp;=&nbsp;"0.05";<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;frenchNumString&nbsp;=&nbsp;"0,05";<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NumberFormat&nbsp;nf&nbsp;=&nbsp;NumberFormat.getNumberInstance();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("French&nbsp;format&nbsp;of&nbsp;raw&nbsp;string:&nbsp;"&nbsp;+&nbsp;nf.format(Double.parseDouble(numString)));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Double&nbsp;n&nbsp;=&nbsp;(Double)&nbsp;nf.parse(frenchNumString);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Parse&nbsp;french&nbsp;number&nbsp;and&nbsp;calculate:&nbsp;"&nbsp;+&nbsp;(n&nbsp;+&nbsp;1));<br />&nbsp;&nbsp;&nbsp;&nbsp;}</div><br />3. 免费的录屏软件：HyperCam&nbsp;<br /><br /></span></div><img src ="http://www.blogjava.net/vcycyv/aggbug/381353.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2012-06-23 16:28 <a href="http://www.blogjava.net/vcycyv/archive/2012/06/23/381353.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>微改进版DAV Explorer和推荐几个好看的视频</title><link>http://www.blogjava.net/vcycyv/archive/2012/04/16/374702.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Mon, 16 Apr 2012 00:45:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2012/04/16/374702.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/374702.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2012/04/16/374702.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/374702.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/374702.html</trackback:ping><description><![CDATA[<p>Web Dav是一种可以在上面读写文件的web server. 支持加锁，解锁。还支持版本控制。树状结构，可以在上面存储键值对。</p> <p>管理Web Dav一般用Dav Explorer，它是开源的. 平时用Dav Explorer的时候有两点有些不爽。</p> <p>第一，它不能管理中文（非英文）目录，以至于所有子目录也不能通过这个工具访问</p> <p>第二，它不能保存密码，平时工作时常连不同机器，天天输入密码。</p> <p>最近改了一些它的代码，这两个问题都解决了。</p> <p>解决中文目录问题，主要是通过commons-httpclient.jar包里的URIUtil.encodePath，它能直接encode完整的http url, 而不像URLEncoder只能encode url的文件部分（http://以及前面的context path不能直接用URLEncoder来encode）</p> <p>解决保存密码的问题主要是通过Java的Preferences.</p> <p>改进后的Dav Explorer可以在我的网盘下载：<a href="http://115.com/file/c2k7519k#DAVExplorer-chuyang.zzip">http://115.com/file/c2k7519k#DAVExplorer-chuyang.zzip</a> 下载之后后缀名要改成.zip. jar包里包含源代码。</p> <p>&nbsp;</p> <p>最近看演讲视频练习英语听力，看到一些对自己很有触动的推荐一下，大家有啥推 荐的也说一下！ <br>1.&nbsp; TED演讲，脆弱的力量，这是关于如何做到全心全力（whole-hearted）生活的 演讲，当时反复看了三遍，带给自己很多思考。 <br><a href="http://www.en8848.com.cn/tingli/speech/ted/148318.html">http://www.en8848.com.cn/tingli/speech/ted/148318.html</a></p> <p><br>2. 苹果UI交互设计师的演讲，主题叫Inventing on Principle，期间的演示令人 叹为观止，他的思想也耐人寻味。 <br><a href="http://coolshell.cn/articles/6775.html">http://coolshell.cn/articles/6775.html</a></p> <p><br>3. 奥巴马就职演说，气场还真是强大。 <br><a href="http://v.ku6.com/show/2XDTp_25OOYOJC1u.html">http://v.ku6.com/show/2XDTp_25OOYOJC1u.html</a></p> <p><br>4. Yang Xu在这个bbs推荐过，Randy Pausch的最后一课，值得感受一下他的人生 态度，震撼。看到生日蛋糕那块儿的时候快哭了都。 <br>最喜欢的一段话：The brick walls are there to give us a chance to show how badly we want something. Because the brick walls are there to stop the people who don't want it badly enough. They are there to stop the other people. <br><a href="http://v.youku.com/v_show/id_XMjQ3MTUzNDI4.html">http://v.youku.com/v_show/id_XMjQ3MTUzNDI4.html</a></p> <p><br>5. 乔布斯斯坦福演讲，这个太出名了，不说了。 <br><a href="http://www.tudou.com/programs/view/dS0wNIaiWT4/">http://www.tudou.com/programs/view/dS0wNIaiWT4/</a></p> <p><br>发现TED是很好的视频演讲资源，TED指technology, entertainment, design，可 以在优酷搜“TED演讲集”，最近看了接近20个，很多演讲智慧而幽默。 </p><img src ="http://www.blogjava.net/vcycyv/aggbug/374702.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2012-04-16 08:45 <a href="http://www.blogjava.net/vcycyv/archive/2012/04/16/374702.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>假如我是CTO</title><link>http://www.blogjava.net/vcycyv/archive/2011/08/01/355469.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Mon, 01 Aug 2011 00:25:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2011/08/01/355469.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/355469.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2011/08/01/355469.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/355469.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/355469.html</trackback:ping><description><![CDATA[<p>这辈子怕是没机会当CTO了，只能假如一下了。CTO的主要职责是了解当前流行的技术并有前瞻性地设定技术战略；预先感知未来的技术发展方向，保持市场竞争中技术较为领先的地位，这种领先应该是可以转化为商业价值的。CTO可以很大程度上左右技术型公司的命运，如果作出正确的技术决策，就能在竞争中牵着对手鼻子走，成为游戏规则的制定者；一旦做错决策，可能接下来几年公司在技术上的投入都要化为泡影，浪费大量人力财力。</p> <p>商业公司追求最大化商业利润，CTO应该能够感知公司所处的业务市场的前景，如果整个市场处于萎缩状态，公司难保独善其身，所以CTO”最怕入错行"，CTO有责任勾画公司未来的技术蓝图，并且他还应该懂市场. 我假想IBM当初从软件转向服务，背后的推手应该是CTO, 即使很多操作是由CEO来做，有前瞻性地提出转型的人应该是CTO，因为他了解技术前景，或者说，他了解某些方面的技术是否在未来有市场。</p> <p>整个软件行业都在流行收购，但最初坚定走收购道路的先驱并不能得到广泛的理解和认可。多年以前，oracle开始展现收购战略的时候，他的对手SAP有些揶揄意味地，针锋相对提出“organic grow”, 依靠自身成长。结果“成长”了几年发现还是对手依靠收购扩张得更快，才开始跟着花钱收购，从这个角度说，SAP在战略上已经先输一招儿了。收购战略或许是Larry Ellison的灵光一现，具体扩充哪些方面的市场，选择哪些技术领先的对手进行收购，收购之后的整合规划，这些应该都属于CTO的职责范围。不过就Oracle这个例子，Larry的技术嗅觉可能已经达到猎狗水准了。</p> <p>随着开源越来越盛行，技术也越来越倾向于开放。在开源盛行之前，每个公司，无论大小，都有自己的技术框架。发布的各个产品都架构在这个框架之上。后来出现了不同方面的开源技术框架，比如spring, hibernate, 各种MVC框架，CTO应该直接领导团队对潜在可能吸收的新技术进行评估。这些开源的技术往往更优秀，因为新技术往往吸收了已有技术的精华并加以改进，另外，开源技术有广泛的用户群验证并帮助优化。但同时，技术优秀不表示成熟，<strong>公司里需要有专门的团队评估哪种技术可以在什么时候可以替代公司已有的技术。</strong> 这种选择应该倾向于保守，宁可略微慢一点，也不能冒进做错技术选型。比如flex和html5在三年之前比较，不太容易看出哪种技术有更好的前景。我个人觉得现在比较这两种技术的话，html5的优势明显更大一些了。开放的技术吸引更多的技术狂热者推动并完善它，html5似乎越来越好用了。技术选型的决策需要审慎地慢慢做，但一旦决策完成，就要雷厉风行执行决策。如果公司已经有十个产品基于老框架开发，那么不急于用新框架替代老框架重新修改老产品，毕竟<strong>商业产品最终目的是为了卖钱，而不是追求完美技术。</strong>更替已有产品框架无谓地引入风险。但是对于新产品，就要坚定不移地使用经过评估的新技术，如果公司范围里，有不同的新老技术框架，维护起来的确有格外的成本，但死守老技术肯定是死路一条。<strong>CTO做技术决定的时候，需要把各个方面的考虑写成文档，让一线技术人员了解技术决策的背景</strong>。上面用flex和html5举例，并认为CTO应该让一线技术人员了解技术决策背景，原因就在于我所在的公司一直在推行flex，而我一直疑惑这个决定是怎么做出来的，背后的依据是什么。</p> <p>搞创新性的技术，申请专利当然好，这个没啥可说的。 没有一直追踪新技术，况且最近几个月在学数学，统计，sas之类跟java没啥关系的技术. 只是乱谈一下对技术的看法，java方面个人看好的技术包括，</p> <p>Spring —— 它的对手是JBoss seam, seam当然也值得关注。在之前的博客讨论Domain Driven Design的时候说过，并不喜欢Spring所有方面，尤其它跟DDD的思路有相悖的地方。但Spring已经成为一个流行平台，它太流行了，所以各路专家好手都会在它的基础上帮助改进Spring. 连DDD理论的创建者Eric Evans都说要把DDD的思想带入Spring中，这就是龙头的优势。公司跟进Spring的技术风险小。Spring Roo是个格外值得关注的项目。</p> <p>AspectJ —— AspectJ可以支撑技术框架，但是不适合大量用在应用层面的程序里。</p> <p>OSGI —— 其实不太了解OSGI，但他看起来比较容易成为标准</p> <p>Groovy —— 很多人在讨论谁可以替代java， ruby, scala, phython还是groovy。我个人不觉得什么能替代java。横向比较这几个技术，比较看好groovy, 因为它是java的亲戚， 能自然地复用java成熟的产品应用，所以它天生就有“成熟”的因子。人们往往看重快速开发的特性，而我觉得，支持底层开发和易维护性才更适合企业级开发应用。</p><img src ="http://www.blogjava.net/vcycyv/aggbug/355469.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2011-08-01 08:25 <a href="http://www.blogjava.net/vcycyv/archive/2011/08/01/355469.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java serializable（转）</title><link>http://www.blogjava.net/vcycyv/archive/2011/02/23/345019.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Wed, 23 Feb 2011 15:15:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2011/02/23/345019.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/345019.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2011/02/23/345019.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/345019.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/345019.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 这篇包含三篇文章的转载和摘抄一段effective java的code 第一篇转载  大家都知道Serializable是一个mark interface,告诉JVM这个对象可以被转换成二进制流来传输.但是Serializable与Externalizable的转换二进制流的过程是不一样的.Serializable 在我们实现这个接口的时候,我们可以使用4个私有方法来控制序列化的过程:&nbsp;...&nbsp;&nbsp;<a href='http://www.blogjava.net/vcycyv/archive/2011/02/23/345019.html'>阅读全文</a><img src ="http://www.blogjava.net/vcycyv/aggbug/345019.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2011-02-23 23:15 <a href="http://www.blogjava.net/vcycyv/archive/2011/02/23/345019.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>总结java的exception</title><link>http://www.blogjava.net/vcycyv/archive/2011/02/22/344791.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Mon, 21 Feb 2011 16:12:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2011/02/22/344791.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/344791.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2011/02/22/344791.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/344791.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/344791.html</trackback:ping><description><![CDATA[<p>这篇和前面的 <a href="http://www.blogjava.net/vcycyv/archive/2011/02/20/344716.html"><font color="#0000ff">总结java的interface和abstract class</font></a> 一样。跳过最基础语法不聊，只说一些比较tricky的东西和一些好的practice.
<p>&nbsp;
<p><strong>语法：</strong>
<p>Exception继承自Throwable. Throwable还有一个儿子是Error,但是一般用不到。不过有些二百五面试官喜欢问回字有几种写法，所以还是要知道有Error这回事的。Exception分为checked和unchecked两种。
<p>&nbsp;
<p>java的checked exception一直是个很有争议的东西。Thinking in Java的作者Bruce, C#的设计者和Martin Fowler都对checked exception持质疑态度。 一个典型的反对理由是："We felt it was unrealistic to require the programmer to provide handlers in situations where no meaningful action can be taken." 这话太实惠了。写方法给caller调用，怎么能magically知道调用这个方法的<strong>所有</strong>caller，能够处理特定的异常呢？</p>
<p>&nbsp;</p>
<p>关于Exception有个比较基本的语法是，子类override的方法如果声明抛出exception, 只能抛出父方法声明的exception，或者那个exception的子类。需要注意的是，&#8220;回&#8221;字还有一种写法，对于constructor来说没有这个限制，子类可以抛出任意exception。父类构造函数声明的exception,子类也必须声明，而且子类的构造函数不能捕捉父类声明的exception. 这个想想也容易理解，父类构造出错了，儿子居然能处理还把自己生出来了，没老子哪来的儿子？当然你可以抬杠说老子戴绿帽子的情况。
<p>&nbsp;
<p><strong>实践：</strong>
<p>1. 尽量不要在构造函数里做复杂的操作，尽量不要让constructor抛出exception。如果在构造函数里抛出exception,需要用nested try block. 如下：</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Cleanup { </pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>   <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) { </pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     <span style="color: #0000ff">try</span> { </pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>       InputFile in = <span style="color: #0000ff">new</span> InputFile(<span style="color: #006080">"Cleanup.java"</span>); </pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>       <span style="color: #0000ff">try</span> { </pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>         String s; </pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>         <span style="color: #0000ff">int</span> i = 1; </pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>         <span style="color: #0000ff">while</span>((s = in.getLine()) != null) </pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>           ; <span style="color: #008000">// Perform line-by-line processing here... </span></pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>       } <span style="color: #0000ff">catch</span>(Exception e) { </pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>         System.out.println(<span style="color: #006080">"Caught Exception in main"</span>); </pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>         e.printStackTrace(System.out); </pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>       } <span style="color: #0000ff">finally</span> { </pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>         in.dispose(); </pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>       } </pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>     } <span style="color: #0000ff">catch</span>(Exception e) { </pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>       System.out.println(<span style="color: #006080">"InputFile construction failed"</span>); </pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>     } </pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>   } </pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span> } </pre>
<!--CRLF--></div>
</div>
<p>而不是用finally来做清理工作。</p>
<p>&nbsp;</p>
<p>2. exception的一个基本使用原则是，exception不是设计用来控制程序flow的。 这是很简单的道理，还是引用effective java的一个例子吧</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #008000">// Horrible abuse of exceptions. Don't ever do this!</span></pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #0000ff">try</span> {</pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #0000ff">int</span> i = 0;</pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #0000ff">while</span>(true)</pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> range[i++].climb();</pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> } <span style="color: #0000ff">catch</span>(ArrayIndexOutOfBoundsException e) {</pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> }</pre>
<!--CRLF--></div>
</div>
<p>我真正要说明的是，上面说的原则很对，但是走到极端就不对了。有的人为了 不用exception控制程序flow, 就写一大堆的if&#8230;else语句试图考虑各种情况，正好前不久有同事说了个笑话，我觉得可以辅助解释这个问题。</p>
<p>=============================</p>
<p>某日,老师在课堂上想考考学生们的智商,就问一个男孩&#8220;树上有十只鸟,开枪打死一只,还剩几只?&#8221;
<p>男孩反问&#8220;是无声手枪，还是其他没有声音的枪么?&#8221;
<p>&#8220;不是。&#8221;
<p>&#8220;枪声有多大?&#8221;
<p>&#8220;80~100分贝。&#8221;
<p>&#8220;那就是说会震的耳朵疼?&#8221;
<p>&#8220;是。&#8221;
<p>&#8220;在这个城市里打鸟犯不犯法?&#8221;
<p>&#8220;不犯。&#8221;
<p>&#8220;您确定那只鸟真的被打死啦?&#8221;
<p>&#8220;确定.&#8221;老师已经不耐烦了,&#8221;拜托,你告诉我还剩几只就行了,OK?&#8221;
<p>&#8220;OK.鸟里有没有聋子?&#8221;
<p>&#8220;没有。&#8221;
<p>&#8220;有没有鸟智力有问题，呆傻到听到枪响不知道飞的？&#8221;
<p>&#8220;没有，智商都在200以上！&#8221;
<p>&#8220;有没有关在笼子里的?&#8221;
<p>&#8220;没有。&#8221;
<p>。。。
<p>==============================
<p>后面还有一堆&#8220;例外&#8221;情况。我们写程序总不能真写成
<p>if(鸟是聋子)
<p>&#8230;
<p>else if(鸟是傻子)
<p>&#8230;
<p>else if(鸟是瘸子)
<p>&#8230;
<p>原则应该是，如果一些情况确实是 &#8220;例外情况&#8221;，就用exception处理吧。不要很勤奋地写一堆defensive的判断。我们不会有故事里的小男孩儿思维那么滴水不漏的。别把Java程序退回c语言了。另一个例子是FileNotFoundException, java I/O没有让你每次用文件都提前调用exists()检查一下，我想原因不光是 检查文件的那一毫秒文件存在，run到下一步的时候，下一毫秒文件消失了，Sun没觉得你人品那么坏吧。理念仍然是，如果你觉得文件肯定存在，你就直接用吧，一旦不存在，你再另外当成异常情况处理。不要让一堆if&#8230;else弄脏了程序。
<p>&nbsp;
<p>3. exception有个典型用法是在方法体中，进行参数合法性校验
<p>&nbsp;</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">public</span> BigInteger mod(BigInteger m) {</pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #0000ff">if</span> (m.signum() &lt;= 0)</pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ArithmeticException(<span style="color: #006080">"Modulus &lt;= 0: "</span> + m);</pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> ... <span style="color: #008000">// Do the computation</span></pre>
<!--CRLF-->
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> }</pre>
<!--CRLF--></div>
</div>
<p>也有很多人用assert语句判断， 比如 Assert.notNull(object)。手动抛exception可以抛特定的类型，assert语句更方便。可以根据实际情况取舍。</p>
<p>&nbsp;</p>
<p>4. 既然上面说了checked exception本身是java设计不太合理的地方。我倾向于说，应该<strong>及时</strong>把checked exception translate成unchecked. 我知道exception的处理原则有一条是，如果你不知道怎么处理它，就不要捕捉它。 对于checked exception来说，它总force你去处理，太讨厌了。如果caller不知道怎么处理，留着给更上层的程序处理&#8230;底层的程序都不会处理，一般来说上层的程序就更不知道该怎么处理了，那还不如在尽量底层的调用中，要么处理它（这种情况很少，打log之类的算不上&#8220;处理&#8221;），要么就转成RuntimeException抛上来, 消灭掉checked exception带来的burden. 注意：translate exception的过程中，不要扔了原来的exception, 而要把它放在exception constructor的argument里, new RuntimeException(e)。这是很基本的东西，不多说。</p>
<p>&nbsp;</p>
<p>5. 不要吞exception. 这个太基础，不多说。</p>
<p>&nbsp;</p>
<p>6. 见到exception要把它记log里，而不是简单print stack一下，log4j的api有可以接受Throwable作为参数的。 </p>
<p>&nbsp;</p>
<p>7. 每层抛出来的exception要对当前这一层有意义，比如persistence层出问题，UI上你告诉客户hibernate的session关闭了，不能继续load数据了，客户还以为你的程序怎么跟狗熊一样还会冬眠的。即使是UI层以下，底层exception,比如sql exception也不要爬到domain层里处理</p>
<p>&nbsp;</p>
<p>8. apache commons的lang包里有ExceptionUtils类，玩儿exception最好把这个工具揣口袋里。<br />
<br />
9.exception是设计的一部分, 但它不同于API的设计。通常我们设计API的时候，不会设计一个函数destroyBaghdad()，通常我们会写destroyCity(Baghdad)。这样做的目的是为了重用。换句话说，<strong>你设计API的时候，总是装作忘了use case（caller）, </strong>而去写适合复用的API, 尽管上面的例子use case就是destroy Baghdad, 你还是要写更general的destroyCity函数，然后把城市的名字作为参数传进去.但exception的设计不应该用同样的思路做，因为你很难料想到复用的情况下，你声明的exception是不是总能在任何情况下都得到妥善的处理。举个例子，这是我临时想的例子，形象但是科技太超前了。我们写一个print()程序给打印机A，print()的时候没有纸可以抛一个checked NoPaperException，这时候exceptino的处理程序可以自动加载纸（目前这么高级的功能正在贝尔实验室研发呢）。也许有新型的打印机B总是先加纸，后打印，那么永远也不存在NoPaperException。如果打印机是老式打印机C，不会自动加载纸，见到NoPaterException也无计可施，没法处理。checked exception的哲学是，强制让caller处理它。从上面的例子看，只有A打印机<strong>需要并能够</strong>处理NoPaperException。 B打印机不需要处理exception。C打印机没能力处理exception. 所以，|&#8220;需要并能够&#8221;处理是个太严格的限制，一般情况下不应该用checked exception. 我们可以让print声明抛出unchecked exception. 提醒caller可以处理它，但是对于不应该处理它的caller也不强制去处理它。</p>
<img src ="http://www.blogjava.net/vcycyv/aggbug/344791.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2011-02-22 00:12 <a href="http://www.blogjava.net/vcycyv/archive/2011/02/22/344791.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>总结java的interface和abstract class</title><link>http://www.blogjava.net/vcycyv/archive/2011/02/20/344716.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Sun, 20 Feb 2011 14:06:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2011/02/20/344716.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/344716.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2011/02/20/344716.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/344716.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/344716.html</trackback:ping><description><![CDATA[<p>先说说interface和abstract method语法中需要注意的地方。</p> <p>Interface:</p> <p>1. An interface can contain fields, but these are implicitly static and final.</p> <p>2. You can choose to explicitly declare the methods in an interface as public, but they are public even if you don’t say it.</p> <p>3. Interface cannot define static method</p> <p>&nbsp;</p> <p>Abstract:</p> <p>1. 一个类中如果所有的方法都有实现，我们仍然可以定义这个类为abstract class</p> <p>2. abstract和static不能放在一起定义方法。</p> <p>&nbsp;</p> <p>Interface和Abstract class的实践</p> <p>1. interface适合定义mixins（不知道mixin怎么翻译，它指窄接口，只定义specific contract）. </p> <p>java不能多重继承。如果想达到多重继承的效果，需要借助“多重实现”interface. interface的一个典型用法是定义小接口。比如Comparable。这样它的实现成本比较小，一个class比较容易mixin多个interface。</p> <p>2. 如果interface不是mixin, 而是大一点的接口。</p> <p><a href="http://cid-d8b11f9bf86fecfa.office.live.com/self.aspx/.Public/books/Effective.Java.2nd.Edition.May.2008.pdf">effective java, 2nd edition</a>有精彩的阐述，对于大接口，我们往往使用skeletal implementation class. 举个例子：</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #008000">// Concrete implementation built atop skeletal implementation</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #0000ff">static</span> List&lt;Integer&gt; intArrayAsList(<span style="color: #0000ff">final</span> <span style="color: #0000ff">int</span>[] a) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #0000ff">if</span> (a == null)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> NullPointerException();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> AbstractList&lt;Integer&gt;() {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> <span style="color: #0000ff">public</span> Integer get(<span style="color: #0000ff">int</span> i) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> <span style="color: #0000ff">return</span> a[i];  <span style="color: #008000">// Autoboxing (Item 5)</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span> @Override <span style="color: #0000ff">public</span> Integer set(<span style="color: #0000ff">int</span> i, Integer val) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span> <span style="color: #0000ff">int</span> oldVal = a[i];</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span> a[i] = val;     <span style="color: #008000">// Auto-unboxing</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span> <span style="color: #0000ff">return</span> oldVal;  <span style="color: #008000">// Autoboxing</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> size() {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span> <span style="color: #0000ff">return</span> a.length;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span> };</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span> }</pre><!--CRLF--></div></div>
<p>new AbstractList&lt;Integer&gt;就是在应用Skeletal implementation. 有两个好处：</p>
<p>a) 它使实现接口更方便了</p>
<p>b) If, in a subsequent release, you want to add a new method to an abstract class, you can always add a concrete method containing a reasonable default implementation. All existing implementations of the abstract class will then provide the new method. This does not work for interfaces.</p>
<p>跟interface相关的还有一个话题是wrapper class，也很精彩，它是把继承转成合成的方式，应用了decorater模式的思想. 在书里的第16章介绍。</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #008000">// Wrapper class - uses composition in place of inheritance</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> InstrumentedSet&lt;E&gt; <span style="color: #0000ff">extends</span> ForwardingSet&lt;E&gt; {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span> addCount = 0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>     <span style="color: #0000ff">public</span> InstrumentedSet(Set&lt;E&gt; s) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>         <span style="color: #0000ff">super</span>(s);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>     @Override <span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> add(E e) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>         addCount++;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>         <span style="color: #0000ff">return</span> <span style="color: #0000ff">super</span>.add(e);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>     @Override <span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> addAll(Collection&lt;? <span style="color: #0000ff">extends</span> E&gt; c) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>         addCount += c.size();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>         <span style="color: #0000ff">return</span> <span style="color: #0000ff">super</span>.addAll(c);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> getAddCount() {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>         <span style="color: #0000ff">return</span> addCount;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span> <span style="color: #008000">// Reusable forwarding class</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ForwardingSet&lt;E&gt; <span style="color: #0000ff">implements</span> Set&lt;E&gt; {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">final</span> Set&lt;E&gt; s;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>     <span style="color: #0000ff">public</span> ForwardingSet(Set&lt;E&gt; s) { <span style="color: #0000ff">this</span>.s = s; }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> clear()               { s.clear();            }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> contains(Object o) { <span style="color: #0000ff">return</span> s.contains(o); }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> isEmpty()          { <span style="color: #0000ff">return</span> s.isEmpty();   }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> size()                 { <span style="color: #0000ff">return</span> s.size();      }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>     <span style="color: #0000ff">public</span> Iterator&lt;E&gt; iterator()     { <span style="color: #0000ff">return</span> s.iterator();  }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> add(E e)           { <span style="color: #0000ff">return</span> s.add(e);      }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> remove(Object o)   { <span style="color: #0000ff">return</span> s.remove(o);   }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> containsAll(Collection&lt;?&gt; c)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>                                    { <span style="color: #0000ff">return</span> s.containsAll(c); }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> addAll(Collection&lt;? <span style="color: #0000ff">extends</span> E&gt; c)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>                                    { <span style="color: #0000ff">return</span> s.addAll(c);      }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> removeAll(Collection&lt;?&gt; c)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>                                    { <span style="color: #0000ff">return</span> s.removeAll(c);   }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> retainAll(Collection&lt;?&gt; c)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>                                    { <span style="color: #0000ff">return</span> s.retainAll(c);   }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>     <span style="color: #0000ff">public</span> Object[] toArray()          { <span style="color: #0000ff">return</span> s.toArray();  }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>     <span style="color: #0000ff">public</span> &lt;T&gt; T[] toArray(T[] a)      { <span style="color: #0000ff">return</span> s.toArray(a); }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>     @Override <span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> equals(Object o)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>                                        { <span style="color: #0000ff">return</span> s.equals(o);  }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum42">  42:</span>     @Override <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> hashCode()    { <span style="color: #0000ff">return</span> s.hashCode(); }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum43">  43:</span>     @Override <span style="color: #0000ff">public</span> String toString() { <span style="color: #0000ff">return</span> s.toString(); }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum44">  44:</span> }</pre><!--CRLF--></div></div>
<p>使用它的客户端程序：</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> Set&lt;Date&gt; s = <span style="color: #0000ff">new</span> InstrumentedSet&lt;Date&gt;(<span style="color: #0000ff">new</span> TreeSet&lt;Date&gt;(cmp));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> Set&lt;E&gt; s2 = <span style="color: #0000ff">new</span> InstrumentedSet&lt;E&gt;(<span style="color: #0000ff">new</span> HashSet&lt;E&gt;(capacity));</pre><!--CRLF--></div></div>
<p>&nbsp;</p>
<p>3. 人人都说 面向接口编程，很多时候矫枉过正了， 搞得接口漫天飞。interface常常是重构出来的，而不总是设计出来的。程序分层中，越是靠近底层的程序，越倾向于需要接口，越靠近顶层的程序，对接口的需求倾向于越小。如果你明知道以后不太可能提供另一套实现，提前写个接口摆那里也没啥意义，尽管定义接口的成本很低。如果你担心一旦有一天确实提供了另一套实现，重构code也不会很麻烦。只要遵守了单一职责原则和迪米特法则。迪米特法则的一种表述方式是：talk only to your immediate friends。</p><img src ="http://www.blogjava.net/vcycyv/aggbug/344716.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2011-02-20 22:06 <a href="http://www.blogjava.net/vcycyv/archive/2011/02/20/344716.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>利用jetty实现超轻量级web开发</title><link>http://www.blogjava.net/vcycyv/archive/2011/02/13/344214.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Sun, 13 Feb 2011 14:40:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2011/02/13/344214.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/344214.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2011/02/13/344214.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/344214.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/344214.html</trackback:ping><description><![CDATA[<p>几年前，如果你不用EJB，很多人都用IDE+tomcat插件做web开发。 现在越来越多人使用jetty了。这不是简单地用Jetty替换tomcat，巧妙的地方在于我们可以直接用main函数在IDE里启动Jetty. 这样，你不需要配置IDE的server。不同于IDE+tomcat插件的方式需要两个project,web app和server project. 使用下面介绍的方式你只有一个java project就行. 开发环境更简单，debug也更方便，不需要remote debug的方式，jetty非常快，可以明显提高开发效率。</p> <p>参考了网上的一些文章，整理了一下。本文以Eclipse作为IDE。最后有可以运行的示例代码下载，非常简单。</p> <p>1. 建立Java project(不必是dynamic web project), 取个名字，比如jetty</p> <p>2. 建立jetty目录及其子目录如图所示。 所有文件都可以在最后的附件里找到。略作一点说明：webdefault.xml通常不需要你改。它是web.xml之外的一个配置文件，配置一些公共属性。web app的文件，jsp之类的都放在web目录下。web.xml和index.jsp都是示例性质最简单的。lib目录下需要放一些jetty的jar包，也一样在附件里有</p> <p>&nbsp;<a href="http://www.blogjava.net/images/blogjava_net/vcycyv/WindowsLiveWriter/jettyweb_13ECB/image_2.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.blogjava.net/images/blogjava_net/vcycyv/WindowsLiveWriter/jettyweb_13ECB/image_thumb.png" width="283" height="435"></a> </p> <p>3. 新建java文件如下：（注意这里我用的端口是8020，以免和其他jboss或tomcat冲突）</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">package</span> jetty;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #0000ff">import</span> org.mortbay.jetty.Connector;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #0000ff">import</span> org.mortbay.jetty.Server;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #0000ff">import</span> org.mortbay.jetty.nio.SelectChannelConnector;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> <span style="color: #0000ff">import</span> org.mortbay.jetty.webapp.WebAppContext;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> OneWebApp</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) <span style="color: #0000ff">throws</span> Exception</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>     {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>         String jetty_home = <span style="color: #006080">"jetty"</span>;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>         <span style="color: #0000ff">int</span> port = 8020;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>         Server server = <span style="color: #0000ff">new</span> Server();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>         </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>         Connector connector=<span style="color: #0000ff">new</span> SelectChannelConnector();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>         connector.setPort(port);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>         server.addConnector(connector);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>         </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>         WebAppContext webapp = <span style="color: #0000ff">new</span> WebAppContext(jetty_home + <span style="color: #006080">"/web"</span>, <span style="color: #006080">"/jetty"</span>);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>         webapp.setDefaultsDescriptor(jetty_home+<span style="color: #006080">"/etc/webdefault.xml"</span>);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>         </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>         server.setHandler(webapp);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>         </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>         server.start();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>         <span style="color: #008000">//server.join();</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span> }</pre><!--CRLF--></div></div>
<p>可以run这个java文件，然后在浏览器里访问<a href="http://localhost:8020/jetty">http://localhost:8020/jetty</a>看结果。</p>
<p>&nbsp;</p>
<p>整个eclipse工程可以从这里下载：<a title="http://cid-d8b11f9bf86fecfa.office.live.com/self.aspx/.Public/code/RunJettyInEclipse.rar" href="http://cid-d8b11f9bf86fecfa.office.live.com/self.aspx/.Public/code/RunJettyInEclipse.rar">http://cid-d8b11f9bf86fecfa.office.live.com/self.aspx/.Public/code/RunJettyInEclipse.rar</a></p> <img src ="http://www.blogjava.net/vcycyv/aggbug/344214.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2011-02-13 22:40 <a href="http://www.blogjava.net/vcycyv/archive/2011/02/13/344214.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>jvm 和 profiler</title><link>http://www.blogjava.net/vcycyv/archive/2011/01/30/343782.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Sun, 30 Jan 2011 10:12:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2011/01/30/343782.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/343782.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2011/01/30/343782.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/343782.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/343782.html</trackback:ping><description><![CDATA[<p><strong>年轻代：</strong></p> <p>&nbsp;&nbsp;&nbsp; 所有新生成的对象首先都是放在年轻代的。年轻代的目标就是尽可能快速的收集掉那些生命周期短的对象。年轻代分三个区。一个Eden区，两个 Survivor区(一般而言)。大部分对象在Eden区中生成。当Eden区满时，还存活的对象将被复制到Survivor区（两个中的一个），当这个 Survivor区满时，此区的存活对象将被复制到另外一个Survivor区，当这个Survivor去也满了的时候，从第一个Survivor区复制 过来的并且此时还存活的对象，将被复制&#8220;年老区(Tenured)&#8221;。需要注意，Survivor的两个区是对称的，没先后关系，所以同一个区中可能同时 存在从Eden复制过来 对象，和从前一个Survivor复制过来的对象，而复制到年老区的只有从第一个Survivor去过来的对象。而且，Survivor区总有一个是空 的。同时，根据程序需要，Survivor区是可以配置为多个的（多于两个），这样可以增加对象在年轻代中的存在时间，减少被放到年老代的可能。</p> <p><strong>年老代：</strong></p> <p>&nbsp;&nbsp;&nbsp; 在年轻代中经历了N次垃圾回收后仍然存活的对象，就会被放到年老代中。因此，可以认为年老代中存放的都是一些生命周期较长的对象。</p> <p><strong>持久代：</strong></p> <p>&nbsp;&nbsp;&nbsp; 用于存放静态文件，如今Java类、方法等。持久代对垃圾回收没有显著影响，但是有些应用可能动态生成或者调用一些class，例如Hibernate 等，在这种时候需要设置一个比较大的持久代空间来存放这些运行过程中新增的类。持久代大小通过-XX:MaxPermSize=&lt;N&gt;进行设 置。</p> <p><strong>什么情况下触发垃圾回收</strong></p> <p>由于对象进行了分代处理，因此垃圾回收区域、时间也不一样。GC有两种类型：<strong>Scavenge GC</strong> 和<strong>Full GC</strong> 。</p> <p><strong>Scavenge GC</strong></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 一般情况下，当新对象生成，并且在Eden申请空间失败时，就会触发Scavenge GC，对Eden区域进行GC，清除非存活对象，并且把尚且存活的对象移动到Survivor区。然后整理Survivor的两个区。这种方式的GC是对 年轻代的Eden区进行，不会影响到年老代。因为大部分对象都是从Eden区开始的，同时Eden区不会分配的很大，所以Eden区的GC会频繁进行。因 而，一般在这里需要使用速度快、效率高的算法，使Eden去能尽快空闲出来。</p> <p><strong>Full GC</strong></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 对整个堆进行整理，包括Young、Tenured和Perm。Full GC因为需要对整个对进行回收，所以比Scavenge GC要慢，因此应该尽可能减少Full GC的次数。在对JVM调优的过程中，很大一部分工作就是对于FullGC的调节。有如下原因可能导致Full GC：</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 年老代（Tenured）被写满</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 持久代（Perm）被写满</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.gc()被显示调用</p> <p>&nbsp;&nbsp;&nbsp;&nbsp; 上一次GC之后Heap的各域分配策略动态变化</p> <p><strong>串行处理器：</strong></p> <p>--适用情况：数据量比较小（100M左右）；单处理器下并且对响应时间无要求的应用。 <br>--缺点：只能用于小型应用</p> <p><strong>并行处理器：</strong></p> <p>--适用情况：&#8220;对吞吐量有高要求&#8221;，多CPU、对应用响应时间无要求的中、大型应用。举例：后台处理、科学计算。 <br>--缺点：垃圾收集过程中应用响应时间可能加长</p> <p><strong>并发处理器：</strong></p> <p>--适用情况：&#8220;对响应时间有高要求&#8221;，多CPU、对应用响应时间有较高要求的中、大型应用。举例：Web服务器/应用服务器、电信交换、集成开发环境。</p> <p>===================================</p> <p>今天使用jmap和jhat试图解决permgen out of memory的问题，找到了一个很好的文章：</p> <p><a href="http://blogs.sun.com/fkieviet/entry/how_to_fix_the_dreaded">http://blogs.sun.com/fkieviet/entry/how_to_fix_the_dreaded</a></p> <p>贴个开头，防止原帖丢了，连google的线索都没有</p> <p>How to fix the dreaded "java.lang.OutOfMemoryError: PermGen space" exception (classloader leaks) </p> <p>In the previous blog entry Classloader leaks: the dreaded "java.lang.OutOfMemoryError: PermGen space" exception&nbsp; I explained how this type of problem can originate in the application code that you deploy to an application server.&nbsp; In this post I'll explain how to track down the leak so that you can fix it. </p> <p>Profilers <br>Memory leak? Use a profiler. Right? Well... generally speaking the answer is yes, but classloader leaks are a bit special... </p>  <img src ="http://www.blogjava.net/vcycyv/aggbug/343782.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2011-01-30 18:12 <a href="http://www.blogjava.net/vcycyv/archive/2011/01/30/343782.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java weak reference（转）</title><link>http://www.blogjava.net/vcycyv/archive/2011/01/30/343781.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Sun, 30 Jan 2011 10:11:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2011/01/30/343781.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/343781.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2011/01/30/343781.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/343781.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/343781.html</trackback:ping><description><![CDATA[<p>转载自： <a title="http://www.java-tips.org/java-se-tips/java.util/using-weakhashmap-for-listener-lists.html" href="http://www.java-tips.org/java-se-tips/java.util/using-weakhashmap-for-listener-lists.html">http://www.java-tips.org/java-se-tips/java.util/using-weakhashmap-for-listener-lists.html</a></p> <p>In the Java platform, there are four types of references to objects. Direct references are the type you normally use, as in:</p> <p><code>Object obj = <strong>new </strong>Object()</code></p> <p>You can think of direct references as strong references that require no extra coding to create or access the object. The remaining three types of references are subclasses of the <code>Reference</code> class found in the <code>java.lang.ref</code> package. Soft references are provided by the <code>SoftReference</code> class, weak references by the <code>WeakReference</code> class, and phantom references by <code>PhantomReference</code>.</p> <p>&nbsp; <br>Soft references act like a data cache. When system memory is low, the garbage collector can arbitrarily free an object whose only reference is a soft reference. In other words, if there are no strong references to an object, that object is a candidate for release. The garbage collector is required to release any soft references before throwing an <code>OutOfMemoryException</code>. </p> <p><br>Weak references are weaker than soft references. If the only references to an object are weak references, the garbage collector can reclaim the memory used by an object at any time. There is no requirement for a low memory situation. Typically, memory used by the object is reclaimed in the next pass of the garbage collector. </p> <p><br>Phantom references relate to cleanup tasks. They offer a notification immediately before the garbage collector performs the finalization process and frees an object. Consider it a way to do cleanup tasks within an object. </p>   <img src ="http://www.blogjava.net/vcycyv/aggbug/343781.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2011-01-30 18:11 <a href="http://www.blogjava.net/vcycyv/archive/2011/01/30/343781.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java父线程子线程（转）</title><link>http://www.blogjava.net/vcycyv/archive/2011/01/30/343779.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Sun, 30 Jan 2011 10:06:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2011/01/30/343779.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/343779.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2011/01/30/343779.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/343779.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/343779.html</trackback:ping><description><![CDATA[<p>问：在父线程中New了一个子线程，想在停止父线程时也停止子线程，应该怎么做？</p> <p>答：</p> <p>从某种程度上讲，做不到。&nbsp;&nbsp; </p> <p>&nbsp; 不管是父线程还是子线程，这只不过是在运行时谁建了谁时用的，一旦所谓的字线程被启动，这两个线程是没有先后贵贱区分的。&nbsp;&nbsp; </p> <p>&nbsp; 任何线程是没有办法把另外一个线程终止的。&nbsp;&nbsp; </p> <p>&nbsp; 如果你一定想你说的那样是线的话，下面是唯一个可行方案。在"父线程"建立&#8220;子线程&#8221;时，把&#8220;父线程&#8221;的instance传过去，在&#8220;子线程&#8221;里，不停的check"父线程"是否还存活，如果否，停止。&nbsp;&nbsp; </p> <p>&nbsp; 相反的，如果"父线程"需要在"子线程"终了时结束，在"父线程"建立&#8220;子线程&#8221;时，留住&#8220;子线程&#8221;的instance然后keep&nbsp;&nbsp; checking&nbsp;&nbsp; whether&nbsp;&nbsp; it's&nbsp;&nbsp; still&nbsp;&nbsp; alive.</p> <p>================================================================================</p> <p>只有在所有非守护进程都停止的情况下，jvm才退出。main线程停止jvm也不一定退出：</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> TestMitiThread {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] rags) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>        System.out.println(Thread.currentThread().getName() + <span style="color: #006080">" 线程运行开始!"</span>);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>        <span style="color: #0000ff">new</span> MitiSay(<span style="color: #006080">"A"</span>).start();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>        <span style="color: #0000ff">new</span> MitiSay(<span style="color: #006080">"B"</span>).start();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>        System.out.println(Thread.currentThread().getName() + <span style="color: #006080">" 线程运行结束!"</span>);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>    }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span> class MitiSay <span style="color: #0000ff">extends</span> Thread {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>    <span style="color: #0000ff">public</span> MitiSay(String threadName) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>        <span style="color: #0000ff">super</span>(threadName);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>    }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> run() {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>        System.out.println(getName() + <span style="color: #006080">" 线程运行开始!"</span>);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>        <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; 10; i++) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>            System.out.println(i + <span style="color: #006080">" "</span> + getName());</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>            <span style="color: #0000ff">try</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>                sleep((<span style="color: #0000ff">int</span>) Math.random() * 10);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>            } <span style="color: #0000ff">catch</span> (InterruptedException e) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>                e.printStackTrace();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>            }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum42">  42:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum43">  43:</span>        }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum44">  44:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum45">  45:</span>        System.out.println(getName() + <span style="color: #006080">" 线程运行结束!"</span>);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum46">  46:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum47">  47:</span>    }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum48">  48:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum49">  49:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum50">  50:</span>&nbsp; </pre><!--CRLF--></div></div>
<p>运行结果：</p>
<p>main 线程运行开始!</p>
<p>main 线程运行结束!</p>
<p>A 线程运行开始!</p>
<p>0 A</p>
<p>1 A</p>
<p>B 线程运行开始!</p>
<p>2 A</p>
<p>0 B</p>
<p>3 A</p>
<p>4 A</p>
<p>1 B</p>
<p>5 A</p>
<p>6 A</p>
<p>7 A</p>
<p>8 A</p>
<p>9 A</p>
<p>A 线程运行结束!</p>
<p>2 B</p>
<p>3 B</p>
<p>4 B</p>
<p>5 B</p>
<p>6 B</p>
<p>7 B</p>
<p>8 B</p>
<p>9 B</p>
<p>B 线程运行结束!</p>   <img src ="http://www.blogjava.net/vcycyv/aggbug/343779.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2011-01-30 18:06 <a href="http://www.blogjava.net/vcycyv/archive/2011/01/30/343779.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>