How Java 22-4 multi-threaded code is implemented 1

Source: Internet
Author: User

Requirements: we want to implement multi-threaded Program.
How to achieve it?
Because threads are dependent on processes, we should create a process First.
And the process is created by the system, so we should go to invoke the system function to create a process.
Java is not able to directly invoke the system functions, so we have no way to directly implement multithreaded Programs.
however, Java can go to the program written by C + + to implement multithreaded Programs.
The system function creation process is called by C + + and then by Java to invoke such a thing,
We then provide some classes for our Use. We can implement multithreaded Programs.
So what are the classes that Java provides?
Thread class
By looking at the api, we know there are 2 ways to implement multithreaded Programs.

Mode 1: inherit the thread class.
Steps
A: Custom class Mythread inherits the thread class.
B:mythread class inside rewrite run ()
    Why is the run () method?     

Not all code in the class needs to be executed by the THREAD.
The code that needs to be executed by the thread is the one that executes longer, and the other code waits until it runs Out.
So this time, the code needs to be executed by the THREAD.
At this point, in order to differentiate which code can be executed by the thread, Java provides a run () in the thread class to contain the code that is executed by the Thread.

C: Create an Object
D: Start Thread

Customize the Mythread class and override the Run () method

1 //note: When you create this class, you inherit the thread class2 3  public classMyThreadextendsThread {4 5      public voidRun () {6         7          for(intx = 0; X < 1000; X + +){8 System.out.println (x);9         }Ten     } one  a}

Test:

First we try to run with run ()

1 // to Create a Thread object 2 New MyThread (); 3  // Start Thread 4 My.run (); 5 My.run ();

The result is:

is still single-threaded execution, first executes run (), prints 0-999, and then executes again

Why is it?

Because the run () method directly calls is actually equivalent to the normal method call, so you see the effect of a single thread
To see the effects of multithreading, you must say another method:start ()

Here's an interview question: 

What is the difference between run () and start ()?
Run (): just encapsulate the code executed by the thread, the direct call is the normal method
Start (): starts the thread first, and then the JVM calls the Thread's run () method.

Try the start () method again:

1 New MyThread (); 2 My.start (); 3 My.start ();

Execution result: Prompt for illegalthreadstateexception error: illegal thread state exception

Reason:

Because this is equivalent to my thread being called two Times. Instead of two threads to Start.

Change Again:

1 // Create two Thread objects 2 New MyThread (); 3 New MyThread (); 4 5 My1.start (); 6 My2.start ();

Execution Result: simultaneously executes 2 print 0-1000 threads, prints out the result: thread 1 and thread 2 rob the resources, grab the execution, this time you rob, next time I rob, the output result also is So.

How Java 22-4 multi-threaded code is implemented 1

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.