How to create and run a java thread

To interpret a thread, you must understand what a process is.

What is a process?

A process refers to a running application. Each process has its own independent address space (memory space). For example, a user clicks IE browser on the desktop to start a process, and the operating system will allocate an independent address space for the process. When the user clicks the IE browser on the left again, another process is started, and the operating system will allocate a new independent address space for the new process. At present, operating systems support multi process.

Important: each time a user starts a process, the operating system will allocate an independent memory space for the process.

Threads -- Concepts

After understanding the process, it is easier to understand the concept of thread.

What is a thread?

It is an entity in a process and the basic unit independently scheduled and dispatched by the system. A thread does not own system resources, but only has some essential resources in operation, but it can share all the resources owned by the process with other threads belonging to the same process. One thread can create and undo another thread, and multiple threads in the same process can execute concurrently. Threads have three basic states: ready, blocked and running.

The java thread class is also an object class, and its instances inherit from Java Lang. thread or its subclasses. You can create a thread in Java as follows:

Executing the thread can call the thread's start() method:

In the above example, we did not write running code for the thread, so the thread terminated after calling the method.

There are two ways to write the code executed by the thread Runtime: one is to create an instance of the thread subclass and rewrite the run method; the other is to implement the runnable interface when creating the class. Next, we will explain these two methods in detail:

Create a subclass of thread

Create an instance of the thread subclass and override the run method, which will be executed after calling the start () method. Examples are as follows:

You can create and run the thread subclass described above as follows

Once the thread is started, the start method will return immediately instead of waiting until the run method is completed. It's like the run method is executed on another CPU. When the run method is executed, the string myThread running will be printed out.

You can also create an anonymous subclass of thread as follows:

When the run method of the new thread is executed, the computer will print out the string "thread running".

Implement runnable interface

The second way to write thread execution code is to create a new one that implements Java An instance of the class of the lang. runnable interface. The methods in the instance can be called by threads.

Examples are given below:

To enable the thread to execute the run () method, you need to pass in the instance object of myrunnable in the constructor of the thread class. Examples are as follows:

When the thread runs, it will call the run method that implements the runnable interface. "Myrunnable running" will be printed in the above example.

Similarly, you can create an anonymous class that implements the runnable interface, as shown below:

Create a subclass or implement the runnable interface?

There is no definite answer to which of the these two methods is better. They can meet requirements. In my opinion, I prefer to implement the runnable interface. Because the thread pool can effectively manage the threads that implement the runnable interface, if the thread pool is full, new threads will queue for execution until the thread pool is idle. If the thread is implemented by implementing the thread subclass, this will be more complex.

Sometimes we need to implement runnable interface and thread subclass at the same time. For example, an instance that implements the thread subclass can execute multiple threads that implement the runnable interface. A typical application is thread pool.

Common error: call the run () method instead of the start () method

A common error when creating and running a thread is to call the thread's run () method instead of the start () method, as shown below:

At first you don't feel anything wrong, because the run () method is called as you want. However, in fact, the run () method is not executed by the newly created thread, but by the current thread that created the new thread. That is, it is executed by the thread executing the above two lines of code. If you want the new thread to execute the run () method, you must call the start method of the new thread.

Thread name

When creating a thread, you can give the thread a name. It helps us distinguish between different threads. For example, if multiple threads write to system Out, we can easily find out which thread is outputting through the thread name. Examples are as follows:

It should be noted that since myrunnable is not a subclass of thread, the myrunnable class does not have a getname () method. You can get the reference of the current thread in the following ways:

Therefore, the name of the current thread can be obtained through the following code:

Thread code example:

Here is a small example. First, output the name of the thread executing the main () method. This thread is allocated by the JVM. Then start 10 threads, named 1 ~ 10. Each thread exits after outputting its own name.

summary

The above is all about creating and running a java thread method in this article. I hope it will be helpful to you. Interested friends can continue to refer to this site: talking about the advantages and code examples of Java multithreading, Java creation and end thread code examples, simple implementation code examples of java thread safe counters, etc. if there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>