Two methods for creating a java thread

Source: Internet
Author: User

Two methods for creating a java thread

Java providesTwoMethods

 

1. Create a Thread (java. lang. Thread) subclass.

2. Create a Runnable (java. lang. Runnable) Class

 

Override the run Method

In fact, both are similar in code writing. Build a Thread class, and then override the run method. Both Thread and Runnable have a member method.

Public void run ()

In this method, rewrite your business logic.

 

Thread startup

Although the Execution Code of the Thread is in the member method run, the startup or running of the Thread does not directly call the member method run, but calls the member method of java. lang. Thread:

Public void start ()

After the start method is called, java virtual will automatically start the thread.

 

1. Construct a Thread subclass

Package com. zero; public class TestThread extends Thread {private int times; // constructor public TestThread (int times) {this. times = times; System. out. println (thread + times + creation);} // rewrite the run method @ Overridepublic void run () {for (int I = 0; I <10; I ++) {System. out. println (thread + this. times + running);} public static void main (String [] arges) {// create two threads TestThread testThread1 = new TestThread (1 ); testThread testThread2 = new TestThread (2); // start the thread testThread1.start (); testThread2.start ();}}


Execution result:

First, Second, Third

Note: thread execution is unpredictable. So the results may not always be the same.

 

At first, I thought it was a direct subclass of Thread to calculate the Thread class. Later I found that it was only a subclass of Thread, whether or not it was a direct subclass.

 

2. Implement the Runnable interface to construct a thread

 

Package com. zero; // implement the Runnable interface public class TestRunnable implements Runnable {private int times; // constructor public TestRunnable (int times) {this. times = times; System. out. println (thread + times + creation);} // rewrite the run method @ Overridepublic void run () {for (int I = 0; I <10; I ++) {System. out. println (thread + times + running); }}// main method public static void main (String [] args) {// instantiate Thread 1, 2 Thread testRunnable1 = new Thread (new TestRunnable (1); Thread testRunnable2 = new Thread (new TestRunnable (2); testrunlenable1.start (); testRunnable2.start ();}}


 

Running result:

First, Second, Third

The principle of running results is actually the same as that in the first example.

 

 

Finally, let's talk about it.The difference between the two methods in the Construction of Thread class instantiation

The first method inherits the Thread. when instantiating an object, you can simply create a New subclass. However, the second method of implementing the interface will be slightly different when the Thread class is instantiated.

The second is through the java. lang. Thread constructor.

Public Thread (Runnabke target)

To construct the Instance Object of the Thread. The target parameter type of the constructor is Runnable.

That is, the instantiation of the line (thread) statement is as follows (based on Example 2)

 

Runnable Runnable3 = new TestRunnable(3);Thread testRunnable3 = new Thread(Runnable3);
Or

 

 

Thread testRunnable3 = new Thread(new TestRunnable(3));

Of course, both threads start the start method.

 

 

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.