.异常(Exceptions)
JAVA语言中提供了try/catch来发方便用户捕捉异常,进行异常的处理。但是如果使用不当,也会给JAVA程序的性能带来影响。因此,要注意以下两点。
(1) 避免对应用程序的逻辑使用try/catch
如果可以用if,while等逻辑语句来处理,那么就尽可能的不用try/catch语句
(2) 重用异常
在必须要进行异常的处理时,要尽可能的重用已经存在的异常对象。以为在异常的处理中,生成一个异常对象要消耗掉大部分的时间。
3. 线程(Threading)
一个高性能的应用程序中一般都会用到线程。因为线程能充分利用系统的资源。在其他线程因为等待硬盘或网络读写而 时,程序能继续处理和运行。但是对线程运用不当,也会影响程序的性能。
例2:正确使用Vector类
Vector主要用来保存各种类型的对象(包括相同类型和不同类型的对象)。但是在一些情况下使用会给程序带来性能上的影响。这主要是由Vector类的两个特点所决定的。第一,Vector提供了线程的 安全保护功能。即使Vector类中的许多方法同步。但是如果你已经确认你的应用程序是单线程,这些方法的同步就完全不必要了。第二,在Vector查找存储的各种对象时,常常要花很多的时间进行类型的匹配。而当这些对象都是同一类型时,这些匹配就完全不必要了。因此,有必要 设计一个单线程的,保存特定类型对象的类或集合来替代Vector类.用来替换的程序如下(StringVector.java):
public class StringVector
{
private String[] data;
private int count;
public StringVector() { this(10); // default size is 10 }
public StringVector(int initialSize)
{
data = new String[initialSize];
}
public void add(String str)
{
// ignore null strings
if(str == null) { return; }
ensureCapacity(count + 1);
data[count++] = str;
}
private void ensureCapacity(int minCapacity)
{
int oldCapacity = data.length;
if (minCapacity > oldCapacity)
{
String oldData[] = data;
int newCapacity = oldCapacity * 2;
data = new String[newCapacity];
System.arraycopy(oldData, 0, data, 0, count);
}
}
public void remove(String str)
{
if(str == null) { return // ignore null str }
for(int i = 0; i < count; i++)
{
// check for a match
if(data[i].equals(str))
{
System.arraycopy(data,i+1,data,i,count-1); // copy data
// allow previously valid array element be gc'd
data[--count] = null;
return;
}
}
}
public final String getStringAt(int index) {
if(index < 0) { return null; }
else if(index > count)
{
return null; // index is > # strings
}
else { return data[index]; // index is good }
}
/* * * * * * * * * * * * * * * *StringVector.java * * * * * * * * * * * * * * * * */
因此,代码:
Vector Strings=new Vector();
Strings.add(“One”);
Strings.add(“Two”);
String Second=(String)Strings.elementAt(1);
可以用如下的代码替换:
StringVector Strings=new StringVector();
Strings.add(“One”);
Strings.add(“Two”);
String Second=Strings.getStringAt(1);
这样就可以通过优化线程来提高JAVA程序的性能。用于测试的程序如下(TestCollection.java):
import java.util.Vector;
public class TestCollection
{
public static void main(String args [])
{
TestCollection collect = new TestCollection();
if(args.length == 0)
{
System.out.println(
"Usage: java TestCollection [ vector | stringvector ]");
System.exit(1);
}
if(args[0].equals("vector"))
{
Vector store = new Vector();
long start = System.currentTimeMillis();
JAVA语言中提供了try/catch来发方便用户捕捉异常,进行异常的处理。但是如果使用不当,也会给JAVA程序的性能带来影响。因此,要注意以下两点。
(1) 避免对应用程序的逻辑使用try/catch
如果可以用if,while等逻辑语句来处理,那么就尽可能的不用try/catch语句
(2) 重用异常
在必须要进行异常的处理时,要尽可能的重用已经存在的异常对象。以为在异常的处理中,生成一个异常对象要消耗掉大部分的时间。
3. 线程(Threading)
一个高性能的应用程序中一般都会用到线程。因为线程能充分利用系统的资源。在其他线程因为等待硬盘或网络读写而 时,程序能继续处理和运行。但是对线程运用不当,也会影响程序的性能。
例2:正确使用Vector类
Vector主要用来保存各种类型的对象(包括相同类型和不同类型的对象)。但是在一些情况下使用会给程序带来性能上的影响。这主要是由Vector类的两个特点所决定的。第一,Vector提供了线程的 安全保护功能。即使Vector类中的许多方法同步。但是如果你已经确认你的应用程序是单线程,这些方法的同步就完全不必要了。第二,在Vector查找存储的各种对象时,常常要花很多的时间进行类型的匹配。而当这些对象都是同一类型时,这些匹配就完全不必要了。因此,有必要 设计一个单线程的,保存特定类型对象的类或集合来替代Vector类.用来替换的程序如下(StringVector.java):
public class StringVector
{
private String[] data;
private int count;
public StringVector() { this(10); // default size is 10 }
public StringVector(int initialSize)
{
data = new String[initialSize];
}
public void add(String str)
{
// ignore null strings
if(str == null) { return; }
ensureCapacity(count + 1);
data[count++] = str;
}
private void ensureCapacity(int minCapacity)
{
int oldCapacity = data.length;
if (minCapacity > oldCapacity)
{
String oldData[] = data;
int newCapacity = oldCapacity * 2;
data = new String[newCapacity];
System.arraycopy(oldData, 0, data, 0, count);
}
}
public void remove(String str)
{
if(str == null) { return // ignore null str }
for(int i = 0; i < count; i++)
{
// check for a match
if(data[i].equals(str))
{
System.arraycopy(data,i+1,data,i,count-1); // copy data
// allow previously valid array element be gc'd
data[--count] = null;
return;
}
}
}
public final String getStringAt(int index) {
if(index < 0) { return null; }
else if(index > count)
{
return null; // index is > # strings
}
else { return data[index]; // index is good }
}
/* * * * * * * * * * * * * * * *StringVector.java * * * * * * * * * * * * * * * * */
因此,代码:
Vector Strings=new Vector();
Strings.add(“One”);
Strings.add(“Two”);
String Second=(String)Strings.elementAt(1);
可以用如下的代码替换:
StringVector Strings=new StringVector();
Strings.add(“One”);
Strings.add(“Two”);
String Second=Strings.getStringAt(1);
这样就可以通过优化线程来提高JAVA程序的性能。用于测试的程序如下(TestCollection.java):
import java.util.Vector;
public class TestCollection
{
public static void main(String args [])
{
TestCollection collect = new TestCollection();
if(args.length == 0)
{
System.out.println(
"Usage: java TestCollection [ vector | stringvector ]");
System.exit(1);
}
if(args[0].equals("vector"))
{
Vector store = new Vector();
long start = System.currentTimeMillis();