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

What are the ways to create threads in traditional threading technology

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shares with you about the ways of creating threads in traditional threading technology. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

In traditional threading technology, there are two ways to create threads: one is to inherit the Thread class and rewrite the run () method; the other is to implement the Runnable interface, override the run () method in the interface, and throw the implementation of the Runnable interface to Thread. Most people may know these two ways, but why can they play like this? Let's analyze the context of these two methods in detail.

1. Uncover run () in Thread

Above we see that both of these methods are related to the run () method, so let's take a look at what the run () method does in the source code of Thread:

Overridepublic void run () {if (target! = null) {target.run ();}}

We can see that the run () method is very simple, there is only an if statement, if the target is not empty, execute the run () method of target, otherwise do nothing, then who is the target? When we click in, we can see:

Private Runnable target

It turns out that target is the Runnable interface. Let's click into Runnable to have a look:

@ FunctionalInterfacepublic interface Runnable {public abstract void run ();}

There is only one method in Runnable, which is also the run () method! Well, now back to the run () method of the Thread class, if target is not empty, we implement the Runnable interface, that is, the run () method in Runnable, then we use the run () method in that interface; if target is empty, that is, we do not implement the Runnable interface, then we do nothing, that is, the thread disappears immediately after creation.

So at this point, you can see why there are two ways to create threads. First: don't you have to make an if judgment first? I don't judge now, I kill your if, I write my own code in the run () method, and I can do whatever I want, that is, rewrite the run () method in Thread; second: don't you want to make if judgment first? OK, I'll give you a Runnable interface for you to judge, but you still have to call the run () method in my Runnable, so I'll just override the run () method in my Runnable! Once you know the whole story, let's write an example of these two traditional ways.

two。 Creation method 1: inherit Thread class

It takes only two steps to create and start a thread:

Inherit the Thread class and implement the run () method

Call the start () method to start the thread.

Since you only need to implement a run () method, we can use an anonymous inner class in Java to do this, as follows:

Public class TraditionalThread {public static void main (String [] args) {/ * the first method: inherit the Thread class and override the run () method * / Thread thread1 = new Thread () {@ Override public void run () {try {Thread.sleep (500) / / Let the thread rest for 500ms} catch (InterruptedException e) {e.printStackTrace ();} System.out.println (Thread.currentThread (). GetName ()); / / print out the current thread name}}; thread1.start (); / / Open thread}} 3. Creation method 2: implement Runnable interface

It takes only two steps to create and start a thread:

Implement the Runnable interface and the run () method

Call the start () method to start the thread.

Since you only need to implement a run () method, you can also use an anonymous inner class in Java, as follows:

Public class TraditionalThread {public static void main (String [] args) {/ * second method: implement the Runnable interface and throw it to Thread * / Thread thread2 = new Thread (new Runnable () {@ Override public void run () {try {Thread.sleep (500)) } catch (InterruptedException e) {e.printStackTrace ();} System.out.println (Thread.currentThread (). GetName ());}}); thread2.start ();}} 4. Two ways to use at the same time

If a buddy is good, he uses both methods: he implements the run () method in the Thread class, and throws a Runnable that implements the run () method to Thread. As follows:

Public class TraditionalThread {public static void main (String [] args) {/ / this guy's code is quite good new Thread (new Runnable () {@ Override public void run () {try {Thread.sleep (500);} catch (InterruptedException e) {e.printStackTrace ()) } System.out.println ("Runnable:" + Thread.currentThread (). GetName ();}) {@ Override public void run () {try {Thread.sleep (500);} catch (InterruptedException e) {e.printStackTrace () } System.out.println ("Thread:" + Thread.currentThread () .getName ());} .start ();}}

Which one will be implemented now? If we run the above program, we will find that it will print out the information of Thread, so we are running the run () method of Thread, and we know the conclusion, but why?

From the object-oriented thinking to consider: the above code is actually a new new object (child object) inherited the Thread object (parent object), in the child object rewrote the parent class run () method, the parent object threw a Runnable into the parent object, the run () method in the parent object is the original run () method with if judgment.

All right, now, after executing start (), you must first find the run () method in the subclass, and if you find it, the run () method of the parent class will be killed naturally, so it will print out Thread:,. If we assume that the run () method is not overridden in the subclass, then we must go to the parent class to find the run () method. The run () method of the parent class has to determine whether there is a Runnable coming in. So execute the run () method in Runnable, and Runnable: will be printed.

Thank you for reading! This is the end of this article on "what are the ways to create threads in traditional threading technology?". I hope the above content can be of some help to you, so that 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