"Understanding Java Multi-Thread One" multithreading-related concepts, thread life cycle and thread creation methods

Source: Internet
Author: User

The landlord decided to take Java multithreading to see, so specifically to write down the blog, most of the content is excerpt from the Internet to see the information or books, can give the link I will give the link. Well, I hope everyone with the landlord together, refueling refueling (^ω^) also welcome attention to the personal blog home www.anycodex.com building.

1. Multithreading-related concepts

Program: A set of instructions written in a language in order to accomplish a specific task.

Process: in a running program, the system skips read and a separate unit of resource allocation, and the operating system allocates a memory space for each process.

Threads: execution units that are smaller than the process, each process may have multiple threads, and threads need to be placed in a process to execute. The thread is managed by the program, and the process is dispatched by the system.

Multithreading: Parallel execution of multiple instructions, the CPU time slices in accordance with the scheduling algorithm assigned to each thread, in fact, time-sharing execution, knowledge switching times are short, the user feel at the same.

2. Thread Life cycle

3. Creation of threads

Java threads are created in two ways: inheriting the thread class creation, implementing the Runnable interface

    • Create a process

    • Code Demo

Method One: Inherit the thread class to create

<textarea wrap= "soft" class= "Crayon-plain print-no" data-settings= "DblClick" readonly style= "padding-top:0px; padding-right:5px; padding-left:5px; margin:0px; width:897.546875px; Overflow:hidden; height:556px; Position:absolute; opacity:0; border:0px; Box-shadow:none; border-top-left-radius:0px; border-top-right-radius:0px; border-bottom-right-radius:0px; border-bottom-left-radius:0px; -webkit-box-shadow:none; White-space:pre; Word-wrap:normal; Resize:none; Color:rgb (0, 0, 0); Tab-size:4; z-index:0; line-height:15px!important; background-image:initial; background-attachment:initial; background-size:initial; background-origin:initial; background-clip:initial; background-position:initial; background-repeat:initial; " ></textarea>
Package net.mindview.util;//first step, inherit the Thread class Mythreadrunning extends thread{    //constructor public    Mythreadrunning () {        super ("My Thread");        SYSTEM.OUT.PRINTLN ("New Thread" + getName ());    }        Step two, override the Run method public    void Run () {for        (int i = 0; I <= 3; i++) {            System.out.println ("New Thread" + getName () +i) ;            try {                sleep (+)            } catch (Interruptedexception e) {                //TODO auto-generated catch block                E.printstacktrace ();}}}    public class MyThread {public    static void Main (string[] args) {                ///Third step, instantiating a custom thread class object        mythreadrunning mtr = New Mythreadrunning ();                Fourth step, call the Start method to start the Run method        Mtr.start ();}                    }


Operation Result:

Create a new thread my Thread0
Create a new thread my Thread1
Create a new thread my Thread2
Create a new thread my Thread3

Method Two: Implement Runnable interface implementation

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="padding-top: 0px; padding-right: 5px; padding-left: 5px; margin: 0px; width: 897.546875px; overflow: hidden; height: 528px; position: absolute; opacity: 0; border: 0px; box-shadow: none; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; -webkit-box-shadow: none; white-space: pre; word-wrap: normal; resize: none; color: rgb(0, 0, 0); tab-size: 4; z-index: 0; line-height: 15px !important; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"></textarea>
Package net.mindview.util;//first step, implement Runnable interface class Mythreadrunning implements runnable{        //second step, overriding the Run method    public void Run () {for        (int i = 0; I <= 3; i++) {            System.out.println ("New Thread" + Thread.CurrentThread (). GetName () + i);            try {                thread.sleep;            } catch (Interruptedexception e) {                //TODO auto-generated catch block                E.printstacktrace ();}}}    public class MyThread {public    static void Main (string[] args) {                ///Third step, instantiating a custom thread class object        mythreadrunning mtr = New Mythreadrunning ();                The fourth step, instantiating a thread class object and passing the custom thread class object as a parameter, followed by a thread name, threads thread        = new Thread (MTR, "My thread");                Fifth step, call the Start method to start the thread        Thread.Start ();}                 }


Operation Result:

Create a new thread my Thread0
Create a new thread my Thread1
Create a new thread my Thread2
Create a new thread my Thread3

    • Comparison of the two methods

If the thread class is inherited, then the class cannot inherit other classes;

Implementing the Runnable interface does not affect inheriting other classes.

Summary: This article summed up here, how, things should be most understand it? The next article continues, the content is multi-threaded control, thread priority, and thread scheduling. Come on, come on, (^ω^).

Reference Link: http://blog.csdn.net/coder_pig/article/details/44284423

"Understanding Java Multi-Thread One" multithreading-related concepts, thread life cycle and thread creation methods

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.