20165234 Java programming Eighth week of study summary

Source: Internet
Author: User
Tags thread class

Seventh Week study Summary teaching contents study 12th chapter Java multithreading mechanism
    • Processes and Threads

A process is a dynamic execution of a program that corresponds to a complete process from code loading to execution to completion.

A thread is not a process, it is a smaller execution unit than a process.

During its execution, a process can produce multiple threads, resulting in multiple execution leads.

    • Main thread

Each Java application has a default main thread.

When the JVM (Java virtual machine VM) loads code and discovers the Main method, it starts a thread called the main thread, which is responsible for executing the main method.

The JVM waits until all the threads in the Java application have finished before ending the Java application.

The main thread is primarily responsible for executing the main method. The thread that is created in the execution of the main method is called another thread in the program.

The JVM will switch between the main thread and other threads to ensure that each thread has the opportunity to use CPU resources.

    • Multithreading mechanisms in Java

Multithreading is the case where there are several actuators in an application that work together according to several different execution threads.

    • The state and life cycle of a thread

1. NEW: When an object of the thread class or its subclasses is declared and created, the new thread object is in the new state.

2. Run: The thread must call the Start () method (the method inherited from the parent class) to notify the JVM, so the JVM will know that another new thread is waiting to be switched. Once it comes to the CPU resource, it can start its own life cycle independently of the main thread that created it.

3. Interrupt: The VM switches the CPU resources from the current thread to another thread, leaving the thread to give up the CPU's right to be in a broken state; During thread usage of CPU resources, the sleep (int millsecond) method is executed to bring the current thread into hibernation; The wait () method is taken, and the thread uses CPU resources to perform an operation that goes into a blocking state.

4. Death: The thread in the dead state does not have the ability to continue running. The thread frees the entity.

    • Thread class and the creation of threads

When writing subclasses, you need to override the run () method

Create thread construction Method: Thread (Runnable target) , parameter is an interface of type Runnable, passing instance object to interface

    • Common Methods of threading

start () : The thread calls the method to start the thread and queue it from the new state into the ready queue, and once it comes to the CPU resource, it can start its own life cycle independently of the thread that created it.

run () : The Run () method of the thread class is the same as the function and function of the run () method in the Runnable interface, and is used to define the operations that are performed after the thread object is dispatched, which are methods that the system automatically calls and that the user program must not refer to.

sleep (int millsecond) : A high-priority thread can call the sleep method in its run () method to allow itself to abandon CPU resources and hibernate for a period of time.

isAlive () : When the thread is in the "new" state, the thread calls the IsAlive () method to return false. The thread calls the IsAlive () method to return True before the run () method of the threads ends, that is, before the death state is entered.

CurrentThread () : This method is a class method in the thread class and can be called with the class name, which returns the thread that is currently using the CPU resource.

interrupt (): A thread that occupies a CPU resource can let the dormant thread invoke the interrupt () method to "wake up" itself, that is, the thread that caused the sleep interruptedexception exception, thus ending hibernation, Re-queued for CPU resources.

    • Thread synchronization

When dealing with multithreading issues, you must be aware of the problem when two or more threads access the same variable at the same time, and a thread needs to modify the variable. The program should deal with such problems, otherwise chaos may occur.

The so-called thread synchronization is a method in which several threads in a program need to use a synchronized (synchronous) adornment.

    • Reconciling threads for synchronization

The Wait () method interrupts the execution of the method, causes the thread to wait, temporarily yields the CPU, and allows other threads to use the synchronization method.

The Notifyall () method notifies all waiting threads that are waiting by using this synchronization method. The thread that was interrupted will continue to execute this synchronization method from the point where it was just interrupted, and follow the principle of "first break first".

The notify () method simply notifies a waiting thread of the end of a wait.

GUI thread: When a Java program contains a graphical user interface (GUI), the Java virtual machine automatically launches more threads when the application is run, where the awt-eventquecue thread is responsible for handling GUI events, and the awt-windows thread is responsible for drawing the form or component to the desktop.

Code Hosting

The process of code submission:

Code Volume statistics

Last week's summary of the wrong quiz

20165234 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.