java 优化之一:Buffer的使用

1.在java I/O 中,Buffer的使用,使用buffer,文件的读写效率提高一倍左右

public void writeFile() {
try {
FileWriter fw = new FileWriter("file.txt");

long beginTime = System.currentTimeMillis();

for(int i = 0; i < 10000; i++) {
fw.write("testline " + i + "\n");
}

fw.close();

System.out.println("Time spend: " + (System.currentTimeMillis() - beginTime));
} catch (IOException e) {
e.printStackTrace();
}
}

public void writeWithBuffer() {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt"));
long beginTime = System.currentTimeMillis();

for(int i = 0; i < 10000; i++) {
bw.write("testline " + i + "\n");
}

bw.close();

System.out.println("Time spend: " + (System.currentTimeMillis() - beginTime));
} catch (IOException e) {
e.printStackTrace();
}
}



2.对象的缓存

1)无缓存实现
public class NoBufferMovingCircle extends JApplet implements Runnable {
private static final long serialVersionUID = -1081283643259866574L;

Image screenImage = null;
Thread thread;
int x = 5;
int move = 1;

@Override
public void init() {
super.init();
screenImage = createImage(230, 160);
}

@Override
public void start() {
super.start();
if(thread == null) {
thread = new Thread(this);
thread.start();
}
}
@Override
public void run() {
try {
while(true) {
x += move;
if((x > 105) || (x < 5)) {
move *= -1;
}
repaint();
Thread.sleep(10);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void drawDircle(Graphics draw) {
Graphics2D g = (Graphics2D)draw;
g.setColor(Color.red);
g.fillRect(0, 0, 200, 100);
g.setColor(Color.GREEN);
g.fillOval(x, 5, 90, 90);
}

@Override
public void paint(Graphics g) {
g.setColor(Color.GRAY);
g.fillRect(0, 0, 200, 100);
drawDircle(g);
}
}

2)有缓存实现
对Graphics对象缓存后applet图片显示顺畅
public class BufferMovingCircle extends JApplet implements Runnable {
private static final long serialVersionUID = -1081283643259866574L;

Image screenImage = null;
Thread thread;
int x = 5;
int move = 1;
Graphics doubleBuffer = null;

@Override
public void init() {
super.init();
screenImage = createImage(230, 160);
doubleBuffer = screenImage.getGraphics();
}

@Override
public void start() {
super.start();
if(thread == null) {
thread = new Thread(this);
thread.start();
}
}
@Override
public void run() {
try {
while(true) {
x += move;
if((x > 105) || (x < 5)) {
move *= -1;
}
repaint();
Thread.sleep(10);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void drawDircle(Graphics draw) {
Graphics2D g = (Graphics2D)draw;
g.setColor(Color.red);
g.fillRect(0, 0, 200, 100);
g.setColor(Color.GREEN);
g.fillOval(x, 5, 90, 90);
}

@Override
public void paint(Graphics g) {
doubleBuffer.setColor(Color.GRAY);
doubleBuffer.fillRect(0, 0, 200, 100);
drawDircle(doubleBuffer);
g.drawImage(screenImage, 0, 0, this);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值