In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.