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 is Java multithreading

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

Share

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

This article mainly explains "what is Java multithreading". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what is Java multithreading.

What is a thread?

In order to explain what "thread" is, I will start with "process". Take a look at the concept of "process":

❝process is a sequence of instructions that executable programs store in computer memory. It is a process of dynamic execution. ❞

After seeing this concept, do you feel completely confused? how should we understand it? Let's imagine a scene in which we usually use computers.

Do we usually type the code while listening to music while typing the code while chatting with our girlfriend on QQ (except for the last one, I occupied it two days ago, woo). Music player, code editor, QQ these three software are running at the same time, so that we can do a lot of things together, then these three software can be run at the same time, it is the "process" at work. We can open the task manager of Windows, and there is a tab called "processes" in the task manager of Windows.

After opening the windows task manager, we can see all the processes running in the current operating system. For example, we see the process of QQ, the process of Google browser, and so on. And some software can have multiple processes, such as some antivirus software, database software and so on.

In fact, the early operating systems were all single-task operating systems, such as QQ and music players, which can only be run separately, one running before the next. For example, imagine if it is particularly inconvenient for you to listen to songs before you can reply to your friends' questions in QQ.

Our current operating system is a multitasking operating system, which can run multiple programs at the same time, and we can reply to messages while listening to songs. That's why our process is working.

Back to the point, let's talk about what a thread is:

A ❝thread is a smaller running unit than a process, and a process contains multiple threads. ❞

For example, a program is made up of many lines of code, then the code can be divided into many blocks and put into different threads to execute separately, so we think that "the thread is equivalent to a subroutine".

Now that we know the concepts of "process" and "thread", the problem arises. We know that programs run on "CPU", so if you only have one "CPU", how can you ensure that these programs can run at the same time? We can imagine that the execution time of "CPU" is divided into many small pieces, each small piece of time is fixed, we can call this small piece "time slice", the time slice can be very short, for example, a millisecond, so if we have music player, code editor, QQ three software running at the same time, then how can they get the execution time of CPU? This is actually random. Consider this: our music player runs for one millisecond, and then it transfers the right to use CPU to the code editor, and the code editor runs for one millisecond to transfer the right to use CPU to QQ. Then these programs take turns to use CPU in a very short period of time. For CPU, these software actually take turns running, but because the interval between them is very short. As our users, we can't feel its change, so we will think that the software is running at the same time, which is why the software can run at the same time when there is only one CPU. This is called the rotation of time slices. Is through the rotation of the time of the CPU to achieve the effect of running at the same time.

Creation of threads

There are two ways to create threads:

The first: create a Thread class, or an object of a Thread subclass.

The second: create an object of a class that implements the Runnable interface.

This involves a Thread class and a Runnable interface. Let's take a look at the properties and methods of the classes and interfaces defined for us by these two systems:

Thread class

Thread is a thread class under the java.lang package

Common methods of Thread class

Create a thread case

Create a thread class by inheriting the Thread class and override the run () method

❝here I would like to remind you, for many beginners, there is always a question, why it inherits the Thread class is a thread, we say, a lot of things in Java are written by others we use, Java is so stipulated for everyone, you only inherit the Thread class, achieve the Runnable interface these two ways to get threads, so we do it in this way, so we can achieve our goals. ❞

Code demonstration

Package com.thread; class MyThread extends Thread {@ Override public void run () {System.out.println ("this thread is executing!") ;} public class ThreadTest {public static void main (String [] args) {MyThread mt = new MyThread (); mt.start (); / / start thread}}

Running result

❝does not know what you can see from the above code, I would like to emphasize that when starting a thread, we do not call the run () method. When we learned before, the corresponding method was called when the content was executed, but in the thread, when we use the start () method to start the thread, the code inside the run () method is executed when starting the thread and executing the thread, which requires our attention. ❞

❝also needs to note that a thread can only execute once, that is, it can only call the start () method ❞once.

Package com.thread; class MyThread extends Thread {@ Override public void run () {System.out.println ("this thread is executing!") ;} public class ThreadTest {public static void main (String [] args) {MyThread mt = new MyThread (); mt.start (); / start thread mt.start ();}}

❝as shown in the figure, if the start () method is called multiple times, an exception will be thrown. ❞

Create multiple thread cases

Package com.thread; class MyThread extends Thread {public MyThread (String name) {super (name);} @ Override public void run () {for (int iTuno)

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