??xml version="1.0" encoding="utf-8" standalone="yes"?>最近最新mv在线观看免费高清,婷婷一区二区三区,欧美精品久久久久久久多人混战http://www.aygfsteel.com/seosyw/archive/2015/01/01/422007.html董超z?/dc:creator>董超z?/author>Thu, 01 Jan 2015 14:34:00 GMThttp://www.aygfsteel.com/seosyw/archive/2015/01/01/422007.htmlhttp://www.aygfsteel.com/seosyw/comments/422007.htmlhttp://www.aygfsteel.com/seosyw/archive/2015/01/01/422007.html#Feedback0http://www.aygfsteel.com/seosyw/comments/commentRss/422007.htmlhttp://www.aygfsteel.com/seosyw/services/trackbacks/422007.html我们都知道,JavaQ?a style="color: rgb(11,59,140); font-size: 12px; text-decoration: underline" target="_blank">Java教程 Java培训 Q?/strong> ME 以往UCJ2MEQ?a style="color: rgb(11,59,140); font-size: 12px; text-decoration: underline" target="_blank">J2ME培训 Q?/strong>(Java PlatformQMicro Edition)Q是为机盒、移动电(sh)话和PDA之类嵌入式消费电(sh)子设备提供的Java语言q_Q包括虚拟机和一pd标准化的Java API。它和Java SE、Java EE一h成Java技术的三大版本Qƈ且同h通过JCP(Java Community Process)制订的?/p>

  J2MEq_中有几个重要的概念,例如内存、CLDC、MIDP{。初学J2ME往往对这些概느解不深,甚至出现偏差。本文的目的在于对J2ME中的相关重要概念q行阐述?/p>

  1. 内存

  我们一直在Q移动信息设备的内存非常,使用h应该加倍的珍惜Q但是我们却很少知道q些内存是如何分cȝQ下面将做详l的介绍。事实上MIDP讑֤的内存分ZU,Programme Memory、Heap、persistent Storage?/p>

  Programme Memory是移动信息设备分配给MIDlet suite的空_(d)因ؓ(f)MIDlet suite是以jar文gq行发布的,所以这个文件的大小可以认ؓ(f)是Programme Memory的大。一些厂商对Programme Memory的最大值是有限制的Q例如我的Nokia 6108的最大值是 64kQ超q的话将不能q行安装。减MIDlet suite的大非帔R要,一个便L(fng)Ҏ(gu)是使用h器对应用E序q行hQ这样可以减jar文g的大。在以后的文章中我会(x)谈到如何使用 Proguard?/p>

  Heap是应用程序在q行q程中存放所创徏的对象的存储I间Q本地变量和成员变量也是攑֜Heap上的QMIDP讑֤中提供的HeapI间大概在几十k到几百K?/p>

  Persistent Storage的空间是用来实现MIDP应用E序的本地数据持久性存储的Q在Record Management System从入门到_N中我做了详l的介绍q里׃再多说了?/p>

  2. Connected Limited Device Configuration

  CLDC包括一?strong>Java虚拟机和一pd的基c,J2ME的专家组l过对移动信息设备进行硬件抽象后得到他们的特点,然后设计q实C在移动信息设备上q行的java虚拟机,通常我们把它叫做KVM。在CLDC1.0q同时提供了由java.io、java.lang?javax.microediton.io、java.utill成的基cR在CLDC1.1里面d了java.lang.ref?/p>

  3. Mobile Infomation Device Profile

  MIDP是运行在CLDC基础之上的,在MIDP中定义了应用E序的生命周期、用户图形界面、数据管理系l等子集Q从而构v了J2MEq_。通常Q?strong>J2MEq_׃个CLDC和一个或者多个Profile构成?/p>

  希望通过以上内容的介l,让你能更好的应用J2MEq_?/p>

]]>
Java内存泄漏调试http://www.aygfsteel.com/seosyw/archive/2014/11/21/420381.html董超z?/dc:creator>董超z?/author>Fri, 21 Nov 2014 01:06:00 GMThttp://www.aygfsteel.com/seosyw/archive/2014/11/21/420381.htmlhttp://www.aygfsteel.com/seosyw/comments/420381.htmlhttp://www.aygfsteel.com/seosyw/archive/2014/11/21/420381.html#Feedback0http://www.aygfsteel.com/seosyw/comments/commentRss/420381.htmlhttp://www.aygfsteel.com/seosyw/services/trackbacks/420381.htmlJDK本n试内存泄漏问题提供了比较完善的工兗?/p>

  先用命o(h)jps扑և要调试的jvm的进Eid(jpsq个命o(h)Q就是ps命o(h)前面加jQ列出所有正在运行的jvm的进Eid)?/p>

  例如Qjps

  输出cM下面q样Q?5976 java_app.jar 7586 startup.jar 22476 Jps 12248 Main 5437 Bootstrap

  假设我们要调试的q程id?5976.

  如果只是想简单观察一下堆内存的用情况,可以用命令jmap -histoQlive 15976q个命o(h)?x)输出指定的jvm上当前各?strong>Javacȝ实例数、占用的内存大小和完整的cd。虚拟机内部cȝcd前面?*"标记?/p>

  如果惛_到堆内存使用的详l情况,可以用命令jmap -dumpQliveQformat=bQfile=/tmp/java_app-heap.bin 15976

  q样?tmp目录下得C个java_app-heap.bin文gQ其中保存的信息是指定的jvm中堆内存的用详情。这个二q制文g可以用JDK附带的jhat(Java Heap Analysis Tool)来分析:(x)

  jhat -J-Xmx326m /tmp/java_app-heap.bin

  q个工具相当耗内存,如果出现了OutOfMemoryException的话Q请加大-J-Xmx326m中指定的预留堆内存大再试?/p>

  Jhat?x)解析堆内存信息转储文g(上面用jmap生成的。bin文g)Q输出大概像下面q样Q?/p>

  lewis@mgr $ jhat -J-Xmx326m /tmp/java_app-heap.bin Reading from /tmp/java_app-heap.bin……

  Dump file created Thu Sep 08 20Q?8Q?4 CST 2011 Snapshot readQ?resolving……

  Resolving 71327 objects……

  Chasing referencesQ?expect 14 dots……

  Eliminating duplicate references……

  Snapshot resolved. Started HTTP server on port 7000 Server is ready.

  注意最后两行:(x)堆内存信息{储文件分析完毕后Qjhatq不?x)将分析l果输出Z个静态文件?/p>

  Z方便查找Q以?qing)在相关的类之间DQjhat?x)启动一个服务,监听7000端口。这时候就可以用浏览器来浏览和分析l果了:(x)在浏览器地址栏里输入Q?/p>

  http://localhostQ?000

  里面的各个页面都有很多链接,可以在相关的各个cd(qing)各种l计数据之间跌{Q还是很方便的?/p>

]]>
述Java枚D常见7U用?/title><link>http://www.aygfsteel.com/seosyw/archive/2014/11/21/420379.html</link><dc:creator>董超z?/dc:creator><author>董超z?/author><pubDate>Fri, 21 Nov 2014 01:03:00 GMT</pubDate><guid>http://www.aygfsteel.com/seosyw/archive/2014/11/21/420379.html</guid><wfw:comment>http://www.aygfsteel.com/seosyw/comments/420379.html</wfw:comment><comments>http://www.aygfsteel.com/seosyw/archive/2014/11/21/420379.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/seosyw/comments/commentRss/420379.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/seosyw/services/trackbacks/420379.html</trackback:ping><description><![CDATA[     摘要: DK1.5引入了新的类?#8212;—枚D。在 Java 中它虽然个“?#8221;功能Q却l我的开发带来了“?#8221;方便?  用法一Q常?  在JDK1.5 之前Q我们定义常量都是:(x) publicstaticfianl.... 。现在好了,有了枚DQ可以把相关的常量分l到一个枚丄型里Q而且枚D提供了比帔R更多的方法?..  <a href='http://www.aygfsteel.com/seosyw/archive/2014/11/21/420379.html'>阅读全文</a><img src ="http://www.aygfsteel.com/seosyw/aggbug/420379.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/seosyw/" target="_blank">董超z?/a> 2014-11-21 09:03 <a href="http://www.aygfsteel.com/seosyw/archive/2014/11/21/420379.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JAR文g操作基础http://www.aygfsteel.com/seosyw/archive/2014/11/21/420377.html董超z?/dc:creator>董超z?/author>Fri, 21 Nov 2014 01:01:00 GMThttp://www.aygfsteel.com/seosyw/archive/2014/11/21/420377.htmlhttp://www.aygfsteel.com/seosyw/comments/420377.htmlhttp://www.aygfsteel.com/seosyw/archive/2014/11/21/420377.html#Feedback0http://www.aygfsteel.com/seosyw/comments/commentRss/420377.htmlhttp://www.aygfsteel.com/seosyw/services/trackbacks/420377.htmlThe JavaQ?a style="color: rgb(11,59,140); font-size: 12px; text-decoration: underline" target="_blank">Java教程 Java培训 Q?/strong> Archive (JAR) File Format JAR文g是用ZIP文g格式打包的,所以你可以在一些类似ZIP的Q务中使用它,q行压羃、解压、打包或者解开包。这些都是JAR文g最普遍的用方式,你能发现许多JAR文g的优劉K是源自这些基本的特点?/p>

  即你希望能够用JAR中那些先q的功能比如?sh)子{֐Q你也得先熟(zhn)一些基本的操作方式?/p>

  你可以?strong>Java Archive Tool 来完成JAR文g操作。这个工兯包含在JDK中。因为Java Archive Tool 通过jar命o(h)来调用,Z方便Q我们也可以U它为jat tool.

  下面是最基本的命令操作:(x)

  操作 命o(h)创徏一个JAR文g?jar cf jar-file input-file(s)

  查看JAR文g的内?jar tf jar-file导出JAR文g jar xf jar-file导出JAR文g中制定的文g?jar xf jar-file archived-file(s)

  q行JAR文g中的应用jre -cp app.jar MainClassq行用JAR格式打包的应用java -jar app.jar调用一个打包成JAR的applet 下面是一些详l的介绍Q?/p>

  创徏JAR文g

  jar -cf jar-file input-file(s)

  其中c表示createQf表示fileQ后面紧跟jar-file.也可以增加一个v选项(verbose)Q以昄详细信息 jar -cvf jar-file input-file(s)

  其实q些命o(h)跟常见的tar打包命o(h)cMQ方便记忆?/p>

  另外Q在1.2的版本中q设|了一U?-C 的选项。可以让某些文g夹中的文件直接被打包到根目录下,而没有原始的层次l构的束~。如下例Qjar cf ImageAudio.jar -C images * -C audio *l果是META-INF/MANIFEST.MF cross.gif nor.gif beep.au ding.au return.au yahoo1.au yahoo2.au

  如果不用 -C选项Q直接打包:(x)

  jar cf ImageAudio.jar images audio

  l果如下QMETA-INF/MANIFEST.MF image/cross.gif image/nor.gif audio/beep.au audio/ding.au audio/return.au audio/yahoo1.au audio/yahoo2.au

  查看JAR文g中的内容You canq时候ƈ没有解压jar文gQ直接查看文件内宏V?/p>

  jar tvf TicTacToe.jar同样的,也可以增?-v 选项(verbose)Q显Cl的查看q程?/p>

  解压JAR文g?/p>

  jar文g包中的内容解压出来,在当前工作目录下(而不是jar文g包所在目?创徏解压文g包?/p>

  当然Q也可以定向的解压指定的文g。如下,其中仅有TicTacToe.class images/cross.gif会(x)被解压出来?/p>

  jar xf TicTacToe.jar TicTacToe.class images/cross.gif

  修改Manifest文g有不修改Manifest文g的方法。这里介l一U用jar指o(h)修改的方法?/p>

  有一个m选项Q可以将用户自己~辑的manifest文g同其他文件夹一h包ؓ(f)JAR文g?/p>

  jar cmf manifest-addition jar-file input-file(s)

  q里 manifest-addition 是用戯q辑好的文本文Ӟ文g名没有特D要求,内容为需要写入JAR的manifest文g内容。其他的跟创建JAR文g一栗?/p>

  更新JAR文g

  jar uf jar-file input-file(s)

  当input-file(s)跟JAR文g中原有的文g同名Ӟ则覆盖掉原来的文件。如果没有,则添加相应文件?/p>

  d修改Manifest文g的命令选项后,也可以实现对manifest的更新?/p>

  jar umf manifest jar-file注意其中manifest为txt文gQ内Ҏ(gu)需要更新的manifest文g内容。jar-file则ؓ(f)对应的JAR文g?/p>

  Running JAR-Packaged Software

  java -jar jar-file

  以上命o(h)执行的条件是 JAR的Manifest文g中具?Main-ClassQ?标记。这个标记标明了整个JAR为相应的ȝ服务Q这个主cM必须含有static main E序入口?/p>

  理解Manifest文g

  JAR文g能够支持许多功能Q包括电(sh)子签名,版本控制Q包加密Q扩展等{。这些都是manifest文g的功?

  manifest是一个特D的文本文gQJAR文g包中所有文件的信息都可以包含在其中。通过在manifest中设|不同的|你能lJAR包增dU不同的Ҏ(gu)用途?/p>

  在我们看各种不同的用途之前,先看看manifest的默认内容:(x)文g的默认\径是

  META-INF/MANIFEST.MF默认内容?/p>

  Manifest-VersionQ?1.0q一行展CZmanifest的基本的格式Q键值对。键和值用"Q?分开。默认manifest中展CZ版本受?/p>

  Manifest-VersionQ?1.0

  NameQ?java/math/BigDecimal.class

  SHA1-DigestQ?TD1GZt8G11dXY2p4olSZPc5Rj64=

  MD5-DigestQ?z6z8xPj2AW/Q9AKRSF0cg==

  NameQ?span class="Apple-converted-space"> java/math/BigInteger/class

  SHA1-DigestQoB1GZt8G11dXY2p4olSZPc5Rj64=

  MD5-DigestQ?z6z8xPj2AW/8HAKRSF0cg==

  q里展示了JAR文g中,不同。class文g的不同属性。Name标记了不同的子文Ӟ之后的SHA1_Digest则是对应文g的属性?/p>

  Main-ClassQ?classname Mani-Class键标C整个JAR文gq行的主cR即整个JAR作ؓ(f)一个应用运行时的程序入口?/p>

  Class-PathQ?servlet.jar infobus.jar acme/beans.jar Class-Path键标C需要下载的扩展包?/p>

]]>
JavaU程安全ȝhttp://www.aygfsteel.com/seosyw/archive/2014/11/21/420378.html董超z?/dc:creator>董超z?/author>Fri, 21 Nov 2014 01:01:00 GMThttp://www.aygfsteel.com/seosyw/archive/2014/11/21/420378.htmlhttp://www.aygfsteel.com/seosyw/comments/420378.htmlhttp://www.aygfsteel.com/seosyw/archive/2014/11/21/420378.html#Feedback0http://www.aygfsteel.com/seosyw/comments/commentRss/420378.htmlhttp://www.aygfsteel.com/seosyw/services/trackbacks/420378.html

最q想?strong>JavaQ?a style="color: rgb(11,59,140); font-size: 12px; text-decoration: underline" target="_blank">Java教程 Java培训 Q?/strong>基础的一些东襉K整理整理Q写下来Q这是对知识的ȝQ也是一U乐。已l拟好了提纲Q大概分几个主题Q?javaU程安全Qjava垃圾攉Qjavaq发包详l介l,java profile和jvm性能调优。慢慢写吧。本人jameswxx原创文章Q{载请注明出处Q我费了很多心血Q多谢了。关于javaU程安全Q网上有很多资料Q我只想从自q角度ȝ对这斚w的考虑Q有时候写东西是很痛苦的,知道一些东西,但想用文字说清楚Q却不是那么Ҏ(gu)。我认ؓ(f)要认识javaU程安全Q必M解两个主要的点:(x)java的内存模型,java的线E同步机制。特别是内存模型Qjava的线E同步机制很大程度上都是Z内存模型而设定的。后面我q会(x)写javaq发包的文章Q详lȝ如何利用javaq发包编写高效安全的多线Eƈ发程序。暂时写得比较仓促,后面?x)慢慢补充完善?/p>

  谈java内存模型

  不同的^収ͼ内存模型是不一L(fng)Q但是jvm的内存模型规范是l一的。其实java的多U程q发问题最l都?x)反映在java的内存模型上Q所谓线E安全无非是要控制多个线E对某个资源的有序访问或修改。ȝjava的内存模型,要解决两个主要的问题Q可见性和有序性。我们都知道计算机有高速缓存的存在Q处理器q不是每ơ处理数据都是取内存的。JVM定义了自q内存模型Q屏蔽了底层q_内存理l节Q对于java开发h员,要清楚在jvm内存模型的基上,如果解决多线E的可见性和有序性?/p>

  那么Q何谓可见?多个U程之间是不能互怼递数据通信的,它们之间的沟通只能通过׃n变量来进行。Java内存模型(JMM)规定了jvm有主内存Q主内存是多个线E共享的。当new一个对象的时候,也是被分配在d存中Q每个线E都有自q工作内存Q工作内存存储了d的某些对象的副本Q当然线E的工作内存大小是有限制的。当U程操作某个对象Ӟ执行序如下Q?/p>

  (1)从主存复制变量到当前工作内存(read and load)

  (2)执行代码Q改变共享变量?use and assign)

  (3)用工作内存数据刷C存相兛_?store and write)

  JVM规范定义了线E对d的操作指令:(x)readQloadQuseQassignQstoreQwrite.当一个共享变量在多个U程的工作内存中都有副本Ӟ如果一个线E修改了q个׃n变量Q那么其他线E应该能够看到这个被修改后的|q就是多U程的可见性问题?/p>

  那么Q什么是有序性呢?U程在引用变量时不能直接从主内存中引用,如果U程工作内存中没有该变量Q则?x)从d存中拯一个副本到工作内存中,q个q程为read-loadQ完成后U程?x)引用该副本。当同一U程再度引用该字D|Q有可能重新从主存中获取变量副本(read-load-use)Q也有可能直接引用原来的副本(use)Q也是?readQloadQuse序可以由JVM实现pȝ军_?/p>

  U程不能直接Z存中中字D赋|它会(x)值指定给工作内存中的变量副本(assign)Q完成后q个变量副本?x)同步到d储区(store-write)Q至于何时同步过去,Ҏ(gu)JVM实现pȝ军_。有该字D,则会(x)从主内存中将该字D赋值到工作内存中,q个q程为read-loadQ完成后U程?x)引用该变量副本Q当同一U程多次重复对字D赋值时Q比如:(x)

  Java代码

  for(int i=0;i<10;i++)

  a++;U程有可能只对工作内存中的副本进行赋|只到最后一ơ赋值后才同步到d储区Q所以assignQstoreQweite序可以由JVM实现pȝ军_。假设有一个共享变量xQ线Ea执行x=x+1.从上面的描述中可以知道x=x+1q不是一个原子操作,它的执行q程如下Q?/p>

  1、从d中读取变量x副本到工作内?、给x? 3、将x?后的值写回主 ?/p>

  如果另外一个线Eb执行x=x-1Q执行过E如下:(x)

  1、从d中读取变量x副本到工作内?、给x? 3、将x?后的值写回主?/p>

  那么昄Q最l的x的值是不可靠的。假设x现在?0Q线Ea?Q线Eb?Q从表面上看Q似乎最lxq是?0Q但是多U程情况下会(x)有这U情况发生:(x)

  1Q线Ea从主存读取x副本到工作内存,工作内存中xgؓ(f)10 2Q线Eb从主存读取x副本到工作内存,工作内存中xgؓ(f)10 3Q线Ea工作内存中x?Q工作内存中xgؓ(f)11 4Q线Eax提交d中,d中x?1 5Q线Eb工作内存中x值减1Q工作内存中xgؓ(f)9 6Q线Ebx提交Cd中,d中x?

  同样Qx有可能ؓ(f)11Q如果x是一个银行̎PU程a存款Q线Eb扣款Q显然这h有严重问题的Q要解决q个问题Q必M证线Ea和线Eb是有序执行的Qƈ且每个线E执行的?或减1是一个原子操作。看看下面代码:(x)

  Java代码

  public class Account {

  private int balance;

  public Account(int balance) { this.balance = balance;}

  public int getBalance() { return balance;}

  public void add(int num) { balance = balance + num;}

  public void withdraw(int num) { balance = balance - num;}

  public static void main(String[] args) throws InterruptedException { Account account = new Account(1000);Thread a = new Thread(new AddThread(accountQ?20)Q?"add");Thread b = new Thread(new WithdrawThread(accountQ?20)Q?"withdraw");a.start();b.start();a.join();b.join();System.out.println(account.getBalance());}

  static class AddThread implements Runnable { Account account;int amount;

  public AddThread(Account accountQ?int amount) { this.account = account;this.amount = amount;}

  public void run() { for (int i = 0; i < 200000; i++) { account.add(amount);}

  static class WithdrawThread implements Runnable { Account account;int amount;

  public WithdrawThread(Account accountQ?int amount) { this.account = account;this.amount = amount;}

  public void run() { for (int i = 0; i < 100000; i++) { account.withdraw(amount);}W一ơ执行结果ؓ(f)10200Q第二次执行l果?060Q每ơ执行的l果都是不确定的Q因为线E的执行序是不可预见的。这是java同步产生的根源,synchronized关键字保证了多个U程对于同步块是互斥的,synchronized作ؓ(f)一U同步手D,解决java多线E的执行有序性和内存可见性,而volatile关键字之解决多线E的内存可见性问题。后面将?x)详l介l?/p>


]]>
java的内存管?/title><link>http://www.aygfsteel.com/seosyw/archive/2014/11/21/420376.html</link><dc:creator>董超z?/dc:creator><author>董超z?/author><pubDate>Fri, 21 Nov 2014 01:00:00 GMT</pubDate><guid>http://www.aygfsteel.com/seosyw/archive/2014/11/21/420376.html</guid><wfw:comment>http://www.aygfsteel.com/seosyw/comments/420376.html</wfw:comment><comments>http://www.aygfsteel.com/seosyw/archive/2014/11/21/420376.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/seosyw/comments/commentRss/420376.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/seosyw/services/trackbacks/420376.html</trackback:ping><description><![CDATA[<p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">1?strong>Java</strong>的内存管理就是对象的分配和释N题?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  在Java中,E序员需要通过关键字new为每个对象申请内存空?(基本cd除外)Q所有的对象都在?(Heap)中分配空间?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  对象的释放是由GC军_和执行的?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  在Java中,内存的分配是q序完成的Q而内存的释放是有GC完成的,q种收支两条U的Ҏ(gu)化了E序员的工作。但也加重了JVM的工作。这也是JavaE序q行速度较慢的原因之一?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  GC释放I间Ҏ(gu)Q监控每一个对象的q行状态,包括对象的申诗引用、被引用、赋值等。当该对象不再被引用Ӟ释放对象?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  2、内存管理结构Java使用有向囄方式q行内存理Q对于程序的每一个时刻,我们都有一个有向图表示JVM的内存分配情c(din)?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  对象考虑为有向图的顶点,引用关p考虑为图的有向边Q有向边从引用者指向被引对象。另外,每个U程对象可以作ؓ(f)一个图的v始顶点,例如大多E序从mainq程开始执行,那么该图是以mainq程点开始的一|?wi)。在q个有向图中Q根点可达的对象都是有效对象,GC不回收q些对象。如果某个对?(q通子?与这个根点不可?注意Q该图ؓ(f)有向?Q那么我们认?q些)对象不再被引用,可以被GC回收?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  3、用有向图方式理内存的优~点Java使用有向囄方式q行内存理Q可以消除引用@环的问题Q例如有三个对象Q相互引用,只要它们和根q程不可辄Q那么GC也是可以回收它们的?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  q种方式的优Ҏ(gu)理内存的精度很高,但是效率较低?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  另外一U常用的内存理技术是使用计数器,例如COM模型采用计数器方式管理构Ӟ它与有向囄比,_ֺ行低(很难处理循环引用的问?Q但执行效率很高?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  ★<span id="wmqeeuq" class="Apple-converted-space"> </span><strong>Java</strong>的内存泄露Java虽然由GC来回收内存,但也是存在泄露问题的Q只是比C++一炏V?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  1、与C++的比较c++所有对象的分配和回攉需要由用户来管理。即需要管理点Q也需要管理边。若存在不可辄点,无法在回收分配给那个点的内存Q导致内存泄霌Ӏ存在无用的对象引用Q自然也?x)导致内存泄霌Ӏ?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  Java由GC来管理内存回ӞGC回收不可达的对象占用的内存I间。所以,Java需要考虑的内存泄露问题主要是那些被引用但无用的对?#8212;—x要管理边可以。被引用但无用的对象Q程序引用了该对象,但后l不?x)再使用它。它占用的内存空间就费了?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  如果存在对象的引用,q个对象p定义?#8220;zd?#8221;Q同时不?x)被释放?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  2、Java内存泄露处理处理Java的内存泄露问题:(x)认该对象不再会(x)被用?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  典型的做?#8212;—把对象数据成员设为null从集合中U除该对象注意,当局部变量不需要时Q不需明显的设为nullQ因Z个方法执行完毕时Q这些引用会(x)自动被清理?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  例子Qview plain<span style="padding-bottom: 0px; line-height: 25px; padding-left: 5px; padding-right: 5px; float: left; height: 25px; padding-top: 0px">List myList=new ArrayList();for (int i=1;i<100; i++)</span></p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  { Object o=new Object();myList.add(o);o=null;} //此时Q所有的Object对象都没有被释放Q因为变量myList引用q些对象?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  当myList后来不再用到Q将之设为nullQ释放所有它引用的对象。之后GC便会(x)回收q些对象占用的内存?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  ★ 对GC操作对GC的操作ƈ不一定能辑ֈ理内存的效果?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  GC对于E序员来说基本是透明的,不可见的。我们只有几个函数可以访问GCQ例如运行GC的函数System.gc()QSystem.?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  但是Ҏ(gu)Java语言规范定义Q?System.gc()函数不保证JVM的垃圾收集器一定会(x)执行。因为,不同的JVM实现者可能用不同的法理GC.通常QGC的线E的优先U别较低?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  JVM调用GC的策略有很多U,有的是内存用到达一定程度时QGC才开始工作,也有定时执行的,有的是^~执行GCQ有的是中断式执行GC.但通常来说Q我们不需要关心这些。除非在一些特定的场合QGC的执行媄(jing)响应用程序的性能Q例如对于基于Web的实时系l,如网l游戏等Q用户不希望GCH然中断应用E序执行而进行垃圑֛Ӟ那么我们需要调整GC的参敎ͼ让GC能够通过q缓的方式释攑ֆ存,例如垃圑֛收分解ؓ(f)一pd的小步骤执行QSun提供的HotSpot JVM支持这一Ҏ(gu)?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  ★ 内存泄露(g)市Z已有几种专业(g)?strong>Java</strong>内存泄漏的工P它们的基本工作原理大同小异,都是通过监测JavaE序q行Ӟ所有对象的甌、释攄动作Q将内存理的所有信息进行统计、分析、可视化。开发h员将Ҏ(gu)q些信息判断E序是否有内存泄漏问题。这些工具包括Optimizeit ProfilerQJProbe ProfilerQJinSight Q?Rational 公司的Purify{?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  在运行过E中Q我们可以随时观察内存的使用情况Q通过q种方式Q我们可以很快找到那些长期不被释放,q且不再使用的对象。我们通过(g)查这些对象的生存周期Q确认其是否为内存泄霌Ӏ?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  ★ 软引用特点:(x)只有当内存不够的时候才回收q类内存Q同时又保证在Java抛出OutOfMemory异常之前Q被讄为null.保证最大限度的使用内存而不引vOutOfMemory异常?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  在某些时候对软引用的使用?x)降低应用的q行效率与性能Q例如:(x)应用软引用的对象的初始化q程较ؓ(f)耗时Q或者对象的状态在E序的运行过E中发生了变化,都会(x)l重新创建对象与初始化对象带来不同程度的ȝ(ch)?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  用途:(x)可以用于实现一些常用资源的~存Q实现Cache的功能处理一些占用内存大而且声明周期较长Q但使用q不频繁的对象时应尽量应用该技?#9733; javaE序设计中有兛_存管理的l验1.最基本的徏议是早释放无用对象的引用。如Qview plain<span style="padding-bottom: 0px; line-height: 25px; padding-left: 5px; padding-right: 5px; float: left; height: 25px; padding-top: 0px">……</span></p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  A a = new A();//应用a对象a = null; //当用对象a之后d其讄为空…?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  注:(x)如果a 是方法的q回|不要做这L(fng)处理Q否则你从该Ҏ(gu)中得到的q回值永qؓ(f)I,而且q种错误不易被发现、排?.量用finalize函数。它?x)加大GC的工作量?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  3.如果需要用经常用到的囄Q可以用soft应用cd。它?yu)可能把囄保存在内存?.注意集合数据cdQ包括数l、树(wi)、图、链表等数据l构Q这些数据结构对GC来说Q回收更为复杂?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  5.量避免在类的默认构造器中创建、初始化大量的对象,防止在调用其自类的构造器旉成不必要的内存资源费6.量避免强制pȝ做垃圑ֆ存的回收Q增长系l做垃圾回收的最l时?.量避免昑ּ甌数组I间8.量做远E方法调用类应用开发时使用瞬间值变量,除非q程调用端需要获取该瞬间值变量的倹{?/p> <p style="border-bottom: 0px; text-align: left; border-left: 0px; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); list-style-type: none; text-indent: 0px; margin: 20px 0px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体; white-space: normal; color: rgb(51,51,51); border-top: 0px; border-right: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px">  9.量在合适的场景下用对象池技术以提高pȝ性能?/p><img src ="http://www.aygfsteel.com/seosyw/aggbug/420376.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/seosyw/" target="_blank">董超z?/a> 2014-11-21 09:00 <a href="http://www.aygfsteel.com/seosyw/archive/2014/11/21/420376.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JavaE序q接各种数据库的Ҏ(gu)http://www.aygfsteel.com/seosyw/archive/2014/10/28/419145.html董超z?/dc:creator>董超z?/author>Tue, 28 Oct 2014 14:15:00 GMThttp://www.aygfsteel.com/seosyw/archive/2014/10/28/419145.htmlhttp://www.aygfsteel.com/seosyw/comments/419145.htmlhttp://www.aygfsteel.com/seosyw/archive/2014/10/28/419145.html#Feedback0http://www.aygfsteel.com/seosyw/comments/commentRss/419145.htmlhttp://www.aygfsteel.com/seosyw/services/trackbacks/419145.html1、Oracle8/8i/9i数据库(数据库培?/a> 数据库认?/a> Q?thin模式)

  Class.forName(“oracle.jdbc.driver.OracleDriver”).newInstance();

  String url=“jdbc:oracle:thinQ@localhost:1521:orcl”; //orcl为数据库的SID

  String user=“test”;

  String password=“test”;

  Connection conn= DriverManager.getConnection(urlQuserQpassword);

  2、DB2数据?/p>

  Class.forName(“com.ibm.db2.jdbc.app.DB2Driver ”).newInstance();

  String url=“jdbc:db2://localhost:5000/sample”; //sampleZ的数据库?/p>

  String user=“admin”;

  String password=“”;

  Connection conn= DriverManager.getConnection(urlQuserQpassword);

  3、SQL Server7.0/2000数据?/p>

  Class.forName(“com.microsoft.jdbc.sqlserver.SQLServerDriver”).newInstance();

  String url=“jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb”;

  //mydb为数据库

  String user=“sa”;

  String password=“”;

  Connection conn= DriverManager.getConnection(urlQuserQpassword);

  4、Sybase数据?/p>

  Class.forName(“com.sybase.jdbc.SybDriver”).newInstance();

  String url =“ jdbc:sybase:Tds:localhost:5007/myDB”;//myDBZ的数据库?/p>

  Properties sysProps = System.getProperties();

  SysProps.put(“user”Q?#8220;userid”);

  SysProps.put(“password”Q?#8220;user_password”);

  Connection conn= DriverManager.getConnection(urlQ?SysProps);

  5、Informix数据?/p>

  Class.forName(“com.informix.jdbc.IfxDriver”).newInstance();

  String url = “jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;

  user=testuser;password=testpassword”; //myDB为数据库?/p>

  Connection conn= DriverManager.getConnection(url);

  6、MySQL数据?/p>

  Class.forName(“org.gjt.mm.mysql.Driver”).newInstance();

  //或者Class.forName(“com.mysql.jdbc.Driver”);

  String url =“jdbc:mysql://localhost/myDB?

  user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1”

  //myDB为数据库?/p>

  Connection conn= DriverManager.getConnection(url);

  7、PostgreSQL数据?/p>

  Class.forName(“org.postgresql.Driver”).newInstance();

  String url =“jdbc:postgresql://localhost/myDB” //myDB为数据库?/p>

  String user=“myuser”;

  String password=“mypassword”;

  Connection conn= DriverManager.getConnection(urlQuserQpassword);

  8、access数据库直q用ODBC?/p>

  Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”) ;

  String url=“jdbc:odbc:Driver={MicroSoft Access Driver

  (*.mdb)};DBQ=”+application.getRealPath(“/Data/ReportDemo.mdb”);

  Connection conn = DriverManager.getConnection(urlQ?#8220;”Q?#8220;”);

  Statement stmtNew=conn.createStatement() ;



]]>
实现鼠标d的JavaE序http://www.aygfsteel.com/seosyw/archive/2014/10/28/419144.html董超z?/dc:creator>董超z?/author>Tue, 28 Oct 2014 14:14:00 GMThttp://www.aygfsteel.com/seosyw/archive/2014/10/28/419144.htmlhttp://www.aygfsteel.com/seosyw/comments/419144.htmlhttp://www.aygfsteel.com/seosyw/archive/2014/10/28/419144.html#Feedback0http://www.aygfsteel.com/seosyw/comments/commentRss/419144.htmlhttp://www.aygfsteel.com/seosyw/services/trackbacks/419144.htmlimport java.awt.Color;

  import java.awt.Frame;

  import java.awt.Graphics;

  import java.awt.Point;

  import java.awt.event.MouseAdapter;

  import java.awt.event.MouseEvent;

  import java.awt.event.MouseListener;

  import java.util.ArrayList;

  import java.util.Iterator;

  public class MyMouseAdapter {

  public static void main(String[] args) {

  new MyFrame5(“drawing”);

  }

  }

  class MyFrame5 extends Frame{

  ArrayList points=null;

  MyFrame5(String s){

  super(s);

  points=new ArrayList();

  setLayout(null);

  setBounds(300Q?00Q?00Q?00);

  this.setBackground(new Color(204Q?04Q?55));

  setVisible(true);

  this.addMouseListener(new Monitor5());

  }

  public void paint(Graphics g){

  Iterator i=points.iterator();

  while(i.hasNext()){

  Point p=(Point)i.next();

  g.setColor(Color.blue);

  g.fillOval(p.xQp.yQ?10Q?10);

  }

  }

  public void addPoint(Point p){

  points.add(p);

  }

  }

  class Monitor5 extends MouseAdapter{ //MouseAdapter实现了MouseListener接口

  public void mousePressed(MouseEvent e){

  MyFrame5 f=(MyFrame5)e.getSource();

  f.addPoint(new Point(e.getX()Qe.getY()));

  f.repaint(); //让Frame强制l行重画

  }

  }



]]>
Java设计模式之Visitor模式http://www.aygfsteel.com/seosyw/archive/2014/10/28/419143.html董超z?/dc:creator>董超z?/author>Tue, 28 Oct 2014 14:13:00 GMThttp://www.aygfsteel.com/seosyw/archive/2014/10/28/419143.htmlhttp://www.aygfsteel.com/seosyw/comments/419143.htmlhttp://www.aygfsteel.com/seosyw/archive/2014/10/28/419143.html#Feedback0http://www.aygfsteel.com/seosyw/comments/commentRss/419143.htmlhttp://www.aygfsteel.com/seosyw/services/trackbacks/419143.htmlVisitor 定义

  作用于某个对象群中各个对象的操作?它可以你在不改变这些对象本w的情况下,定义作用于这些对象的新操作?/p>

  在JavaQ?a style="color: rgb(11,59,140); font-size: 12px; text-decoration: underline" target="_blank">Java教程 Java培训 Q?中,Visitor 模式实际上是分离了collection l构中的元素和对q些元素q行操作的行为?/p>

  Z使用Visitor?

  Java 的Collection(包括Vector 和Hashtable)是我们最l常使用的技术,可是Collection好象是个黑色大染~,本来有各U鲜明类型特征的对象一旦放入后Q再取出Ӟq些cd消׃。那么我们势必要用If 来判断,如:(x)

  Iterator iterator = collection.iterator()

  while (iterator.hasNext()) {

  Object o = iterator.next();

  if (o instanceof Collection)

  messyPrintCollection((Collection)o);

  else if (o instanceof String)

  System.out.println(“‘”+o.toString()+“’”);

  else if (o instanceof Float)

  System.out.println(o.toString()+“f”);

  else

  System.out.println(o.toString());

  }

  在上例中Q我们用了 instanceof 来判?o 的类型?/p>

  很显?dng)q样做的~点代码If else if 很繁琐。我们就可以使用Visitor 模式解决它?/p>

  如何使用Visitor?

  针对上例Q我们设计一个接口visitor 讉K者:(x)

  public interface Visitor

  {

  public void visitCollection(Collection collection);

  public void visitString(String string);

  public void visitFloat(Float float);

  }

  在这个接口中Q将我们认ؓ(f)Collection 有可能的cȝcd攑օ其中?/p>

  有了讉K者,我们需要被讉K者,被访问者就是我们Collection 的每个元素ElementQ我们要些Element 定义一个可以接受访问的接口(讉K和被讉K是互动的Q只有访问者,被访问者如果表CZƢ迎Q访问者就不能讉K)Q我们定义这个接口叫VisitableQ?用来定义一个Accept 操作Q也是说让Collection 每个元素具备可访问性?/p>

  public interface Visitable

  {

  public void accept(Visitor visitor);

  }

  好了Q有了两个接口,我们p定义他们的具体实?Concrete class)Q?/p>

  public class ConcreteElement implements Visitable

  {

  private String value;

  public ConcreteElement(String string) {

  value = string;

  }

  //定义accept 的具体内?q里是很单的一句调?/p>

  public void accept(Visitor visitor) {

  visitor.visitString(this);

  }

  }

  再看看访问者的Concrete 实现Q?/p>

  public class ConcreteVisitor implements Visitor

  {

  //在本Ҏ(gu)中,我们实现了对Collection 的元素的成功讉K

  public void visitCollection(Collection collection) {

  Iterator iterator = collection.iterator()

  while (iterator.hasNext()) {

  Object o = iterator.next();

  if (o instanceof Visitable)

  ((Visitable)o).accept(this);

  }

  public void visitString(String string) {

  System.out.println(“‘”+string+“’”);

  }

  public void visitFloat(Float float) {

  System.out.println(float.toString()+“f”);

  }

  }

  }

  在上面的visitCollection 我们实现了对Collection 每个元素讉KQ只使用了一个判断语句,只要判断其是否可以访问?/p>

  xQ我们完成了Visitor 模式基本架构?/p>

  使用Visitor 模式的前?/p>

  对象结构中(Collection) 中的对象cd很少改变Q也是说访问者的w䆾cd很少改变Q如上面中Visitor 中的cd很少改变Q如果需要增加新的操作,比如上例中我们在ConcreteElement 具体实现外,q需要新的ConcreteElement2 ConcreteElement3.

  可见使用Visitor 模式是有前提的,在两个接口Visitor 和Visitable 中,保Visitor 很少变化Q变化的是VisitableQ这样用Visitor 最方便?/p>

  如果Visitor 也经常变化, 也就是说Q对象群中的对象cdl常改变Q一般徏议是Q不如在q些对象cM逐个定义操作。但是Java 的Reflect 技术解决了q个问题?/p>

  Reflect 技术是在运行期间动态获取对象类型和Ҏ(gu)的一U技术,具体实现参考Javaworld的英文原?



]]>
վ֩ģ壺 | ǹ| | | Դ| | | С| ˳| | | | Ѱ| | | ϰ| | ߰| | | ɽ| Ҿ| | | ɽ| | ɳ| | ˫| | ǫ| ¡| ˮ| פ| | | ߰| ˫| ν| ľ| |