Java tread Multithreading (1) Implements runnable interface

Source: Internet
Author: User

Author: Ching

Original address: http://blog.csdn.net/qingdujun/article/details/39347245


This article demonstrates that tread multithreading implements the Runnable interface and simply explains why there is a way to create threads.

First, create a thread in the 2 method:

1) inherit the thread class to implement multi-threading, see my previous article: Java Tread Multithreading (0) a simple multithreaded instance;

2) The second method is to implement the Runnable interface and create a new thread.


Second, why should there be two ways to create a thread?

① main reason: It is method 1) can not meet our needs, so it produced a method 2);

② can not meet the requirements of the reason: mainly Java can only have a single inheritance, if a class student must inherit from the person class, but some content in student we have to implement multi-threading, this is not contradictory? (Because Method 1 implements multithreading, it must inherit from thread.) ), so here is the introduction of the Runnable method: implementation of the Runnable interface to achieve multi-threading effect, it is recommended to use the second method, that is, the method of this article.


A City small demo

Package THREAD.RUNABLE1.QDJ;//1. Defines the class implementation Runnable interface class RUNDEMO1 implements runnable{private int x = 0;//2. Overwrite the Run method in the Runnable interface//to store the thread code in run public void run () {while (x <=) System.out.println ("Runnable:" + (+ +)}} public class CRunableDemo1 {public static void main (string[] args) {RunDemo1 r = new RunDemo1 ()//3. Thread objects are established through the thread class and Runn The subclass object of the able interface as the parameter Thread t1 = new Thread (r); Thread t2 = new Thread (r);//4. Using start to open thread T1.start (); T2.start ();}}
Running Results display: (0-10 printed here, note: No Duplicates)

By running the results above, we'll try to compare the following code (define x as a local variable):

Package THREAD.RUNABLE1.QDJ;//1. Defines the class implementation Runnable interface class RUNDEMO1 implements runnable{//private int x = 0;//2. Overwrite the Run method in the Runnable interface//to store the thread code in run public void run () {for (int x = 0; x <= 3; ++x) System.out.println ("Runnable:" +x);} public class CRunableDemo1 {public static void main (string[] args) {RunDemo1 r = new RunDemo1 ()//3. Thread objects are established through the thread class and Runn The subclass object of the able interface as the parameter Thread t1 = new Thread (r); Thread t2 = new Thread (r);//4. Using start to open thread T1.start (); T2.start ();}}
The result of the run is shown (printed here 2 times did you find it?) ):

The comparison of two cases, in fact, also illustrates a problem:

Conclusion: Local variables have a copy within each thread.


References: Java video Bi Xiangdong presenter

Original address: http://blog.csdn.net/qingdujun/article/details/39347245

Java tread Multithreading (1) Implements runnable interface

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.