(1) I/O stream (2) thread

Source: Internet
Author: User

1.I/O Flow
1.1 ObjectOutputStream Class (emphasis)
(1) Basic concepts
The Java.io.ObjectOutputStream class is primarily used to write a whole Java object to the output stream.
Only objects that support the Java.io.Serializable interface can be written to the stream.
Class by implementing the Java.io.Serializable interface to enable its serialization functionality.
When an object is written to the output stream in its entirety, it is necessary to organize the various related information of the object into a byte sequence and then store it, and the process of the organization is called serialization.

(2) Common methods
ObjectOutputStream (OutputStream out)-constructs an object according to the reference specified by the parameter.
-Where the OutputStream class is an abstract class, the arguments need to pass the object of the subclass.
void WriteObject (Object obj)-used to write the objects specified by the parameter to the output stream.
void Close ()

1.2 ObjectInputStream Class (emphasis)
(1) Basic concepts
The Java.io.ObjectInputStream class is primarily used to read the entire Java object from the input stream.

(2) Common methods
ObjectInputStream (InputStream in)-constructs the object according to the reference specified by the parameter.
-Where the Inputstram class is an abstract class, the arguments need to pass the object of the subclass.
Object ReadObject ()-Used to read the object from the input stream and return it.
void Close ()

Share experience:
When you need to write multiple objects to a file, it is recommended that you put multiple objects into a collection and then call the WriteObject () method to write the whole set as an object, and only call the ReadObject () method one time.

2. Threads
2.1 Basic Concepts
Program-Data structure + algorithm, mainly refers to the executable file stored on the hard disk.
Process-primarily refers to programs that run in memory.

Currently the main operating system support multi-process, is to allow the operating system to perform multiple tasks at the same time, but the process is heavyweight, the new process to the system resource consumption is relatively large, so the number of processes is limited.
A thread is a program flow inside a process, which means that multiple processes are supported in the operating system, and the internal of each process can support multiple threads, and the thread is lightweight, and the new thread shares the resources of the process and therefore consumes less resources.
After the mainstream development of the use of multi-threaded mechanism, and the operating system mainly using time slice rotation method to ensure the simultaneous execution of multitasking, the strategy is called concurrency mechanism, that is, macro parallel, micro-serial mechanism.

2.2 Creation of threads (heavy weight)
(1) How the thread class creates threads
The Java.lang.Thread class is primarily used to describe threads, and Java virtual machines allow Java programs to start multiple threads and simultaneously execute
How to create a thread:
A. The custom class inherits the thread class and overrides the run () method and then creates a custom class object called the start () method
B. The custom class implements the Runnable interface and overrides the Run () method, then creates an object of the custom class as an argument for creating the thread class object, and finally calls the start () method with an object of the thread class.

(2) How to create a thread
Thread ()-Constructs an object in a way that is not a parameter.
Thread (String name)-constructs the object using the name specified by the parameter.
Thread (Runnable target)-constructs an object according to the reference specified by the parameter.
-where runnable is an interface, there are two ways to pass an argument:
A. The custom class implements the Runnable interface, which constructs the object of the custom class as an argument pass.
B. Use an anonymous inner class to directly construct a reference to the interface type as an argument pass.
Thread (Runnable target, String name)
-Constructs the object together with the reference and name specified by the parameter.

void Run ()-if the method is called using an interface type reference to a constructed thread object, the version that the final call interface points to
-Do nothing if the method is not called by a thread object constructed using the interface reference.
void Start ()-Used to start the thread, and the Java virtual Opportunity automatically invokes the run () method of the Thread object.

Principle (Master):
Find the Null constructor method of the thread class = = Call the init () method, the second argument is the method body of the Init () method, and the argument null is assigned to the second parameter, target, and target as the second argument to Init (). Method = = The second parameter target of the init () method becomes null and the null value is assigned to the member variable target.
Find the source of the run () method, and if (target! = null) {Target.run ();}, the condition is not set up =
This proves that the run () method is not doing anything.

(3) How thread creation works
The thread that executes the main () method is called the main thread, and the threads that execute the run () method are called child/new threads.
For the code before the start () method, executed once by the main thread, when the start () method call succeeds, the number of threads is instantly changed from one to 2, where the newly created child thread executes the run () method, the main thread continues to execute the subsequent code, and two threads do not affect each other at the same time.
When the run () method ends, the child thread ends, and when the main () method ends, the main thread ends, and the order of the two threads executes is not explicitly defined, and is determined by the system scheduling algorithm.

Attention:
The creation of threads is relatively simple and easy to understand, but the Java language supports single inheritance, and if one class inherits the thread class, it is not possible to inherit other classes, so it is recommended to create the thread in the second way, thus improving the maintainability and extensibility of the code.

2.3 Number and name of the thread (familiar)
Long GetId ()-Used to get the thread number of the calling object.
String getName ()-Used to get the thread name of the calling object.
void SetName (String name)-Used to set the thread name to specify a value for the parameter.
Static thread CurrentThread ()-Used to return a reference to the currently executing thread object.

2.4 The primary state of the thread (understanding)
New state-the state entered after the thread object was created with the New keyword.
-The thread does not start executing at this time.
Ready state-The state entered after the thread object calls the start () method.
-The thread still does not start execution at this time.
Run state-The state that is entered when the thread object is dispatched by the thread scheduler.
-The thread begins execution at this time.
-Returns to the ready state when the time slice of the thread object finishes executing but the task is not completed.
Extinction state-the state in which the time slice of the thread object finishes and the task is completed.
Blocking state-the state that enters after a blocking event occurs during thread execution, such as the Sleep () method.
-Enter the ready state when the blocking is lifted.

2.5 Common methods
static void sleep (long Millis)
-Used to put the currently executing thread into hibernation, the sleep parameter specifies the milliseconds (emphasis).
static void sleep (long millis, int nanos)
-For the sleep parameter specified milliseconds + nanoseconds, 1 seconds =1000 milliseconds 1 milliseconds =1000 microseconds 1 microseconds =1000 nanoseconds

(1) I/O stream (2) thread

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.