20145207 Java Programming 6th Week of study summary

Source: Internet
Author: User

Summary of learning contents of textbook

A. Input / output

InputStream with the OutputStream

? concept of stream design

From an application perspective, the data is fetched from the source, the input stream can be used, the data is written to the destination, the output stream can be used, and in Java, The input stream represents the object as an Java.io.InputStream instance, and the output stream represents the object as an Java.io.OutputStream instance;

? Streaming Inheritance Framework

System.in and System.out represent standard input and standard output respectively;

You can use the Setin () method of the system to specify the InputStream instance, specifying PrintStream with the SetOut () method, as follows:

System.err is a PrintStream instance, called standard output stream, for displaying error messages immediately;

FileInputStream: is a subclass of InputStream, you can specify the file name creation instance, once the document is created to open, and then can be used to write data, the main operation of the InputStream read () abstract method, so as to read the data in the document;

FileOutputStream: is a subclass of OutputStream, you can specify the name of the creation of an instance, once the document is created to open, and then can be used to write data, the main operation of the OutputStream write abstract method, so that it can write data to the document;

When not in use, close the document with close ();

ByteStream is a subclass of InputStream, you can specify a byte array to create an instance, and once created you can read a byte array as a data source. Bytearrayoutputstream is a subclass of OutputStream, you can specify a byte array to create an instance, and once created you can write the byte array as the destination;

? Stream Decoration Processor

If you want to process the input and output data, you can use the Packager class (e.g. scanner);

Some subclasses of InputStream and OutputStream also have the role of packager, which can accept InputStream and OutputStream instances when they are created;

Common Packager: Bufferedinputstream, Bufferoutputsream (with buffer function), DataInputStream, DataOutputStream (with data conversion processing), ObjectInputStream, ObjectOutputStream (with the ability to serialize objects) and so on;

Character Processing classes

? Reader and writer inheritance architecture

Java.io.Reader class: Abstracts the source of character data read-in;

Java.io.Writer class: Abstracts the data to write the destination;

FileReader: Reads the document and converts the read data into characters; StringWriter: Writes the character data to its last method of using ToString () to get the string; The code is as follows:

? character processing adorners

The byte data is converted into corresponding encoded characters, and the stream data can be packaged using InputStreamReader and OutputStreamWriter;

Improve character input and output efficiency, provide buffer function: BufferedReader, bufferwriter;

PrintWriter: To OutStream packaging, to writer packaging;

two . threading vs. parallel API

Thread

? Threading Introduction

Single-threaded program: Start the process from the main () program entry point start to end only one process; multi-thread programs: have multiple processes;

The process that starts in Java from main () is executed by the main thread to create a thread instance to execute the run () method defined by the runable instance; (The Tortoise and the Hare)

? Thread and Runnable

To create a thread instance is to add CPU to the JVM, to start an extra CPU is to invoke the instance's start () method, and the additional CPU entry point can be defined in the run () method of the Runable interface;

In addition to defining the process as such, another way to compose a multithreaded procedure is to inherit the thread class and redefine the run () method;

The advantage of operating the Runnable interface is that it is more resilient, and your class has the opportunity to inherit other classes, and if the thread class is inherited, it is usually to take advantage of some of the methods defined in thread.

? Thread life cycle

Daemon thread: If a thread is labeled as a Deamon thread, the JVM will automatically terminate at the end of all non-Deamon threads, as follows:

Thread basic state diagram: executable, blocked, executed;

Threads look but the fact is that at the same point in time, a CPU can only execute one thread, just because it is constantly switching and fast, so it looks like it is executing simultaneously;

Thread has its priority, SetPriority () method set priority, use multithreading to improve performance;

When the thread uses join () to join a different thread, the other line waits for the joined threads to finish working before continuing with the action; The code is as follows:

After the thread finishes the run () method, it enters dead, and the start () method cannot be called again, otherwise the illiglethreadexception will be thrown;

? about the Threadgroup

Each thread belongs to a line Cheng group, once the thread is grouped into a group, it cannot be replaced; You can use the following program fragment to get the current thread's owning line Cheng group name: Thread.CurrentThread (). Getthreadgroup (). GetName ();

You can redefine this method by using the Uncoughtexception () method to handle a situation in which a thread in a group has not been caught by an exception;

Problems in code debugging and the resolution process

p345, the volatile part is not very clear.
Solution: Package cc.openhome;

Class  Variable1 {
static  int  0  0;
 
{
        i++;
        j++;
    }
 
{
        System.Out .printf( "i =%d, j =%d%n", i, j);
    }
}
 
Public  class  Variable1test {
{
New Thread(() -> {
While  ( true) {
                Variable1.one();
           }
            });
New Thread(() -> {
While  ( true) {
                Variable1.two();
            }
        });
        thread1.start();
        thread2.start();
    }
}

20145207 Java Programming 6th Week of study summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.