Java Handy notes nine IO and threads

Source: Internet
Author: User

IO: Data read and write operations. Input stream: Source----application, read output stream: Application (source)---destination,write  byte stream: Inputstream,outputstream character stream:reader,writer   Read:1. Creating an input stream object associated with the file to be read 2. Read the file Contents 3. Closes the stream write:1. Creates an output stream object associated with the file to write data 2. Writes data to file 3. Close stream   character wrapper stream object: Bufferdreader: String readLine (), Nullbufferdwriter:newline ()   byte wrapper stream object: Objectinputstreamobjectoutputsream: Basic type data and object data reading and writing operations can be performed  file object: Information that encapsulates a file or directory, such as a path name, file name, and so on. If you want to read and write to a file, you can only use Io,file itself to read and write, just encapsulate the information.   Serialization: Storing objects in bytes----Write deserialization: Re-restoring stored objects----read most objects in a standard class library can be serialized, such as: parcel classes, strings, arrays, collections, and so on.   Serialization rules: 1. Serialization only serializes field values, and constructors and methods do not serialize 2. If another object is used in the serialized object, the object is also serialized 3. If an object is related to a system operation, the object cannot be serialized 4. Static member variables do not participate in serialization.   Custom object serialization, you must implement the Serializable interface if a field does not want to be serialized, it can be decorated with transient.   Console device: Standard input device: keyboard system.in = = InputStream standard output device: terminal window System.out = = PrintStream standard error device: terminal window System.err = = printstream  convert Stream object: InputStreamReader: Converts the byte input into a character input stream OutputStreamWriter: streams the byte output into a character output stream  inputstreamreader ( InputStream in) outputstreamwriter (outputsream ou) Scanner:java.util package, is a scanner that can scan simple file data (Basic classType or String) is scanned in the form of a field that encounters delimiters that return data (spaces, tabs, carriage return newline characters) to be scanned: file object, byte stream, character stream, string  printf ("format code", parameter 1, parameter 2, ....); Standard syntax for formatting codes:%[-][0][length]. [Precision]< Format Code >eg:%,8d: At this point, "," represents the Thousand separator  %s,%b,%d,%o,%x (%x),%c,%n,%% Note: If the specified length is less than the actual length, the default right alignment is output according to the actual length, and the space-complement thread: The CPU is executing a thread in an application, and when executing multiple threads, the CPU is doing a high-speed switchover. A process has multiple threads, and only one thread in a process is called single-threaded. The JVM is the process main method, which is the main thread of multiple independent code fragments within main thread i.e. multiple child threads. The CPU switching thread is based on the number of times the thread's priority (1-5-10) CPU is executing on which thread is indeterminate. Create thread: 1. Define the class implementation runnable interface and overwrite the code 2 of the Run method that encapsulates the child thread in the Run method. The first step of the object is passed as a parameter to JAVA.LANG.THREAD3. Call the thread's start () method Start thread custom Create thread: 0 Custom class inherits the thread class gets the threading name: Thread. The CurrentThread () static method gets all object objects for the current thread. GetName () gets the name. Thread life cycle: New Thread---can run (start)--Plug (Sleep)---running (run) (object's lock Pool)-wait () and notify () blocking object's wait pool--end (run end) multi-threaded Can be run with the running loop   STOP thread: Notification method to stop the thread Cheng Zi threads Create an instance variable or encapsulate a variable true or FALSE to provide a set method in the main method of the object call method Wake: Active Wake: Thread. Sleep (1000) Specify time static method sleep Passive Wakeup: Call interrupt () instance method   Multithreading shared data security issue: Dirty data may occur when multiple threads are performing operations on the same run method when a thread executes a run method that does not run out Another thread participates in dirty data resolution: Synchronous statement block {} synchronized (this) synchronous lock other objects do not enter when the synchronized statement ends several situations when the lock tag is released back to the object 1. Thread normal execution to languageEnd of sentence Block 2. Exception 3 is thrown in the statement block. A break statement or return statement was encountered in the statement block. Premises multithreading the same object stick{} Static statement block statement block class LOAD statement block the main method executes the JVM compilation as the class loads and execution {int age = 20} statement block is only executed once in the method and disappears. Multithreaded resource-based interaction: Thread object 1 Wait () waits for Thread object 2 notify () immediately wakes thread 1wait and the Notify method must be synchronously called by the same lock pool object. Notifyall Wake up all the threads

Java Handy notes nine IO and threads

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.