Javase Basic---multithreading

Source: Internet
Author: User
Tags thread class

Process: A program in progress. is actually the memory allocation space when an application runs.

Threads: A program execution control unit in a process, an execution path. The process is responsible for the identity of the space of the application, and the thread is responsible for the order in which the application executes.

Process and thread relationships: a process has at least one thread running, and when multiple threads are present in a process, the application is called a multithreaded application, and each thread has its own execution space, its own method area, and variables in the stack area.

When the JVM starts, it first has a main thread, which is responsible for the execution of the program, and the main function is called. The code executed by the main thread is in the main method.

When generating garbage, garbage collection is not required to complete the main thread, it should be the main thread in the execution of the code to stop, to run garbage collector code, inefficient, so a separate thread responsible for garbage collection.

One, two ways to create multithreaded procedures: the thread class and the Runnable interface

(i) by inheriting the thread class to complete

1. 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 automatically execute the Run method, note: The start () method executes two commands, 1, turns on the thread, 2, executes the Run method

2, the status of the thread:

(1) was created: Start ();

(2) Operation: qualified for execution, with the right to execute.

(3) Freeze: Sleep (time), wait ()---notify () wake up, thread release execution, and release execution eligibility;

(4) Zero blocking state: The thread has the CPU execution qualification, no CPU execution right

(5) Extinction: Stop ()

(b) By implementing the Runnable interface (if a class has inherited other classes, it cannot inherit the thread class, Java's single inheritance limitations.) This class can only be extended for additional functions)

1. Steps:

(1) Define class implementation Runnbale interface;

(2) Overwrite the Run method in the interface (the code that encapsulates the thread to execute);

(3) Creating thread objects through the thread class;

(4) The subclass object that implements the Runnable interface is passed as the actual parameter to the constructor in the thread class; (The code run method that the thread is going to execute is defined)

(5) Call the Start () method of the Thread object, open the thread, and execute the Run () method in the subclass object of the Runnable interface.

2. Runnable interface

(1) Avoid the limitations of Java single inheritance

(2) When the thread class is described, the internally defined run method is also derived from the Runnable interface

(3) The Runnable interface encapsulates the task that the thread is performing as an object

(c) Case

1. Buy Tickets

      

Two, multi-threaded security issues

A thread security issue occurs when a thread is involved in an operation that performs operations that share data without taking the other threads into the process.

1. Two prerequisites for a security issue

(1) Multiple threads working on shared data

(2) The thread code that operates the shared data has more than one

2. Solutions

Let another program not perform the operation of the data as long as one thread is executing multiple codes that share data. That is, synchronization

Three, synchronization

1. Two prerequisites for defining synchronization

(1) must have two or more threads, only need to synchronize

(2) Multiple threads must be guaranteed to use the same lock

2. Two forms of manifestation of synchronization

(1) Synchronizing code blocks:

Format:

Synchronized (object)// can be any object, which is actually the lock {    // code that needs to be synchronized }    

(2) Synchronous function: The synchronization keyword is defined on the function, so that the function is synchronized.

The lock used by the Sync function lock is this

When a synchronous function is statically decorated, the call to the static function does not require the object, but the bytecode file of the class that the static function belongs to is loaded into the memory, and the byte-code file is encapsulated as an object. So the lock used by the synchronization function at this point is the bytecode file object for that class. (class name.)

2. The difference between synchronous code block and synchronous function

(1) The lock used by the synchronization code block can be any object.

The lock used by the synchronization function is this, and the lock of the static synchronization function is the bytecode file object of the class.

3, synchronous code block or synchronous function

You can use a synchronization function when there is only one synchronization in a class, but if you have more than one synchronization, you must use a synchronous block of code to determine the different locks. So the synchronization code block is relatively flexible.

4. Advantages and disadvantages of using synchronization

(1) Benefits: resolves thread security issues

(2) Disadvantages: Relatively low performance, because the lock needs to use resources to determine

Javase Basic---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.