缓冲Buffer,缓存Cache,池Pool

来源:互联网 发布:mac版芒果tv缓存在哪 编辑:程序博客网 时间:2024/06/05 15:22

缓冲

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

在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 blocke1.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 blocke1.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);}}
2760
从结果中可以看出来差异了,有明显的性能差异


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 blocke1.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 blocke1.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);}}
940
读取差异

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 blocke1.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 blocke1.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);}}
8172
差异很明显
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 blocke1.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 blocke1.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);}}
50392
从以上四种缓冲类来看,明显比非缓冲类效果更佳

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

缓存

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

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

数据库连接池