原创

缓冲Buffer,缓存Cache,池Pool

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://xxlcube.blog.csdn.net/article/details/8232740

缓冲

缓冲可以理解为漏斗一样,是为了解决上下层的性能差异而出现的,缓冲可以有效的减少上层组件对下层组件的等待时间

在JDK中,IO使用缓冲是最多的


主要就这四类,那么建议以后所有IO操作都是用这四个类来操作

import java.io.*;


public class Demo {

	static int len = 100000;
	
	static String content = "Hello World !\n";
	
	static String path = new File("").getAbsolutePath()+File.separator+"demo.txt";

	public static void main(String[] args) {
		Writer writer = null;
		try {
			writer = new BufferedWriter(new FileWriter(new File(path)));//默认构造8K的缓冲区
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		long begin = System.currentTimeMillis();
		try {
			for(int i=0;i<len;i++) {
				writer.write(content);
			}
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		System.out.println(end-begin);
		//-----------------------------------
		try {
			writer =new FileWriter(new File(path));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		begin = System.currentTimeMillis();
		try {
			for(int i=0;i<len;i++) {
				writer.write(content);
			}
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		end = System.currentTimeMillis();
		System.out.println(end-begin);
	}
}
27
60
从结果中可以看出来差异了,有明显的性能差异


import java.io.*;


public class Demo {

	static int len = 100000;
	
	static String content = "Hello World !\n";
	
	static String path = new File("").getAbsolutePath()+File.separator+"demo.txt";

	public static void main(String[] args) {
		Reader reader = null;
		try {
			reader = new BufferedReader(new FileReader(new File(path)));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		long begin = System.currentTimeMillis();
		try {
			for(int i=0;i<len;i++) {
				reader.read();
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		System.out.println(end-begin);
		//-----------------------------------
		try {
			reader =new FileReader(new File(path));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		begin = System.currentTimeMillis();
		try {
			for(int i=0;i<len;i++) {
				reader.read();
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		end = System.currentTimeMillis();
		System.out.println(end-begin);
	}
}
9
40
读取差异

import java.io.*;


public class Demo {

	static int len = 100000;
	
	static String content = "Hello World !\n";
	
	static String path = new File("").getAbsolutePath()+File.separator+"demo.txt";

	public static void main(String[] args) {
		InputStream reader = null;
		try {
			reader = new BufferedInputStream(new FileInputStream(new File(path)));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		long begin = System.currentTimeMillis();
		try {
			for(int i=0;i<len;i++) {
				reader.read();
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		System.out.println(end-begin);
		//-----------------------------------
		try {
			reader = new FileInputStream(new File(path));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		begin = System.currentTimeMillis();
		try {
			for(int i=0;i<len;i++) {
				reader.read();
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		end = System.currentTimeMillis();
		System.out.println(end-begin);
	}
}
8
172
差异很明显
import java.io.*;


public class Demo {

	static int len = 100000;
	
	static String content = "Hello World !\n";
	
	static String path = new File("").getAbsolutePath()+File.separator+"demo.txt";

	public static void main(String[] args) {
		OutputStream writer = null;
		try {
			writer = new BufferedOutputStream(new FileOutputStream(new File(path)));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		long begin = System.currentTimeMillis();
		try {
			for(int i=0;i<len;i++) {
				writer.write(content.getBytes());
			}
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		System.out.println(end-begin);
		//-----------------------------------
		try {
			writer = new FileOutputStream(new File(path));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		begin = System.currentTimeMillis();
		try {
			for(int i=0;i<len;i++) {
				writer.write(content.getBytes());
			}
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		end = System.currentTimeMillis();
		System.out.println(end-begin);
	}
}
50
392
从以上四种缓冲类来看,明显比非缓冲类效果更佳

建议JAVA IO操作全部采用缓冲类来实现

缓存

缓存就是存储用户最近最频繁访问的数据,免得用户每次都从主存中读取数据

JAVA中最为简单的缓存就是HashMap实现

数据库连接池

















文章最后发布于: 2012-11-28 09:35:18
展开阅读全文
0 个人打赏
私信求帮助

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 酷酷鲨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览