Analysis of three implementations of Java threads

Source: Internet
Author: User

First, preface

The Java.lang.Thread class is a thread class for Java. When an object of the thread class is generated, a new thread is created. Each thread in Java accomplishes its operation by means of a particular thread object, the method run () is called the thread body.

The following is a brief introduction to several methods of constructing the thread class:

Public thread () public thread (Runnable target) public thread (Runnable target,string name) public thread (String name)
The parameter target is an instance of implementing the Runnable interface that implements the run () method of the thread body. The destination target can be null, indicating that it has its own instance to execute the program. The name parameter specifies the name of the thread and, if not specified, is automatically assigned by the JVM virtual machine, such as Thread-1, Thread-2, and so on.

There are two ways to implement line Cheng in Java: One is to inherit thread class thread, and the other is to implement Interface runnable



Ii. Mode of Implementation 1

Inheriting the thread class thread and overriding the method run (), when initializing this class instance, the destination target is null, indicating that the thread body is executed by this instance. disadvantage: Because Java only supports single-inheritance, classes defined in this way can no longer inherit from other parent classes . The code listing is as follows:

public class Demo1 extends Thread{boolean isrunning = true;   Stop Mark public void Run () {          //method body while (isrunning) {try{...} catch (Interruptedexception e) {e.printstacktrace ();}}} public static void Main (string[] args) {demo1 =new Demo1 ();         Instantiate object Demo1.start ();}}
Description: Creates a child thread in main main method with new Demo1 () and initiates a child thread through Demo1.start (). Main thread is the main thread, and the main thread is responsible for managing other sub-threads. To be able to stop the thread, add an identity to the main thread, demo1.isrunning =false by changing the identity, and end the thread.



ii. mode of implementation 2

A class that provides an instance interface runnable as the target object for a thread, passing the target object (the class implementing the runnable) to the thread instance when constructing the thread class, and a method that provides the thread run () for the target object (the class that implements the Runnable). Advantage: Classes that implement Interface runnable can still inherit from other parent classes . The code listing is as follows:

public class Demo2 implements Runnable{private Thread Mythread;boolean isrunning = true;   Stop Mark public void Run () {          //method body while (isrunning) {try{...} catch (Interruptedexception e) {e.printstacktrace ();}}} Public Demo2 () {                  //constructor mythread =new Thread (this);      Instantiate object Mythread.start ();} public static void Main (string[] args) {Demo2 demo2 =new Demo2 ();}}


Description: First create a thread member variable mythread, and then use the constructor method new thread (this) to create a threading object, where the create thread is constructed using thread (Runnable target), where this represents this instance, It is an implementation class that implements the Runnable interface.

Three, the realization way 3

Method 3 is an improvement to Method 2, and the code listing is as follows:

public class Demo3 implements Runnable{private Thread Mythread;boolean isrunning = true;   Termination token public Demo2 () {                  //constructor mythread =new Thread (New Runnable () {<pre name= "code" class= "java" >public void Run () {          //method body while (isrunning) {try{...} catch (Interruptedexception e) {e.printstacktrace ();}}});      Instantiate object Mythread.start ();} public static void Main (string[] args) {Demo2 demo2 =new Demo2 ();}}


Note: Compared with mode 2, we found that the frame class no longer implements the Runnable interface, but when instantiating the thread class,defines an anonymous inner class that implements the Runnable interface。 This method is often used in Android application development.





Analysis of three implementations of Java threads

Related Article

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.