Java Basics-Multithreading

Source: Internet
Author: User

1. Processes and Threads

Process: A program that is running. Each process has its own set of variables. Threads: An execution path in a process. threads to share data. Each thread has its own execution space in the stack area, its own method area, and its own variables. 2. MultithreadingAt least one thread in a process is running, and when multiple threads appear in a process, the application is called a multithreaded application. the thread run must be turned on by the method specified in the class: Start MethodStart Method:1) Start thread 2) Let the JVM invoke the Run method How to create a thread: |--inheriting the thread class steps:1. Define class inheritance thread class; 2. The replication Run method, the code that will let the thread run is stored in the Run method; 3. Create a thread object by creating a subclass object of the thread class, 4. Call the thread's Start method, open the thread, and execute the Run method. | --Implement runnable interface 1. Define class implementation runnable interface; 2. Overwrite the Run method in the interface (used to encapsulate the code that the thread wants to run); 2. The thread is created through the thread class and the subclass object that implements the Runnable interface is passed as a parameter to the constructor of the thread class; Why should it be passed? Because you want the thread object to explicitly run the object that the Run method belongs to. 3. The thread class object calls the Start method to turn on threads. Why should there be a runnable interface? 1. The creation of multithreading can be accomplished by inheriting the thread class. However, if a class already has its own parent class, you cannot inherit the thread class because of the limitations of Java single inheritance. the implementation of the Runnable interface avoids the limitations of single inheritance. 2. In fact, it is not similar in the need to be multithreaded execution of code to extract.          The location of the code that is multithreaded to run is defined separately into the interface, providing a precondition for extending the functionality of other classes. So when the thread class describes threads, the internally defined run method is also derived from the Runnable interface. thread Run State: is created:Start () Run:be qualified for execution and have the right to execute. Freeze:Sleep (time), wait ()-notify (), Threads release execution, and release execution eligibility. Blocking:Have the executive qualification, no executive right. Extinction:Stop () 3. Multithreading Security reasons for Multithreading security issues:Because of the fast switching of the CPU, which thread gets the execution of the CPU, which thread executes. 1) Multiple threads are working on shared data 2) There are multiple statements that perform operations on shared data How to solve the security problem:As long as the statements that manipulate the shared data are made by one thread during a certain period of time, other threads cannot come in to execute during execution. 4. Thread Synchronization Benefits:Resolves a thread safety issue. Disadvantages:Relatively low performance because the lock needs to consume resources, resulting in deadlock problems. Define synchronization Prerequisites:1) Two or more threads must be required to synchronize. 2) Multiple threads must be guaranteed to use the same lock. Synchronization functions:Define the Sync keyword on the function so that the function is common. 1)the lock used by the synchronization function is the this lock. 2)When the synchronization function is modified by static, the lock object is the bytecode file object for this class: class name. class   What is the difference between a synchronization function and a synchronized code block?           1. Lock object: The lock used by the synchronization code block can be any object, and the synchronization function is divided into two cases. 2. There is only one synchronization in a class, you can use synchronous functions, and if you have multiple synchronizations, you must use a synchronous code block. Sync Deadlock:Typically, you can see the phenomenon as long as the synchronization is nested. There is a synchronous code block in the synchronization function, and a synchronization function in the synchronization code block. wait for wake-up mechanism: (because these methods all need to be defined in synchronization, so three methods are defined in the object class)Wait: The thread in the synchronization is frozen, the execution is released, and the execution qualifies.          Store thread objects in the thread pool at the same time.          Notify: Wakes one of the waiting threads in the thread pool. Notifyall: All threads in the thread pool are awakened. wait and sleep differences: Wait (you can specify the time or the time is not specified, the time can only be awakened by the corresponding notify or notify): The thread releases the execution and releases the lock. sleep (must specify time, time to automatically turn from frozen state to running state): The thread releases execution, but does not release the lock.   Lock Interface: Encapsulates the lock object as an object to handle synchronization is an implicit lock operation, while the lock object is a displayed lock operation, it appears instead of synchronizing. Instead of directly manipulating the method waiting to wake up in the lock interface, these methods are individually encapsulated in an object: Condition. condition Interface: await (), signal (), Signalall ()    

Java Basics-Multithreading

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.