Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How the Java Thread class implements run in subclasses

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report