Java Eighth Week study summary

Source: Internet
Author: User
Tags switches thread class

12th Chapter Java Multithreading mechanism One, learning content 12.1 process and process

A program is a static code, the process is a process of dynamic execution of a program, which is the process itself from the generation, development to extinction process.

Threads in 12.2Java

1. Multithreading mechanism of Java

A major feature of the Java language is the built-in support for multithreading.

Multithreading refers to a situation in which there are several executions in an application that work together according to several different execution threads.

The computer can only execute one of the threads at any given time. The Java Virtual machine quickly switches control from one thread to another, and these threads are executed in turn, giving each thread a chance to use CPU resources.

2. Main threads (main thread)

The main thread (main threads) is responsible for executing the main method.

If no other thread is created in the main method, the JVM ends the Java application when the main method finishes executing the last statement, that is, when the main method returns. If another thread is created in the main method, the JVM will switch between the main thread and the other threads, ensuring that each thread has the opportunity to use the Cpu,main method even after the final statement is executed (the main thread ends), the JVM does not end the Java application. The JVM has to wait until the Java application has finished before ending the Java application.

3. The State and life cycle of the thread

The Java language uses objects of the thread class and its subclasses to represent threads.

The newly created thread typically undergoes the following 4 states in a full life cycle:

NEW: An object of the thread class or its subclasses is declared and created, and the corresponding memory space and other resources are already available.

Run: When the JVM switches the CPU usage to the thread, the thread can start its own life cycle independently of the main thread that created it.
If the thread is created by a subclass of thread, the run () method in the class executes immediately, and the program must override the parent class's run () method in the subclass.
Do not let the thread call the start () method again until the threads have finished the run () method, or the illegalthreadstateexception exception will occur.

4. Thread scheduling and Priority

Each Java thread has a priority between constants 1 and 10, that is, between Thread.min_priority and Thread.max_priority. If the priority level of the thread is not explicitly set, the priority for each thread is constant 5, which is thread.norm_priority.

The priority can be adjusted by the setpriority (int grade) method, and the GetPriority method returns the priority of the thread.

When the thread uses the CPU resource time, even if the thread does not complete its own operation, the JVM interrupts the execution of the current thread, switches the CPU's use to the next queued thread, and the current thread waits for the next cycle of CPU resources and then resumes execution from the middle.

12.3Thread class and thread creation
    • Subclasses that use thread

When you write a subclass of the thread class, you need to override the run () method of the parent class.

Pros: You can add new member variables to a subclass, implement the city with some kind of attribute, or add new methods to the subclass to make the thread have some kind of function.

Java does not support multiple inheritance, and subclasses of the thread class can no longer extend other classes.

    • Use the thread class.

Create the thread object directly with the thread class: Thread (Runnable target), the parameter in the constructor method is an interface of type Runnable.
When you create a thread object, you must pass an instance of the constructed method's parameters to the constructor of the Runnable interface class, which is called the target object of the created thread, and when the thread calls the start () method, the target object automatically calls the run () in the interface once it has its turn to enjoy the CPU resources. Method (interface Callback)

For threads that use the same target object, the member variables of the target object are naturally the data units shared by those threads, and using the Runnable interface is more flexible than subclasses using thread.

    • The relationship of the target object to the thread

The target object and thread are fully decoupled: the target object is often required to determine which thread is being executed by the JVM by acquiring the name of the thread (because a reference to the thread object cannot be obtained).

Target object combination thread (weakly coupled): The target object can be combined with threads, and the target object class can combine thread objects when the target object is able to obtain a reference to the thread object.

    • about how many times the Run method started

For a thread with the same target object, when one of the threads is enjoying CPU resources, the target object automatically calls the Run method in the interface, where the local variable in the Run method is allocated memory space, and when the other thread is on the CPU resource, the target object calls the Run method in the interface again, run ( ) The local variable in the method allocates memory space again. The run () method has started running two times, running in separate threads

Common methods for 12.4 threads

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 ():
The 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.

12.5 Thread Synchronization

Multiple threads calling the synchronized method must adhere to the synchronization mechanism.

The first thing to do when dealing with thread synchronization is to modify the method of the data with the keyword synchronized.

The so-called thread synchronization is a method that several threads need to use a synchronized adornment.

Thread synchronization mechanism: When a thread A uses the Synchronized method, other threads must wait until thread a uses the synchronized method when they want to use the Synchronized method.

12.6 Coordinating 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.

Wait (), notify (), Notifyall () are the final methods in the object class, are inherited by all classes, and do not allow the method to be overridden. You cannot use Wait (), notify (), Notifyall () in a non-synchronous method.

Second, the Code

Three, the Code Cloud Link:

Https://gitee.com/BESTI-IS-JAVA-2018/20165213zqh

Java Eighth Week 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.