posted @ 2009-06-08 09:13 saobaolu| 編輯 收藏
posted @ 2009-06-08 08:58 saobaolu| 編輯 收藏
今日研究NetBeans欲完成課程設(shè)計學(xué)生管理系統(tǒng),
突然發(fā)現(xiàn)不會連接數(shù)據(jù)庫了……
思索中,用jsp的方法去test了一下,感覺……對啊,老師曾經(jīng)講過這個跟jsp鏈接數(shù)據(jù)庫是一樣的,巨汗…… =_=!
Class.forName………
con………..
stm………..
——運行——報錯(ClassNotFoundException)——添加庫MySQL-JDBC
應(yīng)該沒錯了吧,但是……
SQLException: No suitable driver for mysql://localhost:3306/stu
而后發(fā)現(xiàn)了一個很恥辱的問題:
con = DriverManager.getConnection("mysql://localhost:3306/stu","×","×");
天啊,mysql://localhost:3306/stu,我竟然忘記寫 jdbc在mysql前面!~~
--------------------------------------------------------
中國移動老總上公廁,守門大爺說:進去3毛,出來2毛。
老總一愣說:出來還收費?大爺說:學(xué)習(xí)移動,雙向收費。
老總從廁所出來又被攔住:你蹲的是8號坑,交1元錢的選號費,放了一個屁,交1元的漫游費,超過3分鐘,再交1元的超時費。
廁所有背景音樂,收彩鈴費2毛。
大爺勸老總,如果你經(jīng)常光顧,還是辦個廁所套餐比較合算。
老總大怒:這是那家的王法!大爺一擺手:動感地帶,我的地盤我做主!
祝大家端午節(jié)開心
posted @ 2009-05-26 13:37 saobaolu| 編輯 收藏
Problem A: University
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 726
Accepted: 422
Description
企鵝大學(xué)有很多很多名學(xué)生,課程也很多。譬如企鵝語,北極熊語,企鵝初級數(shù)學(xué),企鵝高級數(shù)學(xué)等等等等…… 現(xiàn)在,企鵝大學(xué)的校長想請你為他們寫一個程序:計算總分的程序。請你由他們輸入課程的數(shù)量以及一個學(xué)生每門課程的分數(shù),輸出該學(xué)生的總分。
Input
第一行:一個整數(shù)n(1 ≤ n ≤ 5000),代表企鵝大學(xué)的課程數(shù)目。
第2至第n+1行:每行有一個1整數(shù),第i+1行代表某一位學(xué)生在第i門課上取得的分數(shù)。分數(shù)為0到100的整數(shù)。
Output
一個整數(shù)。代表該學(xué)生在n門課中所得的總分。
Sample Input
3 100 99 100
Sample Output
299
Problem B: Doudou
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 323
Accepted: 75
Description
有只企鵝叫豆豆,總是被別的企鵝欺負。豆豆在長期的隱忍之后,掌握了所有企鵝的高度和攻擊力強度,還得到了一把黃金劍。在擁有了黃金劍以后,豆豆終于可以展開絕地大反擊。但這把黃金劍的用法卻很奇怪。
首先,豆豆第一次可以選擇任何一只企鵝開始挑戰(zhàn)。豆豆這一次必勝。
再次,當(dāng)豆豆已經(jīng)挑戰(zhàn)過某一只企鵝后,再下一次的挑戰(zhàn)對象只能是比上一名對手高,且比上一名對手攻擊力強的企鵝。這樣豆豆必勝。否則黃金劍會覺得打的沒意思而故意發(fā)脾氣輸?shù)簟6苟惯€會被大家集體暴打。
面對著這把脾氣很大的黃金劍,豆豆想請你幫助他計算一下,他最多可以連續(xù)擊敗多少只企鵝?
Input
第一行:一個數(shù)據(jù)n,代表企鵝群里除了豆豆一共有n(1 ≤ n ≤ 1000)只企鵝。
第2至第n+1行:每行2個數(shù)字。第i+1行的第一個數(shù)字為企鵝i的高度。第i+1行的第二個數(shù)字為企鵝i的攻擊力。0 ≤ 高度,攻擊力 ≤ 1,000,000。
Output
一個數(shù)。代表豆豆最多可以連續(xù)擊敗的企鵝數(shù)。
Sample Input
Sample Input #1
Sample Input #2
3
1 3
3 2
2 4
5
10 1
9 2
7 3
6 4
5 5
Sample Output
Sample Output #1
Sample Output #2
2
1
Problem C: Ball
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 6
Accepted: 0
Description
給出空間上兩個運動的小球,球心坐標(biāo)在分別是A(xa , ya , 0),B(xb , yb , 0),半徑分別為Ra , Rb,速度分別為Va( vax, vay , 0), Vb( vbx , vby, 0)。判斷兩個小球是否會碰撞,若會碰撞, 輸出首次碰撞時的時刻和兩個小球的坐標(biāo);若不會碰撞,輸出“Impossible”(球心和速度的z坐標(biāo)恒為0,可將本題視為只是平面上的運動。初始時刻為0,若初始時刻小球貼在一起,視為首次碰撞)。
Input
第一行:一個整數(shù)T,(T ≤ 30),表示下面有T組數(shù)據(jù)。
接下來,每兩行組成一組數(shù)據(jù),首行包含5個實數(shù),用空格隔開,依次是xa , ya , vax, vay , Ra,下面一行也包含5個實數(shù),依次是xa , ya , vbx , vby, Rb。每組數(shù)據(jù)之間有一個空行。
Output
對于每組數(shù)據(jù),如果兩個小球會碰撞,輸出首次碰撞時的時刻t,和兩個小球的坐標(biāo)xap , yap , xbp , ybp ,用空格隔開,保留三位小數(shù)。如果不能,輸出“Impossible”。
Sample Input
3 100 200 0 0 55 100 100 0 0 45 131 123 45 2 43 454 230 0 -5 35 100 100 1 1 31 200 200 2 2 23
Sample Output
0.000 100.000 200.000 100.000 100.000 6.179 409.053 135.358 454.000 199.105 Impossible
Problem D: String
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 14
Accepted: 0
Description
給定一個字符串S[1..n]和一個整數(shù)T,現(xiàn)在需要在字符串S中找出長度不小于T的一個子串,使得其在原串中不重疊出現(xiàn)的次數(shù)最多,求這個次數(shù)。
Input
第一行:一個整數(shù)T(T > 1)
第二行:一個字符串S,且僅包含小寫字母,字符串長度不超過10000
Output
一個整數(shù)。代表出現(xiàn)最多的次數(shù)
Sample Input
2 ababab
Sample Output
3
Problem E: Papercut
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 3
Accepted: 0
Description
現(xiàn)在桌面上有一張矩形紙,上邊有n×m個格子,每個格子有一個數(shù)字。 每張矩形紙可以算出一個數(shù)值F,F是由紙張里任意兩個不同的格子里的數(shù)字相乘之和。如果該紙只有一個格子,那么F=0。
剪紙規(guī)則是:
1、沿格子邊緣一直剪成兩個矩形紙,每張紙里必須有數(shù)字。
2、每次剪紙在桌面上任意選一張矩形紙,進行1操作,再把剪出來的兩張紙放到桌面。
現(xiàn)在你可以對桌面上的紙最多剪k次,問最后桌面上所有矩形紙的F值之和最小是多少?
1 ≤ n ≤ 10
1 ≤ m ≤ 10
1 ≤ k ≤ 50
Input
第一行:3個整數(shù)n, m, k
接下來n行:每行m個正整數(shù),范圍在[1,10],第i行第j個數(shù)表示當(dāng)前桌面那張矩形紙里邊第i行第j個格子里的數(shù)字。
Output
一個整數(shù)。代表最小F值和。
Sample Input
Sample Input #1
Sample Input #2
4 4 4
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
10 10 5
4 2 3 5 6 10 1 6 5 8
3 6 9 1 7 10 7 10 8 1
7 8 3 3 2 5 9 9 8 2
5 5 9 9 3 10 2 9 10 2
1 1 6 7 6 8 3 9 6 8
7 1 2 5 3 2 3 7 8 10
10 9 8 9 7 8 10 7 3 9
6 3 6 2 1 7 10 6 7 2
2 4 8 4 5 9 10 5 9 10
7 4 3 2 4 9 9 9 8 1
Sample Output
Sample Output #1
Sample Output #2
18
26612
Hint
Sample 1說明: 按照下面方式剪紙4次
1 1 1 1
-------------
1 | 1 | 1 | 1
| | |
1 | 1 | 1 | 1
| | |
1 | 1 | 1 | 1
得到6+3+3+3+3=18
posted @ 2009-05-09 14:31 saobaolu| 編輯 收藏
<script language="javascript">
function checkForm(form){
if(isEmpty(form.uid.value)||isEmpty(form.password.value)||isEmpty(form.confirmPassword.value)||isEmpty(form.email.value)||isEmpty(form.quesstion.value)||isEmpty(form.answer.value)||){
alert("請將表單信息填寫完整!");
return false;
}
if(form.password.value!=form.confirmPassword.value){
alert("兩次密碼不相同!");
return false;
}
if(form.password.value.length<6||form.password.value.length>16){
alert("密碼長度不合法!");
return false;
}
return true;
}
function isEmpty(str){
if(str=null||str.length==0)return true;
else return false;
}
</script>
扣錯!
錯在
if(isEmpty(form.uid.value)||isEmpty(form.password.value)||isEmpty(form.confirmPassword.value)||isEmpty(form.email.value)||isEmpty(form.quesstion.value)||isEmpty(form.answer.value)||){
這里錯了:isEmpty(form.answer.value)||){
意思就是無論如何都是會返回一個true的!
怪不得我在form表單里添加這個函數(shù)沒作用,哎,以后還是小心為好啊!
posted @ 2009-05-09 10:22 saobaolu| 編輯 收藏
如何解決?
輸入:
cd /etc/fonts/conf.d/
為了安全,備份一下:
sudo cp 49-sansserif.conf 49-sansserif.conf_backup
輸入如下指令:
sudo gedit ./49-sansserif.conf
此時文件顯示內(nèi)容。
將其中的第1、2、4個后面的sans-serif或者serif用你自己系統(tǒng)中支持中文的字體的名字代替,注意字體名字的大小寫
比如:我的系統(tǒng)中安裝了微軟雅黑,我則用微軟雅黑代替上述所說的字段,結(jié)果如下:
<match target="pattern">
<test qual="all" name="family" compare="not_eq">
<string>宋體</string>
</test>
<test qual="all" name="family" compare="not_eq">
<string>宋體</string>
</test>
<test qual="all" name="family" compare="not_eq">
<string>monospace</string>
</test>
<edit name="family" mode="append_last">
<string>宋體</string>
</edit>
</match>
posted @ 2009-05-06 07:28 saobaolu| 編輯 收藏
文件上傳的時候,報錯

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

正常上傳tomcat的提示是:
gif|jpg|jpeg|bmp|GIF|JPG|JPEG|BMP500
UploadFile/20090505063838906.jpg
異常的時候是:
gif|jpg|jpeg|bmp|GIF|JPG|JPEG|BMP500
UploadFile/
苦惱中…………
posted @ 2009-05-05 19:13 saobaolu| 編輯 收藏
哎,說來真是點背,8.10升級到9.04之后因為顯卡原因不能用,奈何從網(wǎng)上又down一個ubuntu9.04
運行wubi.exe,沒有反應(yīng),運行,還沒有反應(yīng),
while(see the article in cnbeta)
{
success;
break;
}
失敗原因:盤符名中含有中文。
解決方法:將中文改為英文就可以了。如,原先為"系統(tǒng)盤(C:)",改為"system(C:)"。其他也是類似的改法。注意,所有的盤符都不可以含有中文!
可能是做wubi的程序員考慮欠周全所致,希望以后的版本不會再這樣。
如果你已經(jīng)雙擊了wubi,應(yīng)先清除臨時文件,路徑為C:\Documents and Settings\Administrator\Local Settings\Temp(假設(shè)你的系統(tǒng)盤是C盤,并且是Administrator)。清除后,為了保險起見,重啟后再運行wubi。
via cnbeta~
posted @ 2009-05-02 12:30 saobaolu| 編輯 收藏
Redwood Shores, CA - April 20, 2009
Oracle Corporation (NASDAQ: ORCL) and Sun Microsystems (NASDAQ: JAVA) announced today they have entered into a definitive agreement under which Oracle will acquire Sun common stock for $9.50 per share in cash. The transaction is valued at approximately $7.4 billion, or $5.6 billion net of Sun’s cash and debt. “We expect this acquisition to be accretive to Oracle’s earnings by at least 15 cents on a non-GAAP basis in the first full year after closing. We estimate that the acquired business will contribute over $1.5 billion to Oracle’s non-GAAP operating profit in the first year, increasing to over $2 billion in the second year. This would make the Sun acquisition more profitable in per share contribution in the first year than we had planned for the acquisitions of BEA, PeopleSoft and Siebel combined,” said Oracle President Safra Catz.
“The acquisition of Sun transforms the IT industry, combining best-in-class enterprise software and mission-critical computing systems,” said Oracle CEO Larry Ellison. “Oracle will be the only company that can engineer an integrated system – applications to disk – where all the pieces fit and work together so customers do not have to do it themselves. Our customers benefit as their systems integration costs go down while system performance, reliability and security go up.”
There are substantial long-term strategic customer advantages to Oracle owning two key Sun software assets: Java and Solaris. Java is one of the computer industry’s best-known brands and most widely deployed technologies, and it is the most important software Oracle has ever acquired. Oracle Fusion Middleware, Oracle’s fastest growing business, is built on top of Sun’s Java language and software. Oracle can now ensure continued innovation and investment in Java technology for the benefit of customers and the Java community.
The Sun Solaris operating system is the leading platform for the Oracle database, Oracle’s largest business, and has been for a long time. With the acquisition of Sun, Oracle can optimize the Oracle database for some of the unique, high-end features of Solaris. Oracle is as committed as ever to Linux and other open platforms and will continue to support and enhance our strong industry partnerships.
“Oracle and Sun have been industry pioneers and close partners for more than 20 years,” said Sun Chairman Scott McNealy. “This combination is a natural evolution of our relationship and will be an industry-defining event.”
“This is a fantastic day for Sun’s customers, developers, partners and employees across the globe, joining forces with the global leader in enterprise software to drive innovation and value across every aspect of the technology marketplace,” said Jonathan Schwartz, Sun’s CEO, “From the Java platform touching nearly every business system on earth, powering billions of consumers on mobile handsets and consumer electronics, to the convergence of storage, networking and computing driven by the Solaris operating system and Sun’s SPARC and x64 systems. Together with Oracle, we’ll drive the innovation pipeline to create compelling value to our customer base and the marketplace.”
“Sun is a pioneer in enterprise computing, and this combination recognizes the innovation and customer success the company has achieved. Our largest customers have been asking us to step up to a broader role to reduce complexity, risk and cost by delivering a highly optimized stack based on standards,” said Oracle President Charles Phillips. “This transaction will preserve and enhance investments made by our customers, while we continue to work with our partners to provide customers with choice.”
The Board of Directors of Sun Microsystems has unanimously approved the transaction. It is anticipated to close this summer, subject to Sun stockholder approval, certain regulatory approvals and customary closing conditions.
There will be a conference call today to discuss the transaction at 5:30 a.m. Pacific time. Investors can listen to the conference call by dialing (719) 234-7870, passcode 923645. A replay will be available for 24 hours after the call ends at (719) 884-8882, passcode: 923645. A live audio webcast of the call will be made available at www.oracle.com/investor and a replay will be available for seven days after the call ends.
About Sun Microsystems
Sun Microsystems, Inc. (NASDAQ: JAVA) develops the technologies that power the global marketplace. Guided by a singular vision -- "The Network is the Computer" -- Sun drives network participation through shared innovation, community development and open source leadership. Sun can be found in more than 100 countries and on the Web at http://www.sun.com.
About Oracle
Oracle (NASDAQ: ORCL) is the world's largest enterprise software company. For more information about Oracle, please visit our Web site at http://www.oracle.com.
Trademarks
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Sun, Sun Microsystems, the Sun logo, Java, Solaris and the Network is the Computer are trademarks or registered trademarks of Sun Microsystems, Inc. or its subsidiaries in the United States and other countries
Cautionary Statement Regarding Forward-Looking Statements
This press release contains certain forward-looking statements about Oracle and Sun, including statements that involve risks and uncertainties concerning Oracle's proposed acquisition of Sun, anticipated product information, estimates of future results of operations and general business outlook. When used in this press release, the words "anticipates", “estimates”, "may", "can", “will”, "believes", "expects", "projects", "intends", "likely", similar expressions and any other statements that are not historical facts are intended to identify those assertions as forward-looking statements. Any such statement may be influenced by a variety of factors, many of which are beyond the control of Oracle or Sun, that could cause actual outcomes and results to be materially different from those projected, described, expressed or implied in this press release due to a number of risks and uncertainties. Potential risks and uncertainties include, among others, the possibility that the transaction will not close or that the closing may be delayed, the anticipated synergies of the combined companies may not be achieved after closing, the combined operations may not be successfully integrated in a timely manner, if at all, general economic conditions in regions in
which either company does business, and the possibility that Oracle or Sun may be adversely affected by other economic, business, and/or competitive factors. Accordingly, no assurances can be given that any of the events anticipated by the forward-looking statements will transpire or occur, or if any of them do so, what impact they will have on the results of operations or financial condition of Oracle or Sun.
In addition, please refer to the documents that Oracle and Sun, respectively, file with the Securities and Exchange Commission (the “SEC”) on Forms 10-K, 10-Q and 8-K. These filings identify and address other important factors that could cause Oracle's and Sun's respective financial and operational results to differ materially from those contained in the forward-looking statements set forth in this document. You are cautioned to not place undue reliance on forward-looking statements, which speak only as of the date of this report. Neither Oracle nor Sun is under any duty to update any of the information in this release.
Additional Information about the Merger and Where to Find It
In connection with the proposed merger, Sun will file a proxy statement with the SEC. Additionally, Sun and Oracle will file other relevant materials in connection with the proposed acquisition of Sun by Oracle pursuant to the terms of an Agreement and Plan of Merger by and among Oracle, Soda Acquisition Corporation, a wholly-owned subsidiary of Oracle, and Sun. The materials to be filed by Sun with the SEC may be obtained free of charge at the SEC's web site at www.sec.gov. Investors and security holders of Sun are urged to read the proxy statement and the other relevant materials when they become available before making any voting or investment decision with respect to the proposed merger because they will contain important information about the merger and the parties to the merger.
Oracle, Sun and their respective directors, executive officers and other members of its management and employees, under SEC rules, may be deemed to be participants in the solicitation of proxies of Sun stockholders in connection with the proposed merger. Investors and security holders may obtain more detailed information regarding the names, affiliations and interests of certain of Oracle's executive officers and directors in the solicitation by reading the proxy statement and other relevant materials filed with the SEC when they become available. Information concerning the interests of Sun's participants in the solicitation, which may, in some cases, be different than those of Sun's stockholders generally, is set forth in the materials filed with the SEC on Form 10-K and will be set forth in the proxy statement relating to the merger when it becomes available.
Contact Info
Karen Tillman
Oracle Corporate Communications
+1.650.607.0326
karen.tillman@oracle.com
Roy Lobo
Oracle Investor Relations
+1.650.506.4073
investor_us@oracle.com
posted @ 2009-04-21 18:08 saobaolu| 編輯 收藏
posted @ 2009-04-17 03:13 saobaolu| 編輯 收藏
posted @ 2009-04-05 14:06 saobaolu| 編輯 收藏
一、概要
Java 2 Platform, Enterprise Edition (J2EE)是當(dāng)前很多商業(yè)應(yīng)用系統(tǒng)使用的開發(fā)平臺,該技術(shù)提供了一個基于組件的方法來設(shè)計、開發(fā)、裝配和部署企業(yè)級應(yīng)用程序。J2EE平臺提供了一個多層結(jié)構(gòu)的分布式的應(yīng)用程序模型,可以更快地開發(fā)和發(fā)布的新的應(yīng)用解決方案。
J2EE是一種技術(shù)規(guī)范,定義了整個標(biāo)準(zhǔn)的應(yīng)用開發(fā)體系結(jié)構(gòu)和一個部署環(huán)境,應(yīng)用開發(fā)者開發(fā)時只要專注于具體商業(yè)邏輯和商業(yè)業(yè)務(wù)規(guī)則的實現(xiàn)上,而其他的諸如事務(wù)、持久化、安全等系統(tǒng)開發(fā)問題可以由應(yīng)用程序容器或者服務(wù)器處理,開發(fā)完成后,就可以方便地部署到實現(xiàn)規(guī)范的應(yīng)用服務(wù)器中。
作為網(wǎng)絡(luò)上的商業(yè)應(yīng)用系統(tǒng),同時訪問的人數(shù)是很多的,在大量訪問的情況下,過多的資源請求和有限的服務(wù)器資源(內(nèi)存、CPU時間、網(wǎng)絡(luò)帶寬等)之間就會出現(xiàn)矛盾,應(yīng)用系統(tǒng)的性能就顯得很重要了,有時正確的代碼并不能保證項目的成功,性能往往是最后決定一個項目是否成功關(guān)鍵。
本文主要從性能的角度出發(fā),討論J2EE服務(wù)器端的代碼性能優(yōu)化和提升。
二、常見的Java 編程 J2EE語言基礎(chǔ)是Java,常用的Java代碼問題對應(yīng)用系統(tǒng)的性能影響,
下面討論了一些應(yīng)該注意方面。
使用StringBuffer代替String 當(dāng)處理字符串的相加時,常見的寫法是:
String str1 = "Hello";
String str2 = "welcome to world";
String str3 = str1 + ", " + str2 +"!";
System.out.println(str3);
很多人都知道,這樣的代碼效率是很低的,因為String是用來存儲字符串常量的,如果要執(zhí)行“+”的操作,系統(tǒng)會生成一些臨時的對象,并對這些對象進行管理,造成不必要的開銷。String str2 = "welcome to world";
String str3 = str1 + ", " + str2 +"!";
System.out.println(str3);
如果字符串有連接的操作,替代的做法是用StringBuffer類的append方法,它的缺省構(gòu)造函數(shù)和append的實現(xiàn)是:
public StringBuffer() { // 構(gòu)造函數(shù) 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; } |
當(dāng)字符串的大小超過缺省16時,代碼實現(xiàn)了容量的擴充,為了避免對象的重新擴展其容量,更好的寫法為:
StringBuffer buffer = new StringBuffer(30); // 分配指定的大小。 buffer.append("hello"); buffer.append(","); buffer.append("welcometo world!"); String str = buffer.toString(); |
·生成對象時,分配合理的空間和大小
Java中的很多類都有它的默認的空間分配大小,對于一些有大小的對象的初始化,應(yīng)該預(yù)計對象的大小,然后使用進行初始化,上面的例子也說明了這個問題,StringBuffer創(chuàng)建時,我們指定了它的大小。
另外的一個例子是Vector,當(dāng)聲明Vector vect=new Vector()時,系統(tǒng)調(diào)用:
public Vector() {// 缺省構(gòu)造函數(shù) this(10); // 容量是 10; } |
缺省分配10個對象大小容量。當(dāng)執(zhí)行add方法時,可以看到具體實現(xiàn)為:..
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); } } |
我們可以看到,當(dāng)Vector大小超過原來的大小時,一些代碼的目的就是為了做容量的擴充,在預(yù)先知道該Vector大小的話,可以指定其大小,避免容量擴充的開銷,如知道Vector大小為100時,初始化是就可以象這樣。
Vector vect =.. new Vector(100); |
·優(yōu)化循環(huán)體
循環(huán)是比較重復(fù)運行的地方,如果循環(huán)次數(shù)很大,循環(huán)體內(nèi)不好的代碼對效率的影響就會被放大而變的突出。考慮下面的代碼片:..
Vector vect = new Vector(1000); ... for( inti=0; i<vect.size(); i++){ ... } |
for循環(huán)部分改寫成:
int size = vect.size(); for( int i=0; i>size; i++){ ... } |
如果size=1000,就可以減少1000次size()的系統(tǒng)調(diào)用開銷,避免了循環(huán)體重復(fù)調(diào)用。
再看如下的代碼片:..
for (int i = 0;i <100000;i++) if (i%10 == 9) { ... // 每十次執(zhí)行一次 } |
改寫成也可以提高效率:..
for(inti =0,j =10; i<100000; i++,j--){ if(j == 0){ ... // 每十次執(zhí)行一次 j = 10; } } |
所以,當(dāng)有較大的循環(huán)時,應(yīng)該檢查循環(huán)內(nèi)是否有效率不高的地方,尋找更優(yōu)的方案加以改進。
·對象的創(chuàng)建
盡量少用new來初始化一個類的實例,當(dāng)一個對象是用new進行初始化時,其構(gòu)造函數(shù)鏈的所有構(gòu)造函數(shù)都被調(diào)用到,所以new操作符是很消耗系統(tǒng)資源的,new一個對象耗時往往是局部變量賦值耗時的上千倍。同時,當(dāng)生成對象后,系統(tǒng)還要花時間進行垃圾回收和處理。
當(dāng)new創(chuàng)建對象不可避免時,注意避免多次的使用new初始化一個對象。
盡量在使用時再創(chuàng)建該對象。如:
NewObject object = new NewObject(); int value; if(i>0 ) { value =object.getValue(); } |
可以修改為:
int value; if(i>0 ) { NewObject object = new NewObject(); Value =object.getValue(); } |
另外,應(yīng)該盡量重復(fù)使用一個對象,而不是聲明新的同類對象。一個重用對象的方法是改變對象的值,如可以通過setValue之類的方法改變對象的變量達到重用的目的。
·變量的注意事項
盡量使用局部變量,調(diào)用方法時傳遞的參數(shù)以及在調(diào)用中創(chuàng)建的臨時變量都保存在棧(Stack) 中,速度較快。其他變量,如靜態(tài)變量、實例變量等,都在堆(Heap)中創(chuàng)建,速度較慢。
盡量使用靜態(tài)變量,即加修飾符static,如果類中的變量不會隨他的實例而變化,就可以定義為靜態(tài)變量,從而使他所有的實例都共享這個變量。
·方法(Method)調(diào)用
在Java中,一切都是對象,如果有方法(Method)調(diào)用,處理器先要檢查該方法是屬于哪個對象,該對象是否有效,對象屬于什么類型,然后選擇合適的方法并調(diào)用。
可以減少方法的調(diào)用,同樣一個方法:
public void CallMethod(int i ){ if( i ==0 ){ return; } ... // 其他處理 } |
如果直接調(diào)用,
int i = 0; ... CallMethod(i); |
就不如寫成:
int i = 0; ... if( i ==0 ){ CallMethod(i); } |
不影響可讀性等情況下,可以把幾個小的方法合成一個大的方法。
另外,在方法前加上final,private關(guān)鍵字有利于編譯器的優(yōu)化。
·慎用異常處理
異常是Java的一種錯誤處理機制,對程序來說是非常有用的,但是異常對性能不利。拋出異常首先要創(chuàng)建一個新的對象,并進行相關(guān)的處理,造成系統(tǒng)的開銷,所以異常應(yīng)該用在錯誤處理的情況,不應(yīng)該用來控制程序流程,流程盡量用while,if等處理。
在不是很影響代碼健壯性的前提下,可以把幾個try/catch塊合成一個。
·同步
同步主要出現(xiàn)在多線程的情況,為多線程同時運行時提供對象數(shù)據(jù)安全的機制,多線程是比較復(fù)雜話題,應(yīng)用多線程也是為了獲得性能的提升,應(yīng)該盡可能減少同步。
另外,如果需要同步的地方,可以減少同步的代碼段,如只同步某個方法或函數(shù),而不是整個代碼。
·使用Java系統(tǒng)API
Java的API一般都做了性能的考慮,如果完成相同的功能,優(yōu)先使用API而不是自己寫的代碼,如數(shù)組復(fù)制通常的代碼如下:
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++){ // 復(fù)制 strArray2[i]=(new String((String)a[i])); } |
如果使用Java提供的API,就可以提高性能:
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); // 復(fù)制 |
同樣的一個規(guī)則是,當(dāng)有大量數(shù)據(jù)的復(fù)制時,應(yīng)該使用System.arraycopy()。
輸入/輸出(I/O)包括很多方面,我們知道,進行I/O操作是很費系統(tǒng)資源的。程序中應(yīng)該盡量少用I/O操作。使用時可以注意: . 合理控制輸出函數(shù)System.out.println()對于大多時候是有用的,特別是系統(tǒng)調(diào)試的時候,但也會產(chǎn)生大量的信息出現(xiàn)在控制臺和日志上,同時輸出時,有序列化和同步的過程,造成了開銷。
特別是在發(fā)行版中,要合理的控制輸出,可以在項目開發(fā)時,設(shè)計好一個Debug的工具類,在該類中可以實現(xiàn)輸出開關(guān),輸出的級別,根據(jù)不同的情況進行不同的輸出的控制。
·使用緩存
讀寫內(nèi)存要比讀寫文件要快很多,應(yīng)盡可能使用緩沖。
盡可能使用帶有Buffer的類代替沒有Buffer的類,如可以用BufferedReader 代替Reader,用BufferedWriter代替Writer來進行處理I/O操作。
同樣可以用BufferedInputStream代替InputStream都可以獲得性能的提高。
四、Servlet
Servlet采用請求——響應(yīng)模式提供Web服務(wù),通過ServletResponse以及ServletRequest這兩個對象來輸出和接收用戶傳遞的參數(shù),在服務(wù)器端處理用戶的請求,根據(jù)請求訪問數(shù)據(jù)庫、訪問別的Servlet方法、調(diào)用EJB等等,然后將處理結(jié)果返回給客戶端。
·盡量不使用同步
Servlet是多線程的,以處理不同的請求,基于前面同步的分析,如果有太多的同步就失去了多線程的優(yōu)勢了。
·不用保存太多的信息在HttpSession中
很多時候,存儲一些對象在HttpSession中是有必要的,可以加快系統(tǒng)的開發(fā),如網(wǎng)上商店系統(tǒng)會把購物車信息保存在該用戶的Session中,但當(dāng)存儲大量的信息或是大的對象在會話中是有害的,特別是當(dāng)系統(tǒng)中用戶的訪問量很大,對內(nèi)存的需求就會很高。
具體開發(fā)時,在這兩者之間應(yīng)作好權(quán)衡。
·清除Session
通常情況,當(dāng)達到設(shè)定的超時時間時,同時有些Session沒有了活動,服務(wù)器會釋放這些沒有活動的Session,.. 不過這種情況下,特別是多用戶并訪時,系統(tǒng)內(nèi)存要維護多個的無效Session。
當(dāng)用戶退出時,應(yīng)該手動釋放,回收資源,實現(xiàn)如下:..
HttpSession theSession = request.getSession(); // 獲取當(dāng)前Session if(theSession != null){ theSession.invalidate(); // 使該Session失效 } |
五、EJB 問題
EJB是Java服務(wù)器端服務(wù)框架的規(guī)范,軟件廠商根據(jù)它來實現(xiàn)EJB服務(wù)器。應(yīng)用程序開發(fā)者可以專注于支持應(yīng)用所需的商業(yè)邏輯,而不用擔(dān)心周圍框架的實現(xiàn)問題。EJB規(guī)范詳細地解釋了一些最小但是必須的服務(wù),如事務(wù),安全和名字等。
·緩存Home接口
EJB庫使用Enterprise Bean 的客戶端通過它的Home接口創(chuàng)建它的實例。客戶端能通過JNDI訪問它。服務(wù)器通過Lookup方法來獲取。
JNDI是個遠程對象,通過RMI方式調(diào)用,對它的訪問往往是比較費時的。所以,在設(shè)計時可以設(shè)計一個類專門用來緩存Home接口,在系統(tǒng)初始化時就獲得需要的Home接口并緩存,以后的引用只要引用緩存即可。
·封裝Entity Bean
直接訪問Entity Bean是個不好的習(xí)慣,用會話Bean封裝對實體Bean的訪問能夠改進事務(wù)管理,因為每一個對get方法的直接調(diào)用將產(chǎn)生一個事務(wù),容器將在每一個實體Bean的事務(wù)之后執(zhí)行一個“Load-Store”.. 操作。
最好在Session Bean中完成Entity Bean的封裝,減少容器的事務(wù)處理,并在Session Bean中實現(xiàn)一些具體的業(yè)務(wù)方法。
·釋放有狀態(tài)的Session Bean
相當(dāng)于HttpSession,當(dāng)把一個Session Bean設(shè)為Stateful,即有狀態(tài)的Session Bean 后,應(yīng)用容器(Container)就可能有“鈍化”(Passivate)和活化(Activate)過程,即在主存和二級緩存之間對SessionBean進行存儲位置的轉(zhuǎn)移,在這個過程中,存在序列化過程。
通常有狀態(tài)Session Bean的釋放是在超時時發(fā)生,容器自動的清除該對象,但是如果交給容器管理,一方面可能產(chǎn)生對象鈍化,另一方面未超時期間,系統(tǒng)還要 維護一份該對象,所以如果我們確認使用完該StatefulSession Bean后不再需要時,可以顯式的將其釋放掉,方法是調(diào)用:
theSesionBean.remove(); |
六、數(shù)據(jù)庫訪問
在J2EE開發(fā)的應(yīng)用系統(tǒng)中,數(shù)據(jù)庫訪問一般是個必備的環(huán)節(jié)。數(shù)據(jù)庫用來存儲業(yè)務(wù)數(shù)據(jù),供應(yīng)用程序訪問。
在Java技術(shù)的應(yīng)用體系中,應(yīng)用程序是通過JDBC(Java Database Connectivity)實現(xiàn)的接口來訪問數(shù)據(jù)庫的,JDBC支持“建立連接、SQL語句查詢、處理結(jié)果”等基本功能。在應(yīng)用JDBC接口訪問數(shù)據(jù)庫的過程中,只要根據(jù)規(guī)范來實現(xiàn),就可以達到要求的功能。
但是,有些時候進行數(shù)據(jù)查詢的效率著實讓開發(fā)人員不如所愿,明明根據(jù)規(guī)范編寫的程序,運行效果卻很差,造成整個系統(tǒng)的執(zhí)行效率不高。
·使用速度快的JDBC驅(qū)動
JDBC API包括兩種實現(xiàn)接口形式,一種是純Java實現(xiàn)的驅(qū)動,一種利用ODBC驅(qū)動和數(shù)據(jù)庫客戶端實現(xiàn),具體有四種驅(qū)動模式并各有不同的應(yīng)用范圍,針對不同的應(yīng)用開發(fā)要選擇合適的JDBC驅(qū)動,在同一個應(yīng)用系統(tǒng)中,如果選擇不同的JDBC驅(qū)動,在效率上會有差別。
例如,有一個企業(yè)應(yīng)用系統(tǒng),不要求支持不同廠商的數(shù)據(jù)庫,這時就可以選擇模式4的JDBC驅(qū)動,該驅(qū)動一般由數(shù)據(jù)庫廠商實現(xiàn)的基于本地協(xié)議的驅(qū)動,直接調(diào)用數(shù)據(jù)庫管理系統(tǒng)使用的協(xié)議,減少了模式3中的中間層。
·使用JDBC連接池
為了提高訪問數(shù)據(jù)庫的性能,我們還可以使用JDBC 2.0的一些規(guī)范和特性,JDBC是占用資源的,在使用數(shù)據(jù)庫連接時可以使用連接池Connection Pooling,避免頻繁打開、關(guān)閉Connection。而我們知道,獲取Connection是比較消耗系統(tǒng)資源的。
Connection緩沖池是這樣工作的:當(dāng)一個應(yīng)用程序關(guān)閉一個數(shù)據(jù)庫連接時,這個連接并不真正釋放而是被循環(huán)利用,建立連接是消耗較大的操作,循環(huán)利用連接可以顯著的提高性能,因為可以減少新連接的建立。
一個通過DataSource獲取緩沖池獲得連接,并連接到一個CustomerDB數(shù)據(jù)源的代碼演示如下:
Context ctx = new InitialContext(); DataSource dataSource = (DataSource) ctx.lookup("jdbc/CustomerDB"); Connection conn = dataSource.getConnection("password","username"); |
·緩存DataSource
一個DataSource對象代表一個實際的數(shù)據(jù)源。這個數(shù)據(jù)源可以是從關(guān)系數(shù)據(jù)庫到表格形式的文件,完全依賴于它是怎樣實現(xiàn)的,一個數(shù)據(jù)源對象注冊到JNDI名字服務(wù)后,應(yīng)用程序就可以從JNDI服務(wù)器上取得該對象,并使用之和數(shù)據(jù)源建立連接。
通過上面的例子,我們知道DataSource是從連接池獲得連接的一種方式,通過JNDI方式獲得,是占用資源的。
為了避免再次的JNDI調(diào)用,可以系統(tǒng)中緩存要使用的DataSource。
·關(guān)閉所有使用的資源
系統(tǒng)一般是并發(fā)的系統(tǒng),在每次申請和使用完資源后,應(yīng)該釋放供別人使用,數(shù)據(jù)庫資源每個模式的含義可以參考SUN JDBC的文檔,不同是比較寶貴的,使用完成后應(yīng)該保證徹底的釋放。
請看下面的代碼段:
Connection conn = null; Statement stmt = null; ResultSet rs = null; try { DataSource dataSource = getDataSource(); // 取的DataSource的方法,實現(xiàn)略。 conn = datasource.getConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM ..."); ... // 其他處理 rs.close(); stmt.close(); conn.close(); }catch (SQLException ex) { ... // 錯誤處理 } |
粗看似乎沒有什么問題,也有關(guān)閉相關(guān)如Connection等系統(tǒng)資源的代碼,但當(dāng)出現(xiàn)異常后,關(guān)閉資源的代碼可能并不被執(zhí)行,為保證資源的確實已被關(guān)閉,應(yīng)該把資源關(guān)閉的代碼放到finally塊:
Connection conn = null; Statement stmt = null; ResultSet rs = null; try { DataSource dataSource = getDataSource(); // 取的DataSource的方法,實現(xiàn)略。 conn = datasource.getConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM ..."); ... // 其他處理 }catch (SQLException ex) { ... // 錯誤處理 }finally{ if (rs!=null) { try { rs.close(); // 關(guān)閉ResultSet} catch (SQLException ex) { ... // 錯誤處理 } } if (stmt!=null){ try { stmt.close(); // 關(guān)閉Statement} catch (SQLException ex) { ... // 錯誤處理 } } if (conn!=null){ try { conn.close(); // 關(guān)閉Connection} catch (SQLException ex) { ... // 錯誤處理 } } } |
·大型數(shù)據(jù)量處理
當(dāng)我們在讀取諸如數(shù)據(jù)列表、報表等大量數(shù)據(jù)時,可以發(fā)現(xiàn)使用EJB的方法是非常慢的,這時可以使用直接訪問數(shù)據(jù)庫的方法,用SQL直接存取數(shù)據(jù),從而消除EJB的經(jīng)常開支(例如遠程方法調(diào)用、事務(wù)管理和數(shù)據(jù)序列化,對象的構(gòu)造等)。
·緩存經(jīng)常使用的數(shù)據(jù)
對于構(gòu)建的業(yè)務(wù)系統(tǒng),如果有些數(shù)據(jù)要經(jīng)常要從數(shù)據(jù)庫中讀取,同時,這些數(shù)據(jù)又不經(jīng)常變化,這些數(shù)據(jù)就可以在系統(tǒng)中緩存起來,使用時直接讀取緩存,而不用頻繁的訪問數(shù)據(jù)庫讀取數(shù)據(jù)。
緩存工作可以在系統(tǒng)初始化時一次性讀取數(shù)據(jù),特別是一些只讀的數(shù)據(jù),當(dāng)數(shù)據(jù)更新時更新數(shù)據(jù)庫內(nèi)容,同時更新緩存的數(shù)據(jù)值。
一個例子是,在一套企業(yè)應(yīng)用系統(tǒng)中,企業(yè)的信息數(shù)據(jù)(如企業(yè)的名稱)在多個業(yè)務(wù)應(yīng)用模塊中使用,這時就可以把這些數(shù)據(jù)緩存起來,需要時直接讀取緩存的企業(yè)信息數(shù)據(jù)。
七、總結(jié)
一般意義上說,參與系統(tǒng)運行的代碼都會對性能產(chǎn)生影響,實際應(yīng)用中應(yīng)該養(yǎng)成良好的編程規(guī)范、編寫高質(zhì)量的代碼,當(dāng)系統(tǒng)性能出現(xiàn)問題時,要找到主要影響性能的瓶頸所在,然后集中精力優(yōu)化這些代碼,能達到事半功倍的效果。
J2EE性能的優(yōu)化包括很多方面的,要達到一個性能優(yōu)良的系統(tǒng),除了關(guān)注代碼之外,還應(yīng)該根據(jù)系統(tǒng)實際的運行情況,從服務(wù)器軟硬件環(huán)境、集群技術(shù)、系統(tǒng)構(gòu)架設(shè)計、系統(tǒng)部署環(huán)境、數(shù)據(jù)結(jié)構(gòu)、算法設(shè)計等方面綜合考慮。
posted @ 2009-04-04 09:16 saobaolu| 編輯 收藏
posted @ 2009-04-03 17:07 saobaolu| 編輯 收藏
posted @ 2009-04-01 18:14 saobaolu| 編輯 收藏