20165324 Java programming Eighth week of study summary

Source: Internet
Author: User
Tags thread class

Study No. 20165324 "Java Program design" Eighth Week study summary textbook Learning contents Summary 12th Chapter Java Multithreading mechanism

Processes and Threads

进程是程序的一次动态执行过程;线程是比进程更小的执行单位

The state and life cycle of a thread

    • The Java language uses the object of the thread class and its subclasses to represent threads, and the complete life cycle undergoes state:
      1. New
      2. Run: The start () method must be called to notify the JVM to run
      3. Interrupts: Four causes interrupted
      4. Death: There are two reasons for the end of the normal operation; the second is that the thread is forced to end prematurely, forcing the run () method to end.

Thread Scheduling and prioritization

    • The thread scheduler in a Java Virtual machine is responsible for managing threads, with a priority of 10 levels, represented by class constants in the thread class. The priority can be setPriority(int grade) adjusted by method, and getPriority the method returns the priority of the thread.
    • The task of the JVM's thread scheduler is to enable high-priority threads to always run, and once the time slices are idle, the threads with the same priority will use the time slices in a rotating fashion.

Thread class and the creation of threads

    • Subclasses of thread: When you write a subclass of the thread class, you need to override the method of the parent class to run() standardize the specific operation of the thread.
    • Use the thread class: The constructor method is Thread(Runnable target) that the parameter in the constructed method is an interface of type runnable.

Using the Runnable interface is more flexible than using subclasses of the thread class

    • If a class inherits the thread, it is not suitable for resource sharing. However, if the Runable interface is implemented, it is easy to realize the resource sharing.

The relationship of the target object to the thread

    • The target object is fully decoupled from the thread
    • Target object combination thread (weakly coupled)

about how many times the Run method started

Common Methods of threading

    • start(), the method is called to start the thread and queue it into the ready queue
    • run()That defines what to do after the thread object is dispatched
    • sleep(int millsecond), the method is called to discard the CPU resources and hibernate. The sleep () method must be called in the Try-catch statement.
    • isAlive()The thread call method returns False when the thread is in the new state isAlive() .
    • currenThread()Method is a class method of thread that can be called with the class name to return the thread that is currently using the CPU resource.
    • interrupt(), call this method to end hibernation and re-queue for CPU resources.

Thread synchronization

    • Concept: Several threads in a program need to use a method, and this method is decorated with synchronized (synchronous). Multiple threads calling this method must adhere to the synchronization mechanism.

Reconciling threads for synchronization

    • The wait () method is used in the synchronization method to execute the interrupt thread.
    • The synchronization method uses the Notify () method to notify all waiting threads that are waiting because of this synchronization method.
    • wait(),notify(),notifyAll()都是Object类中的final方法

Thread Federation

    • While a thread A is occupying CPU resources, it is possible for other threads to call join () and this thread to federate as: B.join(); .

GUI thread

    • When a Java program contains a graphical user interface (GUI), the Java virtual machine automatically launches more threads when it runs the application, with two important threads: AWT-EventQuecue and AWT-Windows , the previous one responsible for the GUI event, and the next thread to draw the form or component to the desktop.

Timer thread

Daemon Threads

    • A thread can call a void setDaemon(boolean on) method and set it to a daemon thread.
Problems in teaching materials learning and solving problems in debugging process code and solving process code hosting

Last week's summary of the wrong quiz
    • Wrong question 1 and reason, understand the situation
    • Wrong question 2 and reason, understand the situation
    • ...
Resources
    • Java Learning Notes (8th Edition)

    • Java Learning Note (8th Edition) Learning Guide
    • ...

20165324 Java programming Eighth week of study summary

Related Article

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.