One, Java multithreaded Programming core technology (note)--Multithreading implementation

Source: Internet
Author: User

Concept: A child task that can be understood as running independently in a process. For example, QQ.exe is running with a lot of subtasks running, buddy video threads, downloading file threads, transferring data threads, and so on.

Advantages: 1, improve CPU resource utilization, improve system performance.

First, the implementation of multithreading 1.1, inherits the thread class (extends thread), the thread class implements the Runnable interface

Implements Runnable
Extends thread{    @Override    void run () {System.out.println ("MyThread");}}   

1.2. Implement Runnable interface (implements Runnable)
Implements runnable{    @Override    void run () {System.out.println ("MyThread");}}  

Note: The use of inherited thread classes to develop multithreaded application design has limitations, because Java does not support multiple inheritance, in order to change this limitation, you can use the implementation, implementation of the Runnable interface (implements Runnable) way to achieve multithreading.

1.3. The randomness of Multithreading: When multi-threading is used, the result of running the code is independent of the order in which the code is executed or called.

Verify:

View Code

Results:

1.4. Non-thread-safe: When multiple threads operate on the same variable in the same object, the value is changed, the value is not synchronized, and the program runs.Non-thread safe

Resolve non-threading security issues by using the Resolve synchronized keyword

View Code

CurrentThread (): Returns which thread the code snippet is being called from. IsAlive (): Determines whether the current thread is active. Sleep (): Specifies the number of milliseconds for the thread that is executing to hibernate. GetId (): Gets the unique identity of the thread two, the thread that cannot be stopped

1. Determine if the thread is a stopped state

In the Java SDK, the Thread.java class provides two methods

This.interrupted (): Tests if the front thread has been interrupted and has been executed with the ability to clear the status ID to False.

This.isinterrupted (); Tests whether the thread is interrupted, but does not identify the purge state.

View Code

One, Java multithreaded Programming core technology (note)--Multithreading implementation

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.