Java Basic Four

Source: Internet
Author: User

Multithreading

Implement Runnable Interface than inherited thread class has the advantage of:

1 ): Suitable for multiple threads of the same program code to process the same resource

2 ): You can avoid Java The limit of single inheritance in

3 ): Increase the robustness of the program, the code can be shared by multiple threads, code and data Independent.

The life cycle of a thread

After the thread is created and started, it undergoes five states, namely new, Ready (Runnable), run (Running), blocking (Blocked), and Death (Dead).

NEW: When a thread is created with the keyword new, it is in a new state, where only the virtual machine allocates memory and initializes member variables, and the program does not execute the thread execution body.

Ready: When the thread object calls the start () method, the thread is in a ready state, and the virtual machine creates a call stack and program counter for it, but at this point the thread is not running, only that the thread can run. Run: When a thread in the ready state obtains the CPU and starts executing the Run method, the thread is running. Blocking: When a thread calls the sleep method, the blocking Io method, the Suspend method, and waits for notification (notify), the thread is nearly blocked. The blocking state will re-enter the ready state at the appropriate time. Death: The thread ends in the following three ways, then it is in the dead state, the Run method executes, the thread throws an uncaught exception, and the direct jump Stop method ends the process.Control of ThreadsisAlive (): Test thread is activeIsdaemo (): Test whether the process is daemonjoin (): Waits for the process to terminatesleep (): Allows the executing thread to hibernate within the specified number of milliseconds without losing ownership of any monitoryield (): Pauses the currently executing thread object and executes another thread, which is moving the thread to the Ready statesetpriority (int newpriority): Change the priority of a threadCommunication of ThreadsJava in order to avoid polling detection, through the wait (), the Notify () and the Notifyall () method to implement in-process communication mechanism. wait (): tells the calling thread to discard the monitor into wait mode until another thread enters the same monitor and calls the Notify () method. notify (): Wakes a single thread waiting on this synchronizerNotifyall (): Wakes up all the threads waiting on this synchronizerJava SystemClassification of IO streams
    • Divided into: character stream and byte stream according to different types of processing data
    • Divided into: input stream and output stream according to data flow
    • According to the data function is divided into: node flow and processing flow

As long as you are working with plain text data, you prioritize the use of character streams, and in addition, you use byte streams.

Object flow: Transmits data in an object unit. Use the class ObjectInputStream and ObjectOutputStream implementations to save the stream in FileOutputStream and FileInputStream classes.

Serialization of objects: Converts objects in a heap into the form output of binary data.

Deserialization of objects: Restores binary data to objects in piles.

Serialization Flag keyword: Serializable

Keywords that cannot be serialized: transient

Serialization: Only one object is serialized into a byte stream for network transmission or persistence.

Java Image Interface Programming

There are two sets of ATW and swing components in Java to support graphical user interface programming.

ATW is the first to appear, but is too dependent on the operating system, there is a great disadvantage, AWT relies on local methods to achieve its function, so the AWT component is often referred to as heavyweight components.

Swing was developed on top of ATW. Completely Java code written, independent of the operating system, you can adjust the appearance, making programmers more convenient to write programs.

Swing's GUI (graphical user interface) is made up of containers and components, where containers are placed.

Layout Manager

FlowLayout: Components are arranged horizontally from left to right in a row.

BorderLayout: North, South, east, west, middle.

GridLayout: Arranging components in a grid of rows and columns

BoxLayout: More complex, more powerful grid layouts

Event-driven programming

Event: The object that describes what happened .

There are different types of event classes used to describe various types of user interaction.

Event Source : The generator of the event.

Event Handlers : Methods to receive events, interpret events, and handle user interaction

Java Basic Four

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.