应用j2ee平台开发的系统的性能是系统使用者和开发者都关注的问题,本文从服务器端编程时应注意的几个方面讨论代码对性能的影响,并总结一些解决的建议。
关键词:性能,java,j2ee,ejb,servlet,jdbc
一、概要
java 2 platform, enterprise edition (j2ee)是当前很多商业应用系统使用的开发平台,该技术提供了一个基于组件的方法来设计、开发、装配和部署企业级应用程序。j2ee平台提供了一个多层结构的分布式的应用程序模型,可以更快地开发和发布的新的应用解决方案。
j2ee是一种技术规范,定义了整个标准的应用开发体系结构和一个部署环境,应用开发者开发时只要专注于具体商业逻辑和商业业务规则的实现上,而其他的诸如事务、持久化、安全等系统开发问题可以由应用程序容器或者服务器处理,开发完成后,就可以方便地部署到实现规范的应用服务器中。
作为网络上的商业应用系统,同时访问的人数是很多的,在大量访问的情况下,过多的资源请求和有限的服务器资源(内存、cpu时间、网络带宽等)之间就会出现矛盾,应用系统的性能就显得很重要了,有时正确的代码并不能保证项目的成功,性能往往是最后决定一个项目是否成功关键。
本文主要从性能的角度出发,讨论j2ee服务器端的代码性能优化和提升。
二、常见的java 编程
j2ee语言基础是java,常用的java代码问题对应用系统的性能影响,下面讨论了一些应该注意方面。
·使用stringbuffer代替string
当处理字符串的相加时,常见的写法是:..
| string str1 = "hello"; string str2 = "welcome to world"; string str3 = str1 + ", " + str2 +"!"; system.out.println(str3); |
| public stringbuffer() { // 构造函数 this(16); // 缺省容量16} public synchronized stringbuffer append(string str) { if (str == null) { str = string.valueof(str); } int len =str.length(); int newcount = count + len; if(newcount > value.length) expandcapacity(newcount); // 扩充容量 str.getchars(0, len, value, count); count = newcount; return this; } |
| stringbuffer buffer = new stringbuffer(30); // 分配指定的大小。 buffer.append("hello"); buffer.append(","); buffer.append("welcometo world!"); string str = buffer.tostring(); |
| public vector() {// 缺省构造函数 this(10); // 容量是 10; } |
| public synchronized boolean add(object o) { modcount++; ensurecapacityhelper(elementcount+1); elementdata[elementcount++] =o; return true; } private void ensurecapacityhelper(int mincapacity) { int oldcapacity = elementdata.length; if (mincapacity > oldcapacity) { object olddata[] = elementdata; int newcapacity = (capacityincrement > 0) ? (oldcapacity + capacityincrement) : (oldcapacity * 2); if (newcapacity < mincapacity) { newcapacity = mincapacity; } elementdata = new object[newcapacity]; system.arraycopy(olddata, 0, elementdata, 0, elementcount); } } |
| vector vect =.. new vector(100); |
| vector vect = new vector(1000); ... for( inti=0; i<vect.size(); i++){ ... } |
| int size = vect.size(); for( int i=0; i>size; i++){ ... } |
| for (int i = 0;i <100000;i++) if (i%10 == 9) { ... // 每十次执行一次 } |
| for(inti =0,j =10; i<100000; i++,j--){ if(j == 0){ ... // 每十次执行一次 j = 10; } } |
| newobject object = new newobject(); int value; if(i>0 ) { value =object.getvalue(); } |
| int value; if(i>0 ) { newobject object = new newobject(); value =object.getvalue(); } |
| public void callmethod(int i ){ if( i ==0 ){ return; } ... // 其他处理 } |
| int i = 0; ... callmethod(i); |
| int i = 0; ... if( i ==0 ){ callmethod(i); } |
| int size = 1000; string[] strarray1 = new string[size]; string[] strarray2 = new string[size]; for(inti=0;i<size;i++){ // 赋值 strarray1[i] = (new string("array: " + i)); } for(inti=0;i<size;i++){ // 复制 strarray2[i]=(new string((string)a[i])); } |
| int size = 1000; string[] strarray1 = new string[size]; string[] strarray2 = new string[size]; for(inti=0;i<size;i++){ // 赋值 strarray1[i] = (new string("array: " + i)); } system.arraycopy(strarray1,0,strarray2,0,size); // 复制 |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器