Java Multithreaded Learning Notes

Source: Internet
Author: User


process: The executing program is actually the space where the application runs in memory. (only for space allocation)

Threads: an execution unit in a process that is responsible for the running of a process rollup, with at least one thread in a process.

Multithreading: There can be multiple threads in a process, and this application can also be called a multithreaded program.

What is the role of a program that starts multiple threads?

A: 1. Multi-part programs can be implemented simultaneously, the jargon is called concurrency.

2. Multithreading can be used to make reasonable use of CPU resources, if too many threads can cause performance degradation.

The 3.CPU handler is done by fast switching, which seems to us to be random.

/***************************************************************
* Java multithreaded Learning. Use code to demonstrate the difference between before and after.
*
* In the previous code, after the JVM started, there must be an execution path (thread) starting from the main method.
* Always execute until the main method ends
* In Java This thread is called the main thread.
*
* When the main thread is executing in this program, if a loop is encountered, it is specified as long dwell time.
* Cannot execute the following program
* Can you implement a main thread that is responsible for executing one of these loops, with another thread responsible for other code execution.
* Implement multi-part code simultaneously.
* This is the problem that multithreading technology can solve.
*
* How do I create a thread?
*
* The thread class was found through the search of the Chinese thread in the API.
* By reading the description in the thread class.
*
* There are two ways of creating threads:
* 1. Inherit the thread class
* 1.1 defines a class that inherits the thread.
* 1.2 Rewrite the Run method.
* 1.3 Creating a Subclass object is creating a thread object.
* 1.4 calls the Start method, opens the thread and lets the thread execute it, and tells the JVM to call the Run method.
*
* Why do you do this?
* Inherit the thread class because the thread class describes threading things and has the functionality that threads should have.
* So why not just talk about creating objects for the thread class?
* Thread t1= new thread ();
* T1.start ();//There is no mistake in doing so, but when the start is tuned
* The Run method in the thread class does not define the code that we need the thread to execute.
*
* What is the purpose of creating threads?
* is to create a separate execution path that enables multiple parts of the code to execute concurrently.
* That is, the thread creates and executes the code that needs to be given (the thread's task)
* For the previous main thread, his task is defined in the main function.
* The tasks that a custom thread needs to perform are defined in the Run method.
* The task inside the Run method in the thread class is not what we need, just rewrite the Run method.
* Now that the thread class has defined the location of the threads task, just define the task code in the location.
* So the override Run method action is performed.
*
* Multithreaded execution is, in the stack memory, in fact, each execution thread has a piece of its own stack memory space.
* Methods of stacking and stacking.
*
* When the execution thread's task is finished, the thread is automatically freed in the stack memory.
* But when all the execution threads are finished, the process is over.
*
*//Get thread name
* Thread:currentthread () Gets the current thread object.
* How to name it? GetName ()
* Thread.current (). GetName ();
*
* Name of main thread: main
* Custom Thread: Thread-1
***************************************************************/

Example:

For example, only the main thread executes a program.

Classdemo_01 {PrivateString name; Demo_01 (String name) {This.name=Name }Publicvoid Show () {for (int x=1;x<=20;x++X); }} /******************************************* * the thread in Java The code that executes is present in the main function, so Java calls this thread the *******************************************/ Span style= "color: #0000ff;" >public static voidnew demo_01 ("Xiao Qiang" new demo_01 ("Wang Choi"  

Operation Result:

Example two:

Using the multi-threaded way, main thread to execute the cockroach method, a new thread to execute the Wang Choi method

Memory diagram

.

classDemo_02extendsthread{PrivateString name; Demo_02 (String name) { This. name=name; }     Public voidrun () { for(intx=0;x<20;x++) {System.out.println ("Name=" +name+ "..." +x); }    }        /******************************************* * * The code that the thread is executing in Java exists in the main function, so Java calls this thread the main path * *******************************************/     Public Static voidMain (string[] args) {//two thread objects were created. Demo_02 d1=NewDemo_02 ("Xiao Qiang"); Demo_02 D2=NewDemo_02 ("Wang Choi")); D2.start ();//turn the D2 thread onD1.run ();//The main thread is responsible for        /********************************************** * * Call the Run method without opening the thread, only the object call method * Call the Start method to open the thread and let the JVM tune         Executes in the open thread with the Run method. *          **********************************************/    }}

Example three:

On the basis of the function implementation of example Two, the implementation gets the name of the thread.

classDemo_03extendsthread{PrivateString name; DEMO_03 (String name) { This. name=name; }     Public voidrun () { for(intx=0;x<20;x++) {System.out.println ("Name=" +name+ "..." +thread.currentthread (). GetName () + "..." +x); }    }        /******************************************* * * The code that the thread is executing in Java exists in the main function, so Java calls this thread the main path * *******************************************/     Public Static voidMain (string[] args) {//two thread objects were created. Demo_03 d1=NewDemo_03 ("Xiao Qiang"); DEMO_03 D2=NewDemo_03 ("Wang Choi")); D2.start ();//turn the D2 thread onD1.run ();//The main thread is responsible for        /********************************************** * * Call the Run method without opening the thread, only the object call method * Call the Start method to open the thread and let the JVM tune         Executes in the open thread with the Run method. *          **********************************************/    }}

Program:

Java Multithreaded Learning Notes

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.