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);
    }
}

你可能感兴趣的:(java)