Black Horse programmer------Multithreading (one)

Source: Internet
Author: User
Tags ticket

Black Horse programmer------Multithreading (one)

1.1 Multi-Threading concept

1.1.1 The concept of process, thread, and multi-process
process : is a program that is being executed.
Each process execution has an execution order. The order is an execution path, or a control unit.

thread : is an independent control unit in the process.
The thread is controlling the execution of the process.

There is at least one thread in a process.

A Java VM will start with a process java.exe.

At least one thread in the process is responsible for the execution of the Java program.
And the code that this thread runs on is in the main method.
This thread is called the primary thread.

Extension: In fact, more details indicate that JVM,JVM started more than one thread, as well as a thread responsible for the garbage collection mechanism.

1.2 How do you customize a thread in your custom code?

By looking at the API, Java has provided a description of what the thread is like. On the thread class.

1.2.1 The first way to create a thread: Inherit the thread class (example 1).
Steps:
1, define class inheritance thread.
2. The Run method in the thread class is replicated.
Purpose: Store your custom code in the Run method. Let the thread run.

3, call the thread's Start method,
The method has two functions: start the thread and call the Run method.

Example 1:

1 classDemoextendsThread2 {3      Public voidRun ()4     {5          for(intx=0; x<60; X + +)6System.out.println ("Demo Run----" +x);7     }8 }9 Ten classThreaddemo One { A      Public Static voidMain (string[] args) -     { -         //for (int x=0; x<4000; x + +) the         //System.out.println ("Hello world!"); -  -Demo d =NewDemo ();//Create a thread.  -         //D.start ();//Open the thread and execute the thread's Run method.  +D.run ();//is simply an object invocation method. And the thread was created and not running.  -  +          A          for(intx=0; x<60; X + +) atSystem.out.println ("Hello World!--" +x); -          -  -Thread T =NewThread ();//Creating Threads -      -     } in}

found that the results of the run are different every time.
Because multiple threads are getting the CPU's execution rights. The CPU executes to whom, who runs it.
Make it clear that at some point there can only be one program running. (except multicore)
The CPU is doing a quick switchover to achieve the effect that appears to be running simultaneously.
We can image the multi-threaded running behavior in each other to rob CPU execution right.

This is a feature of multithreading: Randomness. Who grabs who to execute, as to how long the CPU says the calculation.

Why overwrite the Run method?

The thread class is used to describe threads.
The class defines a feature that stores the code that the thread wants to run. This storage function is the Run method.

This means that the Run method in the thread class is used to store the code that the thread is running.

1.2.2 Second way to create a thread: Implementing the Runable Interface (example 2)

Steps:
1, define class implementation runnable interface
2, overwrite the Run method in the Runnable interface.
Store the code that the thread will run in the Run method.

3, the thread object is established through the thread class.
4, pass the subclass object of the Runnable interface as the actual parameter to the constructor of the thread class.
Why pass the subclass object of the Runnable interface to the constructor of thread.
Because, the object that the custom run method belongs to is the subclass object of the Runnable interface.
So let the thread execute the Run method of the specified object. You must specify the object to which the Run method belongs.

5, call the Start method of the thread class to open the thread and invoke the Run method of the Runnable interface subclass

Example 2:

1 classTicketImplementsRunnable//extends Thread2 {3     Private  intTick = 100;4      Public voidRun ()5     {6          while(true)7         {8             if(tick>0)9             {TenSystem.out.println (Thread.CurrentThread (). GetName () + ".... Sale:" + tick--); One             } A         } -     } - } the  -  - classTicketdemo - { +      Public Static voidMain (string[] args) -     { +  ATicket T =NewTicket (); at  -Thread T1 =NewThread (t);//a thread was created; -Thread t2 =NewThread (t);//a thread was created; -Thread t3 =NewThread (t);//a thread was created; -Thread T4 =NewThread (t);//a thread was created; - T1.start (); in T2.start (); - T3.start (); to T4.start (); +  -     } the}

What is the difference between the 1.2.3 implementation and the way of inheritance?

Implementation benefits: Avoids the limitations of single inheritance.
It is recommended to use implementations when defining threads.

There are two different ways to differentiate:
Inherit thread: The threading code is stored inside the thread subclass Run method.
To implement runnable, the thread code has the Run method of the subclass of the interface.

Black Horse programmer------Multithreading (one)

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.