Joke Collection Website - Public benefit messages - Performance Optimization of Java Programs —— Decorator Mode (4)

Performance Optimization of Java Programs —— Decorator Mode (4)

? Decorator pattern ()

The core class of the system, writing data into a file, can increase the support for writing various data types on the basis of FileOutputStream, while the BufferedOutputStream decorator can increase the buffering function to optimize the performance of I/O. The performance component represented by the BufferedOutputStream is a typical realization of separating the performance module from the functional module.

Public static void main(String[] args) throws IOException {

//Generate a stream object with buffering function.

Data output stream dout=

New data output stream (new buffer output stream (new fileoutputstream

(C:\\a txt)))

//Stream object without buffering function

//data output stream dout = new data output stream(new file output stream

(C:\\a txt))

Long begin = system currentTimeMillis ()

for(int I =; I <; i++)

Dout writeLong (1)

System output println (Spend:+(system current time millis () begin))

}

The above code shows that the typical application of FileOutputStream is that there are two ways to establish OutputStream. The first method adds the performance component BufferedOutputStream, and the second method has no better I/O performance.

Please note that the implementation of OutputStream and InputStream class series in JDK is a typical application of decorator pattern. Through nesting, objects are continuously aggregated to form a super object, which has the functions of all related sub-objects.

Let's look at how the decorator pattern enhances I/O performance through performance components. The workflow of the runtime is shown in the figure.

Figure? Decorator pattern's workflow

BufferedOutputStream write () will be called before calling FileOutputStream write (), which is implemented as follows.

A public synchronous void write (byte b[] int off int len) throws IOException {

if(len & gt; = buf length) {// If the amount of data to be written is greater than the cache capacity,

flushBuffer()? //Write to all caches

Out write(b off len) // Write data directly to a file.

Return;

}

if(len & gt; Buffer length count) (

Refresh buffer ()

}

System array copy (b off buffer count len)

//If less data is written, write to the cache.

count+= len;

}

Private void flushBuffer () throws IOException {

if(count & gt; ) {

Out write (buffer count)? //the out object here is FileOutputStream.

count =;

}

}

It can be seen that every time the BufferedOutputStream write () is called, the data is not written to the disk, but written to the cache. When the cache is full, the FileOutputStream write () method is called to actually write the data, so as to realize the perfect separation of performance components and functional components.

? Back to the directory Java program performance optimization makes your Java program faster and more stable.

Editor's recommendation

? Java programming training video tutorial

? J EE advanced framework actual combat training video tutorial

? J ME mobile development practice teaching video

Development and actual combat of Visual C++ audio and video technology

Oracle indexing technology

Lishi Xinzhi/Article/program/Java/gj/20 13 1 1/2782 1