Multithreading (i) Basic concepts

Source: Internet
Author: User

       Multithreading is a feature of Java applications, and mastering The multithreading of Java is also a The knowledge that Java programmers must have. multithreading refers to the ability to run multiple concurrently in a single programthe same thread performs different tasks. A thread is a sequential control flow within a program that can only be used to assign toresources and the environment.

The first of these two nouns is the beginning of this blog:


the difference between a thread and a process

The internal data and state of multiple processes are completely independent, and multithreading is a shared piece of memory space and a set of system resources that may interact with each other. ?

The data for the thread itself is usually only the register data, and the stack used by a program executes, so the thread's switchover is less burdensome than the process switch.

The purpose of multithreaded programming is to "make the most of CPU resources", when the processing of a thread does not need to consume CPU and only to deal with resources such as I/O, so that other threads that need CPU resources have the opportunity to gain CPU resources. Fundamentally, this is the ultimate goal of multithreaded programming.

Second, learn the basics of Java in multi-threading

1.Java If we do not generate a thread ourselves, then the system will give us a thread (the main thread, the main method is running on the main thread), our program is executed by the thread.

2. Process: Executing procedure (the concept of a procedure is static, the process is a dynamic concept).

3. There are two ways to implement a thread, the first is to inherit the thread class, then rewrite the Run method, and the second is to implement the Runnable interface and then implement its Run method.

4. Put the code that we want the thread to execute into the Run method and start the thread with the Start method, whichfirst prepares the system resources for the execution of the thread and then calls the Run method. when a class inherits the thread class, the class is called a thread class.

5. A process must contain at least one thread.

6. For a single-core CPU, only one thread can execute (micro-serial) at a time, and from a macro point of view, multiple threads are executing simultaneously (macro parallel).

7. For a dual-core or dual-core CPU, you can really do micro-parallelism.


Three, thread source code research:

1) The thread class also implements the Runnable interface, thus implementing the Run method in the Runnable interface;

2) When a thread object is generated, if no name is set for it, then the name of the thread object will be used as follows: Thread-number, the number will be automatically incremented and shared by all thread objects (because it is a static member variable).

3) When using the first method to generate the thread object, we need to rewrite the Run method because the thread class's Run method does not do anything at this time.

4) When using the second method to generate the thread object, we need to implement the Run method of the Runnable interface, and then use the new thread (new MyThread ()) (if MyThread has implemented the Runnable interface) to generate the thread object. The Run method or tune of the thread object at this point would mythread the class's Run method, so that the Run method we wrote was executed.

Description


public void Run () {If (target!=null) {Target.run ()}}


When the thread object is generated using the inherited thread, target is empty and nothing is executed when the second method is used to execute the Target.run (), the target is the instance object of Runnable, which is the method to execute the override

Summary: The difference between two types of generated thread objects:

1. Two methods are required to execute the thread's Start method to assign the required system resources to the thread, schedule the thread to run, and execute the thread's Run method.

2. In specific applications, the method used to construct the thread body depends on the situation. In general, when a thread has inherited another class, it should be constructed in the second way, that is, implementing the Runnable interface.



Multithreading (i) Basic concepts

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.