How the Java Thread class implements run in subclasses
This article introduces how the Java Thread class implements run in the subclass, the content is very detailed, interested friends can refer to it, I hope it can be helpful to you.
Java Thread class has a lot of problems in our learning process, so let's see how to learn it better. Let's imagine what we need to do to create a new thread. Obviously, we have to specify the code that this thread is going to execute, and that's all we need to do to implement multithreading in Java!
As a fully object-oriented language, Java provides a class java.lang.Thread to facilitate multithreaded programming, which provides a large number of ways for us to control our own threads.
So how do we provide Java with the code we want the thread to execute? Let's take a look at the Java Thread class. The most important method of the Java Thread class is run (), which is called by the method start () of the Thread class and provides the code to be executed by our thread. To specify our own code, we just need to overwrite it!
Inherit the Java Thread class and override the method run (). We rewrite run () in the subclass of the created Thread class to add the code to be executed by the thread. Here is an example:
Public class TwoThread extends Thread {public void run () {for (int I = 0; I < 10; iTunes +) {System.out.println ("New thread");}} public static void main (String [] args) {TwoThread tt = new TwoThread (); tt.start (); for (int I = 0; I < 10; iTunes +) {System.out.println ("Main thread");}
This method is simple and clear, in line with everyone's habits, but it also has a big disadvantage, that is, if our class already inherits from a class, we can no longer inherit the Java Thread class.
On the Java Thread class how to implement run in the subclass to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.